From 0e00d9a67d339805e8b329f53a98673f8bf06c97 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 7 Dec 2023 15:21:17 +0000 Subject: [PATCH] LibWeb: Parse background property using TokenStream --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 13 ++++++++----- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index ff3f915ac66..02c831198d5 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2731,8 +2731,10 @@ RefPtr Parser::parse_aspect_ratio_value(TokenStream& return nullptr; } -RefPtr Parser::parse_background_value(Vector const& component_values) +RefPtr Parser::parse_background_value(TokenStream& tokens) { + auto transaction = tokens.begin_transaction(); + auto make_background_shorthand = [&](auto background_color, auto background_image, auto background_position, auto background_size, auto background_repeat, auto background_attachment, auto background_origin, auto background_clip) { return ShorthandStyleValue::create(PropertyID::Background, { PropertyID::BackgroundColor, PropertyID::BackgroundImage, PropertyID::BackgroundPosition, PropertyID::BackgroundSize, PropertyID::BackgroundRepeat, PropertyID::BackgroundAttachment, PropertyID::BackgroundOrigin, PropertyID::BackgroundClip }, @@ -2823,7 +2825,6 @@ RefPtr Parser::parse_background_value(Vector const& remaining_layer_properties.unchecked_append(PropertyID::BackgroundRepeat); }; - auto tokens = TokenStream { component_values }; while (tokens.has_next_token()) { if (tokens.peek_token().is(Token::Type::Comma)) { has_multiple_layers = true; @@ -2875,11 +2876,11 @@ RefPtr Parser::parse_background_value(Vector const& background_position = value.release_nonnull(); // Attempt to parse `/ ` - auto transaction = tokens.begin_transaction(); + auto background_size_transaction = tokens.begin_transaction(); auto& maybe_slash = tokens.next_token(); if (maybe_slash.is_delim('/')) { if (auto maybe_background_size = parse_single_background_size_value(tokens)) { - transaction.commit(); + background_size_transaction.commit(); background_size = maybe_background_size.release_nonnull(); continue; } @@ -2913,6 +2914,7 @@ RefPtr Parser::parse_background_value(Vector const& if (!background_color) background_color = initial_background_color; + transaction.commit(); return make_background_shorthand( background_color.release_nonnull(), StyleValueList::create(move(background_images), StyleValueList::Separator::Comma), @@ -2944,6 +2946,7 @@ RefPtr Parser::parse_background_value(Vector const& background_clip = background_origin; } + transaction.commit(); return make_background_shorthand( background_color.release_nonnull(), background_image.release_nonnull(), @@ -5744,7 +5747,7 @@ Parser::ParseErrorOr> Parser::parse_css_value(Property return parsed_value.release_nonnull(); return ParseError::SyntaxError; case PropertyID::Background: - if (auto parsed_value = parse_background_value(component_values)) + if (auto parsed_value = parse_background_value(tokens); parsed_value && !tokens.has_next_token()) return parsed_value.release_nonnull(); return ParseError::SyntaxError; case PropertyID::BackgroundAttachment: diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 27115b58b14..4e1fa91bb1f 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -227,7 +227,7 @@ private: RefPtr parse_simple_comma_separated_value_list(PropertyID, TokenStream&); RefPtr parse_aspect_ratio_value(TokenStream&); - RefPtr parse_background_value(Vector const&); + RefPtr parse_background_value(TokenStream&); RefPtr parse_single_background_position_x_or_y_value(TokenStream&, PropertyID); RefPtr parse_single_background_repeat_value(TokenStream&); RefPtr parse_single_background_size_value(TokenStream&);