From d72ce7b989362801bbc236e391c68902d1dc189f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 14 Jul 2021 16:20:06 +0100 Subject: [PATCH] LibWeb: Generate a ValueListStyleValue when parsing CSS values We skip whitespace tokens while doing this. As far as I can tell, whitespace is not useful once we get to this point, and it legally may or may not appear between any two tokens. By not including it in the ValueListStyleValue, we make the "if it has 3 parts"-type checks a lot more straightforward. --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 80 ++++++++++++------- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 2 + 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 679d28cfc6..beb13f4560 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1271,10 +1271,9 @@ Optional Parser::try_parse_float(StringView string) return is_negative ? -value : value; } -RefPtr Parser::parse_css_value(PropertyID property_id, TokenStream& tokens) +RefPtr Parser::parse_single_css_value(PropertyID property_id, StyleComponentValueRule const& component_value) { - dbgln_if(CSS_PARSER_TRACE, "Parser::parse_css_value"); - + dbgln_if(CSS_PARSER_TRACE, "Parser::parse_single_css_value '{}'", component_value.to_debug_string()); // FIXME: This is mostly copied from the old, deprecated parser. It is probably not to spec. auto takes_integer_value = [](PropertyID property_id) -> bool { @@ -1287,11 +1286,9 @@ RefPtr Parser::parse_css_value(PropertyID property_id, TokenStream numeric_value; - auto token = tokens.next_token(); - - if (token.is(Token::Type::Dimension)) { - auto length_string = token.token().m_value.string_view(); - auto unit_string = token.token().m_unit.string_view(); + if (component_value.is(Token::Type::Dimension)) { + auto length_string = component_value.token().m_value.string_view(); + auto unit_string = component_value.token().m_unit.string_view(); if (unit_string.equals_ignoring_case("%")) { type = Length::Type::Percentage; @@ -1328,8 +1325,8 @@ RefPtr Parser::parse_css_value(PropertyID property_id, TokenStream Parser::parse_css_value(PropertyID property_id, TokenStream Parser::parse_css_value(PropertyID property_id, TokenStream Parser::parse_css_value(PropertyID property_id, TokenStream Optional { - if (token.is(Token::Type::Ident) && token.token().ident().equals_ignoring_case("transparent")) + if (component_value.is(Token::Type::Ident) && component_value.token().ident().equals_ignoring_case("transparent")) return Color::from_rgba(0x00000000); // FIXME: Handle all the different color notations. // https://www.w3.org/TR/css-color-3/ // Right now, this uses non-CSS-specific parsing, and assumes the whole color value is one token, // which is isn't if it's a function-style syntax. - auto color = Color::from_string(token.token().m_value.to_string().to_lowercase()); + auto color = Color::from_string(component_value.token().m_value.to_string().to_lowercase()); if (color.has_value()) return color; @@ -1415,12 +1408,41 @@ RefPtr Parser::parse_css_value(PropertyID property_id, TokenStream Parser::parse_css_value(PropertyID property_id, TokenStream& tokens) +{ + dbgln_if(CSS_PARSER_TRACE, "Parser::parse_css_value"); + + Vector component_values; + + while (tokens.has_next_token()) { + auto& token = tokens.next_token(); + + if (token.is(Token::Type::Semicolon)) { + tokens.reconsume_current_input_token(); + break; + } + + if (token.is(Token::Type::Whitespace)) + continue; + + component_values.append(token); + } + + if (component_values.is_empty()) + return {}; + + if (component_values.size() == 1) + return parse_single_css_value(property_id, component_values.first()); + + return ValueListStyleValue::create(move(component_values)); +} + Optional Parser::parse_nth_child_pattern(TokenStream& values) { dbgln_if(CSS_PARSER_TRACE, "Parser::parse_nth_child_pattern"); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 3e46103f51..6e8b82e927 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -177,6 +177,8 @@ private: static Optional try_parse_float(StringView string); + RefPtr parse_single_css_value(PropertyID, StyleComponentValueRule const&); + ParsingContext m_context; Tokenizer m_tokenizer;