2020-05-22 21:46:13 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2021-07-14 23:15:40 +02:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-05-22 21:46:13 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-22 21:46:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-10-01 20:07:44 +13:00
|
|
|
#include <AK/FlyString.h>
|
2021-07-14 23:53:11 +02:00
|
|
|
#include <AK/Function.h>
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-05-22 21:46:13 +02:00
|
|
|
#include <AK/Types.h>
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
#include <AK/Variant.h>
|
2020-05-22 21:46:13 +02:00
|
|
|
#include <AK/Vector.h>
|
2025-07-19 19:35:33 -07:00
|
|
|
#include <LibWeb/Export.h>
|
2020-05-22 21:46:13 +02:00
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2020-05-22 21:46:13 +02:00
|
|
|
|
2021-07-15 15:53:54 +02:00
|
|
|
class HTMLTokenizer;
|
|
|
|
|
|
2025-07-19 19:35:33 -07:00
|
|
|
class WEB_API HTMLToken {
|
2021-07-15 22:32:36 +02:00
|
|
|
AK_MAKE_NONCOPYABLE(HTMLToken);
|
2023-06-16 16:16:16 +02:00
|
|
|
AK_MAKE_DEFAULT_MOVABLE(HTMLToken);
|
2020-05-22 21:46:13 +02:00
|
|
|
|
|
|
|
|
public:
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
enum class Type : u8 {
|
2020-05-24 20:24:43 +02:00
|
|
|
Invalid,
|
2020-05-22 21:46:13 +02:00
|
|
|
DOCTYPE,
|
|
|
|
|
StartTag,
|
|
|
|
|
EndTag,
|
|
|
|
|
Comment,
|
|
|
|
|
Character,
|
|
|
|
|
EndOfFile,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-14 23:15:40 +02:00
|
|
|
struct Position {
|
|
|
|
|
size_t line { 0 };
|
|
|
|
|
size_t column { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-14 23:17:35 +02:00
|
|
|
struct Attribute {
|
2023-11-04 11:26:44 +13:00
|
|
|
Optional<FlyString> prefix;
|
2023-10-08 12:19:41 +13:00
|
|
|
FlyString local_name;
|
2023-11-04 11:26:44 +13:00
|
|
|
Optional<FlyString> namespace_;
|
2023-10-08 12:19:41 +13:00
|
|
|
String value;
|
2021-07-14 23:15:40 +02:00
|
|
|
Position name_start_position;
|
|
|
|
|
Position value_start_position;
|
|
|
|
|
Position name_end_position;
|
|
|
|
|
Position value_end_position;
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-15 00:03:50 +02:00
|
|
|
struct DoctypeData {
|
|
|
|
|
// NOTE: "Missing" is a distinct state from the empty string.
|
2023-11-04 10:08:07 +01:00
|
|
|
String name;
|
|
|
|
|
String public_identifier;
|
|
|
|
|
String system_identifier;
|
2021-07-15 00:03:50 +02:00
|
|
|
bool missing_name { true };
|
|
|
|
|
bool missing_public_identifier { true };
|
|
|
|
|
bool missing_system_identifier { true };
|
|
|
|
|
bool force_quirks { false };
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-08 14:20:08 +13:00
|
|
|
static HTMLToken make_start_tag(FlyString const& tag_name)
|
2020-07-23 17:30:03 +02:00
|
|
|
{
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken token { Type::StartTag };
|
2023-11-04 10:08:07 +01:00
|
|
|
token.set_tag_name(tag_name);
|
2020-07-23 17:30:03 +02:00
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken() = default;
|
|
|
|
|
|
|
|
|
|
HTMLToken(Type type)
|
|
|
|
|
: m_type(type)
|
|
|
|
|
{
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
switch (m_type) {
|
|
|
|
|
case Type::Character:
|
|
|
|
|
m_data.set(0u);
|
|
|
|
|
break;
|
|
|
|
|
case Type::DOCTYPE:
|
|
|
|
|
m_data.set(OwnPtr<DoctypeData> {});
|
|
|
|
|
break;
|
|
|
|
|
case Type::StartTag:
|
|
|
|
|
case Type::EndTag:
|
|
|
|
|
m_data.set(OwnPtr<Vector<Attribute>>());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-07-15 15:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-24 00:14:23 +02:00
|
|
|
bool is_doctype() const { return m_type == Type::DOCTYPE; }
|
|
|
|
|
bool is_start_tag() const { return m_type == Type::StartTag; }
|
|
|
|
|
bool is_end_tag() const { return m_type == Type::EndTag; }
|
|
|
|
|
bool is_comment() const { return m_type == Type::Comment; }
|
|
|
|
|
bool is_character() const { return m_type == Type::Character; }
|
|
|
|
|
bool is_end_of_file() const { return m_type == Type::EndOfFile; }
|
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
u32 code_point() const
|
2020-05-24 19:51:50 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_character());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_data.get<u32>();
|
2020-05-24 19:51:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 23:33:12 +02:00
|
|
|
void set_code_point(u32 code_point)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_character());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_data.get<u32>() = code_point;
|
2021-07-14 23:33:12 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
String const& comment() const
|
2021-07-14 23:32:18 +02:00
|
|
|
{
|
|
|
|
|
VERIFY(is_comment());
|
2023-11-04 10:08:07 +01:00
|
|
|
return m_comment_data;
|
2021-07-14 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
void set_comment(String comment)
|
2021-07-14 23:32:18 +02:00
|
|
|
{
|
|
|
|
|
VERIFY(is_comment());
|
2023-11-04 10:08:07 +01:00
|
|
|
m_comment_data = move(comment);
|
2021-07-14 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
FlyString const& tag_name() const
|
2020-05-24 00:14:23 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2023-11-04 10:08:07 +01:00
|
|
|
return m_string_data;
|
2020-05-24 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
void set_tag_name(FlyString name)
|
2021-07-14 23:37:48 +02:00
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_string_data = move(name);
|
2021-07-14 23:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-24 00:14:23 +02:00
|
|
|
bool is_self_closing() const
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_tag_self_closing;
|
2020-05-24 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 23:37:48 +02:00
|
|
|
void set_self_closing(bool self_closing)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_tag_self_closing = self_closing;
|
2021-07-14 23:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 23:53:11 +02:00
|
|
|
size_t attribute_count() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
if (auto* ptr = tag_attributes())
|
|
|
|
|
return ptr->size();
|
|
|
|
|
return 0;
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add_attribute(Attribute attribute)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
ensure_tag_attributes().append(move(attribute));
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-17 17:09:20 -07:00
|
|
|
void for_each_attribute(Function<IterationDecision(Attribute const&)> callback) const
|
2021-07-14 23:53:11 +02:00
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
|
if (!ptr)
|
|
|
|
|
return;
|
|
|
|
|
for (auto& attribute : *ptr) {
|
2021-07-14 23:53:11 +02:00
|
|
|
if (callback(attribute) == IterationDecision::Break)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 09:27:43 +13:00
|
|
|
Optional<String> attribute(FlyString const& attribute_name) const
|
2023-08-24 17:43:05 -04:00
|
|
|
{
|
|
|
|
|
if (auto result = raw_attribute(attribute_name); result.has_value())
|
|
|
|
|
return result->value;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 11:42:00 +13:00
|
|
|
Optional<Attribute const&> raw_attribute(FlyString const& attribute_name) const
|
2020-05-28 12:18:46 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
|
|
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
|
if (!ptr)
|
|
|
|
|
return {};
|
2023-10-08 12:19:41 +13:00
|
|
|
for (auto const& attribute : *ptr) {
|
|
|
|
|
if (attribute_name == attribute.local_name)
|
2023-08-24 17:43:05 -04:00
|
|
|
return attribute;
|
2020-05-28 12:18:46 +02:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 11:42:00 +13:00
|
|
|
bool has_attribute(FlyString const& attribute_name) const
|
2020-10-12 01:51:28 +01:00
|
|
|
{
|
2023-11-11 09:27:43 +13:00
|
|
|
return attribute(attribute_name).has_value();
|
2020-10-12 01:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 00:03:50 +02:00
|
|
|
DoctypeData const& doctype_data() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_doctype());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = m_data.get<OwnPtr<DoctypeData>>().ptr();
|
|
|
|
|
VERIFY(ptr);
|
|
|
|
|
return *ptr;
|
2021-07-15 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 18:58:11 +02:00
|
|
|
DoctypeData& ensure_doctype_data()
|
2021-07-15 00:03:50 +02:00
|
|
|
{
|
|
|
|
|
VERIFY(is_doctype());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto& ptr = m_data.get<OwnPtr<DoctypeData>>();
|
|
|
|
|
if (!ptr)
|
|
|
|
|
ptr = make<DoctypeData>();
|
|
|
|
|
return *ptr;
|
2021-07-15 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
2023-11-05 11:45:55 +13:00
|
|
|
String to_string() const;
|
2020-05-24 00:14:23 +02:00
|
|
|
|
2021-07-14 23:15:40 +02:00
|
|
|
Position const& start_position() const { return m_start_position; }
|
|
|
|
|
Position const& end_position() const { return m_end_position; }
|
2021-05-20 23:11:41 +04:30
|
|
|
|
2021-07-15 15:53:54 +02:00
|
|
|
void set_start_position(Badge<HTMLTokenizer>, Position start_position) { m_start_position = start_position; }
|
|
|
|
|
void set_end_position(Badge<HTMLTokenizer>, Position end_position) { m_end_position = end_position; }
|
LibWeb: Complete Rust HTML tree construction
Finish the Rust implementation of the spec tree-construction algorithms
needed by the LibWeb test suite. Add the remaining table modes, foster
parenting, scope helpers, adoption agency handling, ruby/list/form and
select cases, frameset state, foreign-content edge cases, and parser
host callbacks.
Preserve behavior that depends on the C++ DOM integration, including
parser-created custom element reactions, fragment quirks mode, arbitrary
fragment namespaces, template fragment mode, fragment form ownership,
MathML annotation-xml boundaries, contextual fragment scripts, parser
script source positions, document.close() parser state, void-element
insertion, and duplicate attribute tracking.
Add focused tests for the parser edge cases that are easy to regress at
the boundary between the Rust tree builder and the C++ DOM host.
2026-05-15 21:56:35 +02:00
|
|
|
void set_had_duplicate_attribute(Badge<HTMLTokenizer>) { m_had_duplicate_attribute = true; }
|
2021-07-15 15:53:54 +02:00
|
|
|
|
2024-09-30 17:52:30 -06:00
|
|
|
void normalize_attributes();
|
2024-12-02 12:33:52 +00:00
|
|
|
bool had_duplicate_attribute() const { return m_had_duplicate_attribute; }
|
2024-09-30 17:52:30 -06:00
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
private:
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
Vector<Attribute> const* tag_attributes() const
|
|
|
|
|
{
|
|
|
|
|
return m_data.get<OwnPtr<Vector<Attribute>>>().ptr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<Attribute>* tag_attributes()
|
|
|
|
|
{
|
|
|
|
|
return m_data.get<OwnPtr<Vector<Attribute>>>().ptr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<Attribute>& ensure_tag_attributes()
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
|
|
|
|
auto& ptr = m_data.get<OwnPtr<Vector<Attribute>>>();
|
|
|
|
|
if (!ptr)
|
|
|
|
|
ptr = make<Vector<Attribute>>();
|
|
|
|
|
return *ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 20:24:43 +02:00
|
|
|
Type m_type { Type::Invalid };
|
2020-05-22 21:46:13 +02:00
|
|
|
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
// Type::StartTag and Type::EndTag
|
|
|
|
|
bool m_tag_self_closing { false };
|
|
|
|
|
|
2024-12-02 12:33:52 +00:00
|
|
|
// AD-HOC: We need to know if the token had duplicate attributes, as Content Security Policy disables the nonce
|
|
|
|
|
// attribute on the element that will be created from such a token.
|
|
|
|
|
// https://w3c.github.io/webappsec-csp/#is-element-nonceable
|
|
|
|
|
bool m_had_duplicate_attribute { false };
|
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
// Type::StartTag and Type::EndTag (tag name)
|
|
|
|
|
FlyString m_string_data;
|
|
|
|
|
|
|
|
|
|
// Type::Comment (comment data)
|
|
|
|
|
String m_comment_data;
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
|
2021-09-19 23:00:45 +02:00
|
|
|
Variant<Empty, u32, OwnPtr<DoctypeData>, OwnPtr<Vector<Attribute>>> m_data {};
|
2021-05-20 23:11:41 +04:30
|
|
|
|
|
|
|
|
Position m_start_position;
|
|
|
|
|
Position m_end_position;
|
2020-05-22 21:46:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2026-03-19 09:03:08 +01:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct SentinelOptionalTraits<Web::HTML::HTMLToken> {
|
|
|
|
|
static Web::HTML::HTMLToken sentinel_value() { return {}; }
|
|
|
|
|
static bool is_sentinel(Web::HTML::HTMLToken const& value) { return value.type() == Web::HTML::HTMLToken::Type::Invalid; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
class Optional<Web::HTML::HTMLToken> : public SentinelOptional<Web::HTML::HTMLToken> {
|
|
|
|
|
public:
|
|
|
|
|
using SentinelOptional::SentinelOptional;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|