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

LibWeb: Remove StringBuilder from HTMLToken::m_comment_or_character

This commit is contained in:
Gunnar Beutner 2021-05-23 09:31:40 +02:00 committed by Andreas Kling
parent 3aa202c432
commit c3ad8e9a52
5 changed files with 54 additions and 46 deletions

View file

@ -317,7 +317,7 @@ void HTMLDocumentParser::handle_initial(HTMLToken& token)
} }
if (token.is_comment()) { if (token.is_comment()) {
auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data.to_string())); auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data));
document().append_child(move(comment)); document().append_child(move(comment));
return; return;
} }
@ -347,7 +347,7 @@ void HTMLDocumentParser::handle_before_html(HTMLToken& token)
} }
if (token.is_comment()) { if (token.is_comment()) {
auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data.to_string())); auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data));
document().append_child(move(comment)); document().append_child(move(comment));
return; return;
} }
@ -520,7 +520,7 @@ AnythingElse:
void HTMLDocumentParser::insert_comment(HTMLToken& token) void HTMLDocumentParser::insert_comment(HTMLToken& token)
{ {
auto data = token.m_comment_or_character.data.to_string(); auto data = token.m_comment_or_character.data;
auto adjusted_insertion_location = find_appropriate_place_for_inserting_node(); auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
adjusted_insertion_location.parent->insert_before(adopt_ref(*new DOM::Comment(document(), data)), adjusted_insertion_location.insert_before_sibling); adjusted_insertion_location.parent->insert_before(adopt_ref(*new DOM::Comment(document(), data)), adjusted_insertion_location.insert_before_sibling);
} }
@ -832,7 +832,7 @@ void HTMLDocumentParser::handle_after_body(HTMLToken& token)
} }
if (token.is_comment()) { if (token.is_comment()) {
auto data = token.m_comment_or_character.data.to_string(); auto data = token.m_comment_or_character.data;
auto& insertion_location = m_stack_of_open_elements.first(); auto& insertion_location = m_stack_of_open_elements.first();
insertion_location.append_child(adopt_ref(*new DOM::Comment(document(), data))); insertion_location.append_child(adopt_ref(*new DOM::Comment(document(), data)));
return; return;
@ -870,7 +870,7 @@ void HTMLDocumentParser::handle_after_body(HTMLToken& token)
void HTMLDocumentParser::handle_after_after_body(HTMLToken& token) void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
{ {
if (token.is_comment()) { if (token.is_comment()) {
auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data.to_string())); auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data));
document().append_child(move(comment)); document().append_child(move(comment));
return; return;
} }
@ -2751,7 +2751,7 @@ void HTMLDocumentParser::handle_after_frameset(HTMLToken& token)
void HTMLDocumentParser::handle_after_after_frameset(HTMLToken& token) void HTMLDocumentParser::handle_after_after_frameset(HTMLToken& token)
{ {
if (token.is_comment()) { if (token.is_comment()) {
auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data.to_string())); auto comment = adopt_ref(*new DOM::Comment(document(), token.m_comment_or_character.data));
document().append_child(move(comment)); document().append_child(move(comment));
return; return;
} }

View file

@ -53,7 +53,7 @@ String HTMLToken::to_string() const
if (type() == HTMLToken::Type::Comment || type() == HTMLToken::Type::Character) { if (type() == HTMLToken::Type::Comment || type() == HTMLToken::Type::Character) {
builder.append(" { data: '"); builder.append(" { data: '");
builder.append(m_comment_or_character.data.to_string()); builder.append(m_comment_or_character.data);
builder.append("' }"); builder.append("' }");
} }

View file

@ -8,7 +8,6 @@
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <AK/Vector.h> #include <AK/Vector.h>
@ -34,7 +33,10 @@ public:
{ {
HTMLToken token; HTMLToken token;
token.m_type = Type::Character; token.m_type = Type::Character;
token.m_comment_or_character.data.append(code_point); StringBuilder builder;
// FIXME: This narrows code_point to char, should this be append_code_point() instead?
builder.append(code_point);
token.m_comment_or_character.data = builder.to_string();
return token; return token;
} }
@ -56,7 +58,7 @@ public:
u32 code_point() const u32 code_point() const
{ {
VERIFY(is_character()); VERIFY(is_character());
Utf8View view(m_comment_or_character.data.string_view()); Utf8View view(m_comment_or_character.data);
VERIFY(view.length() == 1); VERIFY(view.length() == 1);
return *view.begin(); return *view.begin();
} }
@ -209,7 +211,7 @@ private:
// Type::Comment // Type::Comment
// Type::Character // Type::Character
struct { struct {
StringBuilder data; String data;
} m_comment_or_character; } m_comment_or_character;
Position m_start_position; Position m_start_position;

View file

@ -74,17 +74,18 @@ namespace Web::HTML {
goto new_state; \ goto new_state; \
} while (0) } while (0)
#define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \ #define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \
do { \ do { \
for (auto code_point : m_temporary_buffer) { \ for (auto code_point : m_temporary_buffer) { \
if (consumed_as_part_of_an_attribute()) { \ if (consumed_as_part_of_an_attribute()) { \
m_current_builder.append_code_point(code_point); \ m_current_builder.append_code_point(code_point); \
} else { \ } else { \
create_new_token(HTMLToken::Type::Character); \ create_new_token(HTMLToken::Type::Character); \
m_current_token.m_comment_or_character.data.append_code_point(code_point); \ m_current_builder.append_code_point(code_point); \
m_queued_tokens.enqueue(m_current_token); \ m_current_token.m_comment_or_character.data = consume_current_builder(); \
} \ m_queued_tokens.enqueue(m_current_token); \
} \ } \
} \
} while (0) } while (0)
#define DONT_CONSUME_NEXT_INPUT_CHARACTER \ #define DONT_CONSUME_NEXT_INPUT_CHARACTER \
@ -139,12 +140,13 @@ namespace Web::HTML {
return m_queued_tokens.dequeue(); \ return m_queued_tokens.dequeue(); \
} while (0) } while (0)
#define EMIT_CHARACTER(code_point) \ #define EMIT_CHARACTER(code_point) \
do { \ do { \
create_new_token(HTMLToken::Type::Character); \ create_new_token(HTMLToken::Type::Character); \
m_current_token.m_comment_or_character.data.append_code_point(code_point); \ m_current_builder.append_code_point(code_point); \
m_queued_tokens.enqueue(m_current_token); \ m_current_token.m_comment_or_character.data = consume_current_builder(); \
return m_queued_tokens.dequeue(); \ m_queued_tokens.enqueue(m_current_token); \
return m_queued_tokens.dequeue(); \
} while (0) } while (0)
#define EMIT_CURRENT_CHARACTER \ #define EMIT_CURRENT_CHARACTER \
@ -402,6 +404,7 @@ _StartOfFunction:
{ {
ON('>') ON('>')
{ {
m_current_token.m_comment_or_character.data = consume_current_builder();
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
ON_EOF ON_EOF
@ -412,12 +415,12 @@ _StartOfFunction:
ON(0) ON(0)
{ {
log_parse_error(); log_parse_error();
m_current_token.m_comment_or_character.data.append_code_point(0xFFFD); m_current_builder.append_code_point(0xFFFD);
continue; continue;
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append_code_point(current_input_character.value()); m_current_builder.append_code_point(current_input_character.value());
continue; continue;
} }
} }
@ -1346,11 +1349,12 @@ _StartOfFunction:
{ {
ON('-') ON('-')
{ {
SWITCH_TO(CommentEnd); SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEnd);
} }
ON('>') ON('>')
{ {
log_parse_error(); log_parse_error();
consume_current_builder();
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
ON_EOF ON_EOF
@ -1361,7 +1365,7 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append('-'); m_current_builder.append('-');
RECONSUME_IN(Comment); RECONSUME_IN(Comment);
} }
} }
@ -1371,17 +1375,17 @@ _StartOfFunction:
{ {
ON('<') ON('<')
{ {
m_current_token.m_comment_or_character.data.append_code_point(current_input_character.value()); m_current_builder.append_code_point(current_input_character.value());
SWITCH_TO(CommentLessThanSign); SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSign);
} }
ON('-') ON('-')
{ {
SWITCH_TO(CommentEndDash); SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEndDash);
} }
ON(0) ON(0)
{ {
log_parse_error(); log_parse_error();
m_current_token.m_comment_or_character.data.append_code_point(0xFFFD); m_current_builder.append_code_point(0xFFFD);
continue; continue;
} }
ON_EOF ON_EOF
@ -1392,7 +1396,7 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append_code_point(current_input_character.value()); m_current_builder.append_code_point(current_input_character.value());
continue; continue;
} }
} }
@ -1402,6 +1406,7 @@ _StartOfFunction:
{ {
ON('>') ON('>')
{ {
m_current_token.m_comment_or_character.data = consume_current_builder();
SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data); SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
} }
ON('!') ON('!')
@ -1410,7 +1415,7 @@ _StartOfFunction:
} }
ON('-') ON('-')
{ {
m_current_token.m_comment_or_character.data.append('-'); m_current_builder.append('-');
continue; continue;
} }
ON_EOF ON_EOF
@ -1421,7 +1426,7 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append('-'); m_current_builder.append('-');
RECONSUME_IN(Comment); RECONSUME_IN(Comment);
} }
} }
@ -1431,7 +1436,7 @@ _StartOfFunction:
{ {
ON('-') ON('-')
{ {
m_current_token.m_comment_or_character.data.append("--!"); m_current_builder.append("--!");
SWITCH_TO(CommentEndDash); SWITCH_TO(CommentEndDash);
} }
ON('>') ON('>')
@ -1447,7 +1452,7 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append("--!"); m_current_builder.append("--!");
RECONSUME_IN(Comment); RECONSUME_IN(Comment);
} }
} }
@ -1457,7 +1462,7 @@ _StartOfFunction:
{ {
ON('-') ON('-')
{ {
SWITCH_TO(CommentEnd); SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEnd);
} }
ON_EOF ON_EOF
{ {
@ -1467,7 +1472,7 @@ _StartOfFunction:
} }
ANYTHING_ELSE ANYTHING_ELSE
{ {
m_current_token.m_comment_or_character.data.append('-'); m_current_builder.append('-');
RECONSUME_IN(Comment); RECONSUME_IN(Comment);
} }
} }
@ -1477,12 +1482,12 @@ _StartOfFunction:
{ {
ON('!') ON('!')
{ {
m_current_token.m_comment_or_character.data.append_code_point(current_input_character.value()); m_current_builder.append_code_point(current_input_character.value());
SWITCH_TO(CommentLessThanSignBang); SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSignBang);
} }
ON('<') ON('<')
{ {
m_current_token.m_comment_or_character.data.append_code_point(current_input_character.value()); m_current_builder.append_code_point(current_input_character.value());
continue; continue;
} }
ANYTHING_ELSE ANYTHING_ELSE

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/Queue.h> #include <AK/Queue.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>