From a4f805682874a7a6b60d75e78494921c23dc09dd Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 30 Mar 2022 12:56:15 +0100 Subject: [PATCH] LibWeb: Spec-comment `parse_a_comma_separated_list_of_component_values` The code had to change a bit to match. Previously, we appended an empty sub-list immediately, but now we append it at the end. The difference is that if there are no tokens, we now correctly return an empty list-of-lists, instead of a list containing an empty list. --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 41 +++++++++++-------- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 2 - 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index c5a8bd51b4..0fa1bbbab8 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2032,33 +2032,42 @@ Vector Parser::parse_a_list_of_component_values(TokenSt return component_values; } -Vector> Parser::parse_as_comma_separated_list_of_component_values() -{ - return parse_a_comma_separated_list_of_component_values(m_token_stream); -} - +// 5.3.11. Parse a comma-separated list of component values +// https://www.w3.org/TR/css-syntax-3/#parse-comma-separated-list-of-component-values template Vector> Parser::parse_a_comma_separated_list_of_component_values(TokenStream& tokens) { - Vector> lists; - lists.append({}); + // To parse a comma-separated list of component values from input: + // 1. Normalize input, and set input to the result. + // Note: This is done when initializing the Parser. + + // 2. Let list of cvls be an initially empty list of component value lists. + Vector> list_of_component_value_lists; + + // 3. Repeatedly consume a component value from input until an or is returned, + // appending the returned values (except the final or ) into a list. + // Append the list to list of cvls. + // If it was a that was returned, repeat this step. + Vector current_list; for (;;) { - auto& next = tokens.next_token(); + auto component_value = consume_a_component_value(tokens); - if (next.is(Token::Type::Comma)) { - lists.append({}); - continue; - } else if (next.is(Token::Type::EndOfFile)) { + if (component_value.is(Token::Type::EndOfFile)) { + list_of_component_value_lists.append(move(current_list)); break; } + if (component_value.is(Token::Type::Comma)) { + list_of_component_value_lists.append(move(current_list)); + current_list = {}; + continue; + } - tokens.reconsume_current_input_token(); - auto component_value = consume_a_component_value(tokens); - lists.last().append(component_value); + current_list.append(component_value); } - return lists; + // 4. Return list of cvls. + return list_of_component_value_lists; } RefPtr Parser::parse_as_style_attribute(DOM::Element& element) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 116f3d24d7..c7b3ce853c 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -89,8 +89,6 @@ public: Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8"); ~Parser() = default; - Vector> parse_as_comma_separated_list_of_component_values(); - NonnullRefPtr parse_as_css_stylesheet(Optional location); RefPtr parse_as_style_attribute(DOM::Element&); RefPtr parse_as_css_rule();