1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

LibWeb: Hide implementation details of HTMLToken attribute list

Previously, HTMLToken would expose the Vector<Attribute> directly to
its users. In preparation for a future change, all users now use
implementation-agnostic APIs which do not expose the Vector directly.
This commit is contained in:
Max Wipfli 2021-07-14 23:53:11 +02:00 committed by Ali Mohammad Pur
parent 15d8635afc
commit 918bde98b1
6 changed files with 108 additions and 59 deletions

View file

@ -61,7 +61,7 @@ using Token = Web::HTML::HTMLToken;
#define EXPECT_TAG_TOKEN_ATTRIBUTE_COUNT(count) \ #define EXPECT_TAG_TOKEN_ATTRIBUTE_COUNT(count) \
VERIFY(last_token.has_value()); \ VERIFY(last_token.has_value()); \
EXPECT_EQ(last_token->attributes().size(), (size_t)count); EXPECT_EQ(last_token->attribute_count(), (size_t)(count));
static Vector<Token> run_tokenizer(StringView const& input) static Vector<Token> run_tokenizer(StringView const& input)
{ {

View file

@ -436,9 +436,10 @@ HTMLDocumentParser::AdjustedInsertionLocation HTMLDocumentParser::find_appropria
NonnullRefPtr<DOM::Element> HTMLDocumentParser::create_element_for(const HTMLToken& token, const FlyString& namespace_) NonnullRefPtr<DOM::Element> HTMLDocumentParser::create_element_for(const HTMLToken& token, const FlyString& namespace_)
{ {
auto element = create_element(document(), token.tag_name(), namespace_); auto element = create_element(document(), token.tag_name(), namespace_);
for (auto& attribute : token.m_tag.attributes) { token.for_each_attribute([&](auto& attribute) {
element->set_attribute(attribute.local_name, attribute.value); element->set_attribute(attribute.local_name, attribute.value);
} return IterationDecision::Continue;
});
return element; return element;
} }
@ -1117,11 +1118,11 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
log_parse_error(); log_parse_error();
if (m_stack_of_open_elements.contains(HTML::TagNames::template_)) if (m_stack_of_open_elements.contains(HTML::TagNames::template_))
return; return;
for (auto& attribute : token.m_tag.attributes) { token.for_each_attribute([&](auto& attribute) {
if (current_node().has_attribute(attribute.local_name)) if (!current_node().has_attribute(attribute.local_name))
continue; current_node().set_attribute(attribute.local_name, attribute.value);
current_node().set_attribute(attribute.local_name, attribute.value); return IterationDecision::Continue;
} });
return; return;
} }
if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::template_, HTML::TagNames::title)) { if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::template_, HTML::TagNames::title)) {
@ -1144,11 +1145,11 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
} }
m_frameset_ok = false; m_frameset_ok = false;
auto& body_element = m_stack_of_open_elements.elements().at(1); auto& body_element = m_stack_of_open_elements.elements().at(1);
for (auto& attribute : token.m_tag.attributes) { token.for_each_attribute([&](auto& attribute) {
if (body_element.has_attribute(attribute.local_name)) if (!body_element.has_attribute(attribute.local_name))
continue; body_element.set_attribute(attribute.local_name, attribute.value);
body_element.set_attribute(attribute.local_name, attribute.value); return IterationDecision::Continue;
} });
return; return;
} }

View file

@ -42,12 +42,13 @@ String HTMLToken::to_string() const
builder.append(" { name: '"); builder.append(" { name: '");
builder.append(tag_name()); builder.append(tag_name());
builder.append("', { "); builder.append("', { ");
for (auto& attribute : m_tag.attributes) { for_each_attribute([&](auto& attribute) {
builder.append(attribute.local_name); builder.append(attribute.local_name);
builder.append("=\""); builder.append("=\"");
builder.append(attribute.value); builder.append(attribute.value);
builder.append("\" "); builder.append("\" ");
} return IterationDecision::Continue;
});
builder.append("} }"); builder.append("} }");
} }

View file

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/Function.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
@ -150,6 +151,62 @@ public:
m_tag.self_closing_acknowledged = true; m_tag.self_closing_acknowledged = true;
} }
bool has_attributes() const
{
VERIFY(is_start_tag() || is_end_tag());
return !m_tag.attributes.is_empty();
}
size_t attribute_count() const
{
VERIFY(is_start_tag() || is_end_tag());
return m_tag.attributes.size();
}
void add_attribute(Attribute attribute)
{
VERIFY(is_start_tag() || is_end_tag());
m_tag.attributes.append(move(attribute));
}
Attribute const& last_attribute() const
{
VERIFY(is_start_tag() || is_end_tag());
VERIFY(!m_tag.attributes.is_empty());
return m_tag.attributes.last();
}
Attribute& last_attribute()
{
VERIFY(is_start_tag() || is_end_tag());
VERIFY(!m_tag.attributes.is_empty());
return m_tag.attributes.last();
}
void drop_attributes()
{
VERIFY(is_start_tag() || is_end_tag());
m_tag.attributes.clear();
}
void for_each_attribute(Function<IterationDecision(Attribute const&)> callback) const
{
VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) {
if (callback(attribute) == IterationDecision::Break)
break;
}
}
void for_each_attribute(Function<IterationDecision(Attribute&)> callback)
{
VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) {
if (callback(attribute) == IterationDecision::Break)
break;
}
}
StringView attribute(FlyString const& attribute_name) StringView attribute(FlyString const& attribute_name)
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
@ -175,29 +232,24 @@ public:
void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name) void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name)
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) { for_each_attribute([&](Attribute& attribute) {
if (old_name == attribute.local_name) { if (old_name == attribute.local_name)
attribute.local_name = new_name; attribute.local_name = new_name;
} return IterationDecision::Continue;
} });
} }
void adjust_foreign_attribute(FlyString const& old_name, FlyString const& prefix, FlyString const& local_name, FlyString const& namespace_) void adjust_foreign_attribute(FlyString const& old_name, FlyString const& prefix, FlyString const& local_name, FlyString const& namespace_)
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
for (auto& attribute : m_tag.attributes) { for_each_attribute([&](Attribute& attribute) {
if (old_name == attribute.local_name) { if (old_name == attribute.local_name) {
attribute.prefix = prefix; attribute.prefix = prefix;
attribute.local_name = local_name; attribute.local_name = local_name;
attribute.namespace_ = namespace_; attribute.namespace_ = namespace_;
} }
} return IterationDecision::Continue;
} });
void drop_attributes()
{
VERIFY(is_start_tag() || is_end_tag());
m_tag.attributes.clear();
} }
Type type() const { return m_type; } Type type() const { return m_type; }
@ -207,12 +259,6 @@ public:
Position const& start_position() const { return m_start_position; } Position const& start_position() const { return m_start_position; }
Position const& end_position() const { return m_end_position; } Position const& end_position() const { return m_end_position; }
Vector<Attribute> const& attributes() const
{
VERIFY(is_start_tag() || is_end_tag());
return m_tag.attributes;
}
private: private:
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };

View file

@ -996,8 +996,8 @@ _StartOfFunction:
} }
ON('/') ON('/')
{ {
if (!m_current_token.m_tag.attributes.is_empty()) if (m_current_token.has_attributes())
m_current_token.m_tag.attributes.last().name_end_position = nth_last_position(1); m_current_token.last_attribute().name_end_position = nth_last_position(1);
RECONSUME_IN(AfterAttributeName); RECONSUME_IN(AfterAttributeName);
} }
ON('>') ON('>')
@ -1014,14 +1014,14 @@ _StartOfFunction:
HTMLToken::Attribute new_attribute; HTMLToken::Attribute new_attribute;
new_attribute.name_start_position = nth_last_position(1); new_attribute.name_start_position = nth_last_position(1);
m_current_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); m_current_token.add_attribute(move(new_attribute));
SWITCH_TO_WITH_UNCLEAN_BUILDER(AttributeName); SWITCH_TO_WITH_UNCLEAN_BUILDER(AttributeName);
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
HTMLToken::Attribute new_attribute; HTMLToken::Attribute new_attribute;
new_attribute.name_start_position = nth_last_position(1); new_attribute.name_start_position = nth_last_position(1);
m_current_token.m_tag.attributes.append(move(new_attribute)); m_current_token.add_attribute(move(new_attribute));
RECONSUME_IN(AttributeName); RECONSUME_IN(AttributeName);
} }
} }
@ -1051,28 +1051,28 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.attributes.last().local_name = consume_current_builder(); m_current_token.last_attribute().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName); RECONSUME_IN(AfterAttributeName);
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.attributes.last().local_name = consume_current_builder(); m_current_token.last_attribute().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName); RECONSUME_IN(AfterAttributeName);
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.attributes.last().local_name = consume_current_builder(); m_current_token.last_attribute().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName); RECONSUME_IN(AfterAttributeName);
} }
ON_EOF ON_EOF
{ {
m_current_token.m_tag.attributes.last().local_name = consume_current_builder(); m_current_token.last_attribute().local_name = consume_current_builder();
RECONSUME_IN(AfterAttributeName); RECONSUME_IN(AfterAttributeName);
} }
ON('=') ON('=')
{ {
m_current_token.m_tag.attributes.last().name_end_position = nth_last_position(1); m_current_token.last_attribute().name_end_position = nth_last_position(1);
m_current_token.m_tag.attributes.last().local_name = consume_current_builder(); m_current_token.last_attribute().local_name = consume_current_builder();
SWITCH_TO(BeforeAttributeValue); SWITCH_TO(BeforeAttributeValue);
} }
ON_ASCII_UPPER_ALPHA ON_ASCII_UPPER_ALPHA
@ -1122,7 +1122,7 @@ _StartOfFunction:
} }
ON('=') ON('=')
{ {
m_current_token.m_tag.attributes.last().name_end_position = nth_last_position(1); m_current_token.last_attribute().name_end_position = nth_last_position(1);
SWITCH_TO(BeforeAttributeValue); SWITCH_TO(BeforeAttributeValue);
} }
ON('>') ON('>')
@ -1136,8 +1136,8 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_tag.attributes.append({}); m_current_token.add_attribute({});
m_current_token.m_tag.attributes.last().name_start_position = m_source_positions.last(); m_current_token.last_attribute().name_start_position = m_source_positions.last();
RECONSUME_IN(AttributeName); RECONSUME_IN(AttributeName);
} }
} }
@ -1145,7 +1145,7 @@ _StartOfFunction:
BEGIN_STATE(BeforeAttributeValue) BEGIN_STATE(BeforeAttributeValue)
{ {
m_current_token.m_tag.attributes.last().value_start_position = nth_last_position(1); m_current_token.last_attribute().value_start_position = nth_last_position(1);
ON_WHITESPACE ON_WHITESPACE
{ {
continue; continue;
@ -1174,12 +1174,12 @@ _StartOfFunction:
{ {
ON('"') ON('"')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
SWITCH_TO(AfterAttributeValueQuoted); SWITCH_TO(AfterAttributeValueQuoted);
} }
ON('&') ON('&')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
m_return_state = State::AttributeValueDoubleQuoted; m_return_state = State::AttributeValueDoubleQuoted;
SWITCH_TO(CharacterReference); SWITCH_TO(CharacterReference);
} }
@ -1206,12 +1206,12 @@ _StartOfFunction:
{ {
ON('\'') ON('\'')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
SWITCH_TO(AfterAttributeValueQuoted); SWITCH_TO(AfterAttributeValueQuoted);
} }
ON('&') ON('&')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
m_return_state = State::AttributeValueSingleQuoted; m_return_state = State::AttributeValueSingleQuoted;
SWITCH_TO(CharacterReference); SWITCH_TO(CharacterReference);
} }
@ -1238,20 +1238,20 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(2); m_current_token.last_attribute().value_end_position = nth_last_position(2);
SWITCH_TO(BeforeAttributeName); SWITCH_TO(BeforeAttributeName);
} }
ON('&') ON('&')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
m_return_state = State::AttributeValueUnquoted; m_return_state = State::AttributeValueUnquoted;
SWITCH_TO(CharacterReference); SWITCH_TO(CharacterReference);
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.attributes.last().value = consume_current_builder(); m_current_token.last_attribute().value = consume_current_builder();
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(1); m_current_token.last_attribute().value_end_position = nth_last_position(1);
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
ON(0) ON(0)
@ -1301,7 +1301,7 @@ _StartOfFunction:
BEGIN_STATE(AfterAttributeValueQuoted) BEGIN_STATE(AfterAttributeValueQuoted)
{ {
m_current_token.m_tag.attributes.last().value_end_position = nth_last_position(1); m_current_token.last_attribute().value_end_position = nth_last_position(1);
ON_WHITESPACE ON_WHITESPACE
{ {
SWITCH_TO(BeforeAttributeName); SWITCH_TO(BeforeAttributeName);

View file

@ -132,7 +132,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
{ palette.syntax_keyword(), {}, false, true }, { palette.syntax_keyword(), {}, false, true },
token->is_start_tag() ? AugmentedTokenKind::OpenTag : AugmentedTokenKind::CloseTag); token->is_start_tag() ? AugmentedTokenKind::OpenTag : AugmentedTokenKind::CloseTag);
for (auto& attribute : token->attributes()) { token->for_each_attribute([&](auto& attribute) {
highlight( highlight(
attribute.name_start_position.line, attribute.name_start_position.line,
attribute.name_start_position.column + token_start_offset, attribute.name_start_position.column + token_start_offset,
@ -147,7 +147,8 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
attribute.value_end_position.column + token_start_offset, attribute.value_end_position.column + token_start_offset,
{ palette.syntax_string(), {} }, { palette.syntax_string(), {} },
AugmentedTokenKind::AttributeValue); AugmentedTokenKind::AttributeValue);
} return IterationDecision::Continue;
});
} else if (token->is_doctype()) { } else if (token->is_doctype()) {
highlight( highlight(
token->start_position().line, token->start_position().line,