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>
|
|
|
|
|
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;
|
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
class 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 };
|
|
|
|
};
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
static HTMLToken make_character(u32 code_point)
|
2020-05-28 18:43:52 +02:00
|
|
|
{
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken token { Type::Character };
|
2021-07-14 23:33:12 +02:00
|
|
|
token.set_code_point(code_point);
|
2020-05-28 18:43:52 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool is_parser_whitespace() const
|
|
|
|
{
|
|
|
|
// NOTE: The parser considers '\r' to be whitespace, while the tokenizer does not.
|
|
|
|
if (!is_character())
|
|
|
|
return false;
|
2020-08-05 16:31:20 -04:00
|
|
|
switch (code_point()) {
|
2020-05-24 19:51:50 +02:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\f':
|
|
|
|
case '\r':
|
|
|
|
case ' ':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-25 20:16:48 +02:00
|
|
|
bool has_acknowledged_self_closing_flag() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_self_closing());
|
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_acknowledged;
|
2020-05-25 20:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void acknowledge_self_closing_flag_if_set()
|
|
|
|
{
|
|
|
|
if (is_self_closing())
|
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_acknowledged = true;
|
2020-05-25 20:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 23:53:11 +02:00
|
|
|
bool has_attributes() 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
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
return ptr && !ptr->is_empty();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Attribute const& last_attribute() 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
|
|
|
VERIFY(has_attributes());
|
|
|
|
return tag_attributes()->last();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Attribute& last_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
|
|
|
VERIFY(has_attributes());
|
|
|
|
return tag_attributes()->last();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void drop_attributes()
|
|
|
|
{
|
|
|
|
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_data.get<OwnPtr<Vector<Attribute>>>().clear();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:09:20 -07:00
|
|
|
void for_each_attribute(Function<IterationDecision(Attribute&)> callback)
|
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
|
|
|
}
|
|
|
|
|
2023-11-04 10:08:07 +01:00
|
|
|
void adjust_tag_name(FlyString const& old_name, FlyString const& new_name)
|
2020-10-12 01:51:28 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2023-11-04 10:08:07 +01:00
|
|
|
if (old_name == tag_name())
|
2021-07-14 23:37:48 +02:00
|
|
|
set_tag_name(new_name);
|
2020-10-12 01:51:28 +01:00
|
|
|
}
|
|
|
|
|
2023-10-08 12:19:41 +13:00
|
|
|
void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name)
|
2020-06-21 06:58:03 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2021-07-14 23:53:11 +02:00
|
|
|
for_each_attribute([&](Attribute& attribute) {
|
|
|
|
if (old_name == attribute.local_name)
|
2021-05-23 08:50:48 +02:00
|
|
|
attribute.local_name = new_name;
|
2021-07-14 23:53:11 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-06-21 06:58:03 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 11:26:44 +13:00
|
|
|
void adjust_foreign_attribute(FlyString const& old_name, Optional<FlyString> const& prefix, FlyString const& local_name, Optional<FlyString> const& namespace_)
|
2020-06-21 06:58:03 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2021-07-14 23:53:11 +02:00
|
|
|
for_each_attribute([&](Attribute& attribute) {
|
2021-05-23 08:50:48 +02:00
|
|
|
if (old_name == attribute.local_name) {
|
2021-05-23 08:08:39 +02:00
|
|
|
attribute.prefix = prefix;
|
2021-05-23 08:50:48 +02:00
|
|
|
attribute.local_name = local_name;
|
2021-05-23 08:08:39 +02:00
|
|
|
attribute.namespace_ = namespace_;
|
2020-06-21 06:58:03 +02:00
|
|
|
}
|
2021-07-14 23:53:11 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-06-03 22:34:50 -06: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; }
|
|
|
|
|
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 };
|
|
|
|
bool m_tag_self_closing_acknowledged { 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|