Browse Source

LibWeb: Bring `parse_a_declaration()` to spec and add comments

User code now calls `parse_as_supports_condition()` which actually does
the conversion to a StyleProperty.
Sam Atkins 3 years ago
parent
commit
2aac9f9258

+ 18 - 8
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -1932,26 +1932,36 @@ NonnullRefPtrVector<StyleRule> Parser::parse_a_list_of_rules(TokenStream<T>& tok
     return list_of_rules;
 }
 
-Optional<StyleProperty> Parser::parse_as_declaration()
+Optional<StyleProperty> Parser::parse_as_supports_condition()
 {
-    return parse_a_declaration(m_token_stream);
+    auto maybe_declaration = parse_a_declaration(m_token_stream);
+    if (maybe_declaration.has_value())
+        return convert_to_style_property(maybe_declaration.release_value());
+    return {};
 }
 
+// 5.3.6. Parse a declaration
+// https://www.w3.org/TR/css-syntax-3/#parse-a-declaration
 template<typename T>
-Optional<StyleProperty> Parser::parse_a_declaration(TokenStream<T>& tokens)
+Optional<StyleDeclarationRule> Parser::parse_a_declaration(TokenStream<T>& tokens)
 {
+    // To parse a declaration from input:
+
+    // 1. Normalize input, and set input to the result.
+    // Note: This is done when initializing the Parser.
+
+    // 2. While the next input token from input is a <whitespace-token>, consume the next input token.
     tokens.skip_whitespace();
 
+    // 3. If the next input token from input is not an <ident-token>, return a syntax error.
     auto& token = tokens.peek_token();
-
     if (!token.is(Token::Type::Ident)) {
         return {};
     }
 
-    auto declaration = consume_a_declaration(tokens);
-    if (declaration.has_value())
-        return convert_to_style_property(declaration.value());
-
+    // 4. Consume a declaration from input. If anything was returned, return it. Otherwise, return a syntax error.
+    if (auto declaration = consume_a_declaration(tokens); declaration.has_value())
+        return declaration.release_value();
     return {};
 }
 

+ 3 - 3
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -89,8 +89,6 @@ public:
     Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8");
     ~Parser() = default;
 
-    // Used in @supports conditions. [CSS3-CONDITIONAL]
-    Optional<StyleProperty> parse_as_declaration();
     // For the contents of a style attribute, which parses text into the contents of a single style rule.
     Vector<DeclarationOrAtRule> parse_as_list_of_declarations();
     // For things that need to consume a single value, like the parsing rules for attr().
@@ -102,6 +100,7 @@ public:
     NonnullRefPtr<CSSStyleSheet> parse_as_css_stylesheet(Optional<AK::URL> location);
     RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
     RefPtr<CSSRule> parse_as_css_rule();
+    Optional<StyleProperty> parse_as_supports_condition();
 
     enum class SelectorParsingMode {
         Standard,
@@ -146,8 +145,9 @@ private:
     template<typename T>
     RefPtr<StyleRule> parse_a_rule(TokenStream<T>&);
 
+    // "Parse a declaration" is used in @supports conditions. [CSS3-CONDITIONAL]
     template<typename T>
-    Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
+    Optional<StyleDeclarationRule> parse_a_declaration(TokenStream<T>&);
     template<typename T>
     Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&);
     template<typename T>

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Supports.cpp

@@ -52,7 +52,7 @@ bool Supports::InParens::evaluate() const
 
 bool Supports::Declaration::evaluate() const
 {
-    auto style_property = Parser({}, declaration).parse_as_declaration();
+    auto style_property = Parser({}, declaration).parse_as_supports_condition();
     return style_property.has_value();
 }