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

LibWeb: User getter+setter for HTMLToken tag name and self-closing flag

This commit is contained in:
Max Wipfli 2021-07-14 23:37:48 +02:00 committed by Ali Mohammad Pur
parent 1aeafcc58b
commit 15d8635afc
4 changed files with 34 additions and 22 deletions

View file

@ -1570,7 +1570,7 @@ void HTMLDocumentParser::handle_in_body(HTMLToken& token)
if (token.is_start_tag() && token.tag_name() == HTML::TagNames::image) { if (token.is_start_tag() && token.tag_name() == HTML::TagNames::image) {
// Parse error. Change the token's tag name to HTML::TagNames::img and reprocess it. (Don't ask.) // Parse error. Change the token's tag name to HTML::TagNames::img and reprocess it. (Don't ask.)
log_parse_error(); log_parse_error();
token.m_tag.tag_name = "img"; token.set_tag_name("img");
process_using_the_rules_for(m_insertion_mode, token); process_using_the_rules_for(m_insertion_mode, token);
return; return;
} }

View file

@ -40,7 +40,7 @@ String HTMLToken::to_string() const
if (type() == HTMLToken::Type::StartTag || type() == HTMLToken::Type::EndTag) { if (type() == HTMLToken::Type::StartTag || type() == HTMLToken::Type::EndTag) {
builder.append(" { name: '"); builder.append(" { name: '");
builder.append(m_tag.tag_name); builder.append(tag_name());
builder.append("', { "); builder.append("', { ");
for (auto& attribute : m_tag.attributes) { for (auto& attribute : m_tag.attributes) {
builder.append(attribute.local_name); builder.append(attribute.local_name);

View file

@ -58,7 +58,7 @@ public:
{ {
HTMLToken token; HTMLToken token;
token.m_type = Type::StartTag; token.m_type = Type::StartTag;
token.m_tag.tag_name = tag_name; token.set_tag_name(tag_name);
return token; return token;
} }
@ -114,18 +114,30 @@ public:
m_comment_or_character.data = move(comment); m_comment_or_character.data = move(comment);
} }
String tag_name() const String const& tag_name() const
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
return m_tag.tag_name; return m_tag.tag_name;
} }
void set_tag_name(String name)
{
VERIFY(is_start_tag() || is_end_tag());
m_tag.tag_name = move(name);
}
bool is_self_closing() const bool is_self_closing() const
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
return m_tag.self_closing; return m_tag.self_closing;
} }
void set_self_closing(bool self_closing)
{
VERIFY(is_start_tag() || is_end_tag());
m_tag.self_closing = self_closing;
}
bool has_acknowledged_self_closing_flag() const bool has_acknowledged_self_closing_flag() const
{ {
VERIFY(is_self_closing()); VERIFY(is_self_closing());
@ -156,8 +168,8 @@ public:
void adjust_tag_name(FlyString const& old_name, FlyString const& new_name) void adjust_tag_name(FlyString const& old_name, FlyString const& new_name)
{ {
VERIFY(is_start_tag() || is_end_tag()); VERIFY(is_start_tag() || is_end_tag());
if (old_name == m_tag.tag_name) if (old_name == tag_name())
m_tag.tag_name = new_name; set_tag_name(new_name);
} }
void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name) void adjust_attribute_name(FlyString const& old_name, FlyString const& new_name)

View file

@ -305,19 +305,19 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
m_current_token.m_end_position = nth_last_position(1); m_current_token.m_end_position = nth_last_position(1);
SWITCH_TO(BeforeAttributeName); SWITCH_TO(BeforeAttributeName);
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
m_current_token.m_end_position = nth_last_position(0); m_current_token.m_end_position = nth_last_position(0);
SWITCH_TO(SelfClosingStartTag); SWITCH_TO(SelfClosingStartTag);
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
m_current_token.m_end_position = nth_last_position(1); m_current_token.m_end_position = nth_last_position(1);
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
@ -1031,7 +1031,7 @@ _StartOfFunction:
{ {
ON('>') ON('>')
{ {
m_current_token.m_tag.self_closing = true; m_current_token.set_self_closing(true);
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
ON_EOF ON_EOF
@ -1858,7 +1858,7 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -1870,7 +1870,7 @@ _StartOfFunction:
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -1882,7 +1882,7 @@ _StartOfFunction:
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -1973,7 +1973,7 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -1985,7 +1985,7 @@ _StartOfFunction:
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -1997,7 +1997,7 @@ _StartOfFunction:
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (!current_end_tag_token_is_appropriate()) { if (!current_end_tag_token_is_appropriate()) {
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
m_queued_tokens.enqueue(HTMLToken::make_character('/')); m_queued_tokens.enqueue(HTMLToken::make_character('/'));
@ -2188,7 +2188,7 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO(BeforeAttributeName); SWITCH_TO(BeforeAttributeName);
@ -2203,7 +2203,7 @@ _StartOfFunction:
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO(SelfClosingStartTag); SWITCH_TO(SelfClosingStartTag);
@ -2218,7 +2218,7 @@ _StartOfFunction:
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
@ -2524,7 +2524,7 @@ _StartOfFunction:
{ {
ON_WHITESPACE ON_WHITESPACE
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO(BeforeAttributeName); SWITCH_TO(BeforeAttributeName);
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
@ -2537,7 +2537,7 @@ _StartOfFunction:
} }
ON('/') ON('/')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO(SelfClosingStartTag); SWITCH_TO(SelfClosingStartTag);
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));
@ -2550,7 +2550,7 @@ _StartOfFunction:
} }
ON('>') ON('>')
{ {
m_current_token.m_tag.tag_name = consume_current_builder(); m_current_token.set_tag_name(consume_current_builder());
if (current_end_tag_token_is_appropriate()) if (current_end_tag_token_is_appropriate())
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
m_queued_tokens.enqueue(HTMLToken::make_character('<')); m_queued_tokens.enqueue(HTMLToken::make_character('<'));