LibWeb: Remove StringBuilders from HTMLToken::AttributeBuilder

This commit is contained in:
Gunnar Beutner 2021-05-23 08:50:48 +02:00 committed by Andreas Kling
parent 992964aa7d
commit 901d71148b
Notes: sideshowbarker 2024-07-18 09:01:44 +09:00
4 changed files with 41 additions and 33 deletions

View file

@ -437,7 +437,7 @@ NonnullRefPtr<DOM::Element> HTMLDocumentParser::create_element_for(const HTMLTok
{
auto element = create_element(document(), token.tag_name(), namespace_);
for (auto& attribute : token.m_tag.attributes) {
element->set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
element->set_attribute(attribute.local_name, attribute.value);
}
return element;
}
@ -1120,9 +1120,9 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
if (m_stack_of_open_elements.contains(HTML::TagNames::template_))
return;
for (auto& attribute : token.m_tag.attributes) {
if (current_node().has_attribute(attribute.local_name_builder.string_view()))
if (current_node().has_attribute(attribute.local_name))
continue;
current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
current_node().set_attribute(attribute.local_name, attribute.value);
}
return;
}
@ -1147,9 +1147,9 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
m_frameset_ok = false;
auto& body_element = m_stack_of_open_elements.elements().at(1);
for (auto& attribute : token.m_tag.attributes) {
if (body_element.has_attribute(attribute.local_name_builder.string_view()))
if (body_element.has_attribute(attribute.local_name))
continue;
body_element.set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
body_element.set_attribute(attribute.local_name, attribute.value);
}
return;
}

View file

@ -43,9 +43,9 @@ String HTMLToken::to_string() const
builder.append(m_tag.tag_name.to_string());
builder.append("', { ");
for (auto& attribute : m_tag.attributes) {
builder.append(attribute.local_name_builder.to_string());
builder.append(attribute.local_name);
builder.append("=\"");
builder.append(attribute.value_builder.to_string());
builder.append(attribute.value);
builder.append("\" ");
}
builder.append("} }");

View file

@ -106,8 +106,8 @@ public:
{
VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) {
if (attribute_name == attribute.local_name_builder.string_view())
return attribute.value_builder.string_view();
if (attribute_name == attribute.local_name)
return attribute.value;
}
return {};
}
@ -130,9 +130,8 @@ public:
{
VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) {
if (old_name == attribute.local_name_builder.string_view()) {
attribute.local_name_builder.clear();
attribute.local_name_builder.append(new_name);
if (old_name == attribute.local_name) {
attribute.local_name = new_name;
}
}
}
@ -141,12 +140,9 @@ public:
{
VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) {
if (old_name == attribute.local_name_builder.string_view()) {
if (old_name == attribute.local_name) {
attribute.prefix = prefix;
attribute.local_name_builder.clear();
attribute.local_name_builder.append(local_name);
attribute.local_name = local_name;
attribute.namespace_ = namespace_;
}
}
@ -179,9 +175,9 @@ private:
struct AttributeBuilder {
String prefix;
StringBuilder local_name_builder;
String local_name;
String namespace_;
StringBuilder value_builder;
String value;
Position name_start_position;
Position value_start_position;
Position name_end_position;

View file

@ -78,7 +78,7 @@ namespace Web::HTML {
do { \
for (auto code_point : m_temporary_buffer) { \
if (consumed_as_part_of_an_attribute()) { \
m_current_token.m_tag.attributes.last().value_builder.append_code_point(code_point); \
m_current_builder.append_code_point(code_point); \
} else { \
create_new_token(HTMLToken::Type::Character); \
m_current_token.m_comment_or_character.data.append_code_point(code_point); \
@ -1007,9 +1007,9 @@ _StartOfFunction:
log_parse_error();
auto new_attribute = HTMLToken::AttributeBuilder();
new_attribute.name_start_position = nth_last_position(1);
new_attribute.local_name_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
m_current_token.m_tag.attributes.append(new_attribute);
SWITCH_TO(AttributeName);
SWITCH_TO_WITH_UNCLEAN_BUILDER(AttributeName);
}
ANYTHING_ELSE
{
@ -1045,34 +1045,39 @@ _StartOfFunction:
{
ON_WHITESPACE
{
m_current_token.m_tag.attributes.last().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName);
}
ON('/')
{
m_current_token.m_tag.attributes.last().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName);
}
ON('>')
{
m_current_token.m_tag.attributes.last().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName);
}
ON_EOF
{
m_current_token.m_tag.attributes.last().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName);
}
ON('=')
{
m_current_token.m_tag.attributes.last().name_end_position = nth_last_position(1);
m_current_token.m_tag.attributes.last().local_name = consume_current_builder();
SWITCH_TO(BeforeAttributeValue);
}
ON_ASCII_UPPER_ALPHA
{
m_current_token.m_tag.attributes.last().local_name_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
continue;
}
ON(0)
{
log_parse_error();
m_current_token.m_tag.attributes.last().local_name_builder.append_code_point(0xFFFD);
m_current_builder.append_code_point(0xFFFD);
continue;
}
ON('"')
@ -1093,7 +1098,7 @@ _StartOfFunction:
ANYTHING_ELSE
{
AnythingElseAttributeName:
m_current_token.m_tag.attributes.last().local_name_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
continue;
}
}
@ -1163,17 +1168,19 @@ _StartOfFunction:
{
ON('"')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
SWITCH_TO(AfterAttributeValueQuoted);
}
ON('&')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
m_return_state = State::AttributeValueDoubleQuoted;
SWITCH_TO(CharacterReference);
}
ON(0)
{
log_parse_error();
m_current_token.m_tag.attributes.last().value_builder.append_code_point(0xFFFD);
m_current_builder.append_code_point(0xFFFD);
continue;
}
ON_EOF
@ -1183,7 +1190,7 @@ _StartOfFunction:
}
ANYTHING_ELSE
{
m_current_token.m_tag.attributes.last().value_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
continue;
}
}
@ -1193,17 +1200,19 @@ _StartOfFunction:
{
ON('\'')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
SWITCH_TO(AfterAttributeValueQuoted);
}
ON('&')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
m_return_state = State::AttributeValueSingleQuoted;
SWITCH_TO(CharacterReference);
}
ON(0)
{
log_parse_error();
m_current_token.m_tag.attributes.last().value_builder.append_code_point(0xFFFD);
m_current_builder.append_code_point(0xFFFD);
continue;
}
ON_EOF
@ -1213,7 +1222,7 @@ _StartOfFunction:
}
ANYTHING_ELSE
{
m_current_token.m_tag.attributes.last().value_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
continue;
}
}
@ -1223,23 +1232,26 @@ _StartOfFunction:
{
ON_WHITESPACE
{
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(1);
m_current_token.m_tag.attributes.last().value = consume_current_builder();
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(2);
SWITCH_TO(BeforeAttributeName);
}
ON('&')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
m_return_state = State::AttributeValueUnquoted;
SWITCH_TO(CharacterReference);
}
ON('>')
{
m_current_token.m_tag.attributes.last().value = consume_current_builder();
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(1);
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
}
ON(0)
{
log_parse_error();
m_current_token.m_tag.attributes.last().value_builder.append_code_point(0xFFFD);
m_current_builder.append_code_point(0xFFFD);
continue;
}
ON('"')
@ -1275,7 +1287,7 @@ _StartOfFunction:
ANYTHING_ELSE
{
AnythingElseAttributeValueUnquoted:
m_current_token.m_tag.attributes.last().value_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
continue;
}
}
@ -1584,7 +1596,7 @@ _StartOfFunction:
ON_ASCII_ALPHANUMERIC
{
if (consumed_as_part_of_an_attribute()) {
m_current_token.m_tag.attributes.last().value_builder.append_code_point(current_input_character.value());
m_current_builder.append_code_point(current_input_character.value());
continue;
} else {
EMIT_CURRENT_CHARACTER;