瀏覽代碼

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.
Sam Atkins 3 年之前
父節點
當前提交
a4f8056828
共有 2 個文件被更改,包括 25 次插入18 次删除
  1. 25 16
      Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
  2. 0 2
      Userland/Libraries/LibWeb/CSS/Parser/Parser.h

+ 25 - 16
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -2032,33 +2032,42 @@ Vector<StyleComponentValueRule> Parser::parse_a_list_of_component_values(TokenSt
     return component_values;
 }
 
-Vector<Vector<StyleComponentValueRule>> 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<typename T>
 Vector<Vector<StyleComponentValueRule>> Parser::parse_a_comma_separated_list_of_component_values(TokenStream<T>& tokens)
 {
-    Vector<Vector<StyleComponentValueRule>> 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<Vector<StyleComponentValueRule>> list_of_component_value_lists;
 
+    // 3. Repeatedly consume a component value from input until an <EOF-token> or <comma-token> is returned,
+    //    appending the returned values (except the final <EOF-token> or <comma-token>) into a list.
+    //    Append the list to list of cvls.
+    //    If it was a <comma-token> that was returned, repeat this step.
+    Vector<StyleComponentValueRule> 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<ElementInlineCSSStyleDeclaration> Parser::parse_as_style_attribute(DOM::Element& element)

+ 0 - 2
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<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
-
     NonnullRefPtr<CSSStyleSheet> parse_as_css_stylesheet(Optional<AK::URL> location);
     RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
     RefPtr<CSSRule> parse_as_css_rule();