Bläddra i källkod

LibWeb: Spec-comment `parse_a_rule()`

We now correctly call convert_to_rule() outside of this function.

As before, I've renamed `parse_as_rule()` -> `parse_as_css_rule()` to
match the free function that calls it.
Sam Atkins 3 år sedan
förälder
incheckning
239c36a19e

+ 27 - 15
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -1866,40 +1866,52 @@ Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations(TokenStream<T
     return list;
 }
 
-RefPtr<CSSRule> Parser::parse_as_rule()
+RefPtr<CSSRule> Parser::parse_as_css_rule()
 {
-    return parse_a_rule(m_token_stream);
+    auto maybe_rule = parse_a_rule(m_token_stream);
+    if (maybe_rule)
+        return convert_to_rule(maybe_rule.release_nonnull());
+    return {};
 }
 
+// 5.3.5. Parse a rule
+// https://www.w3.org/TR/css-syntax-3/#parse-rule
 template<typename T>
-RefPtr<CSSRule> Parser::parse_a_rule(TokenStream<T>& tokens)
+RefPtr<StyleRule> Parser::parse_a_rule(TokenStream<T>& tokens)
 {
-    RefPtr<CSSRule> rule;
+    // To parse a rule from input:
+    RefPtr<StyleRule> rule;
+
+    // 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 from input.
     tokens.skip_whitespace();
 
+    // 3. If the next input token from input is an <EOF-token>, return a syntax error.
     auto& token = tokens.peek_token();
-
     if (token.is(Token::Type::EndOfFile)) {
         return {};
-    } else if (token.is(Token::Type::AtKeyword)) {
-        auto at_rule = consume_an_at_rule(m_token_stream);
-        rule = convert_to_rule(at_rule);
-    } else {
+    }
+    // Otherwise, if the next input token from input is an <at-keyword-token>, consume an at-rule from input, and let rule be the return value.
+    else if (token.is(Token::Type::AtKeyword)) {
+        rule = consume_an_at_rule(m_token_stream);
+    }
+    // Otherwise, consume a qualified rule from input and let rule be the return value. If nothing was returned, return a syntax error.
+    else {
         auto qualified_rule = consume_a_qualified_rule(tokens);
         if (!qualified_rule)
             return {};
 
-        rule = convert_to_rule(*qualified_rule);
+        rule = qualified_rule;
     }
 
+    // 4. While the next input token from input is a <whitespace-token>, consume the next input token from input.
     tokens.skip_whitespace();
 
-    auto& maybe_eof = tokens.peek_token();
-    if (maybe_eof.is(Token::Type::EndOfFile)) {
+    // 5. If the next input token from input is an <EOF-token>, return rule. Otherwise, return a syntax error.
+    if (tokens.peek_token().is(Token::Type::EndOfFile))
         return rule;
-    }
-
     return {};
 }
 
@@ -5268,7 +5280,7 @@ RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const& context, Stri
 RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const& context, StringView css_text)
 {
     CSS::Parser parser(context, css_text);
-    return parser.parse_as_rule();
+    return parser.parse_as_css_rule();
 }
 
 Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const& context, StringView selector_text)

+ 5 - 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;
 
-    // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
-    RefPtr<CSSRule> parse_as_rule();
     // 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.
@@ -103,6 +101,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();
 
     enum class SelectorParsingMode {
         Standard,
@@ -142,8 +141,11 @@ private:
     // "Parse a list of rules" is intended for the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
     template<typename T>
     NonnullRefPtrVector<StyleRule> parse_a_list_of_rules(TokenStream<T>&);
+
+    // "Parse a rule" is intended for use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
     template<typename T>
-    RefPtr<CSSRule> parse_a_rule(TokenStream<T>&);
+    RefPtr<StyleRule> parse_a_rule(TokenStream<T>&);
+
     template<typename T>
     Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
     template<typename T>