From 3d6eb366644c2e43c028942bd2cbe6aeca21a6b0 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 7 Dec 2023 12:34:51 +0000 Subject: [PATCH] LibWeb: Parse content property using TokenStream --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 17 +++++++++++------ Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 983d0af91e8..8f862bd9476 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3430,10 +3430,12 @@ RefPtr Parser::parse_single_shadow_value(TokenStream return ShadowStyleValue::create(color.release_value(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value()); } -RefPtr Parser::parse_content_value(Vector const& component_values) +RefPtr Parser::parse_content_value(TokenStream& tokens) { // FIXME: `content` accepts several kinds of function() type, which we don't handle in property_accepts_value() yet. + auto transaction = tokens.begin_transaction(); + auto is_single_value_identifier = [](ValueID identifier) -> bool { switch (identifier) { case ValueID::None: @@ -3444,10 +3446,13 @@ RefPtr Parser::parse_content_value(Vector const& com } }; - if (component_values.size() == 1) { - if (auto identifier = parse_identifier_value(component_values.first())) { - if (is_single_value_identifier(identifier->to_identifier())) + if (tokens.remaining_token_count() == 1) { + if (auto identifier = parse_identifier_value(tokens.peek_token())) { + if (is_single_value_identifier(identifier->to_identifier())) { + (void)tokens.next_token(); + transaction.commit(); return identifier; + } } } @@ -3455,7 +3460,6 @@ RefPtr Parser::parse_content_value(Vector const& com StyleValueVector alt_text_values; bool in_alt_text = false; - auto tokens = TokenStream { component_values }; while (tokens.has_next_token()) { auto& next = tokens.peek_token(); if (next.is_delim('/')) { @@ -3490,6 +3494,7 @@ RefPtr Parser::parse_content_value(Vector const& com if (!alt_text_values.is_empty()) alt_text = StyleValueList::create(move(alt_text_values), StyleValueList::Separator::Space); + transaction.commit(); return ContentStyleValue::create(StyleValueList::create(move(content_values), StyleValueList::Separator::Space), move(alt_text)); } @@ -5768,7 +5773,7 @@ Parser::ParseErrorOr> Parser::parse_css_value(Property return parsed_value.release_nonnull(); return ParseError::SyntaxError; case PropertyID::Content: - if (auto parsed_value = parse_content_value(component_values)) + if (auto parsed_value = parse_content_value(tokens); parsed_value && !tokens.has_next_token()) return parsed_value.release_nonnull(); return ParseError::SyntaxError; case PropertyID::Display: diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index c08467c577d..925421f5ba6 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -233,7 +233,7 @@ private: RefPtr parse_border_value(PropertyID, Vector const&); RefPtr parse_border_radius_value(TokenStream&); RefPtr parse_border_radius_shorthand_value(TokenStream&); - RefPtr parse_content_value(Vector const&); + RefPtr parse_content_value(TokenStream&); RefPtr parse_display_value(Vector const&); RefPtr parse_flex_value(Vector const&); RefPtr parse_flex_flow_value(Vector const&);