Просмотр исходного кода

LibWeb: Implement and use "parse a CSS stylesheet" algorithm

`parse_a_stylesheet()` should not do any conversion on its rules. This
change corrects that. There are other places where we get this wrong,
but one thing at a time. :^)
Sam Atkins 3 лет назад
Родитель
Сommit
85d8c652e9

+ 19 - 12
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -160,15 +160,10 @@ Parser::Parser(ParsingContext const& context, StringView input, String const& en
 {
 }
 
-NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet(Optional<AK::URL> location)
-{
-    return parse_a_stylesheet(m_token_stream, location);
-}
-
 // 5.3.3. Parse a stylesheet
 // https://www.w3.org/TR/css-syntax-3/#parse-stylesheet
 template<typename T>
-NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens, Optional<AK::URL> location)
+Parser::ParsedStyleSheet Parser::parse_a_stylesheet(TokenStream<T>& tokens, Optional<AK::URL> location)
 {
     // To parse a stylesheet from an input given an optional url location:
 
@@ -177,19 +172,31 @@ NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens,
     // NOTE: These are done automatically when creating the Parser.
 
     // 3. Create a new stylesheet, with its location set to location (or null, if location was not passed).
-    // NOTE: We create the stylesheet at the end.
+    ParsedStyleSheet style_sheet;
+    style_sheet.location = location;
 
     // 4. Consume a list of rules from input, with the top-level flag set, and set the stylesheet’s value to the result.
-    auto parser_rules = consume_a_list_of_rules(tokens, TopLevel::Yes);
-    NonnullRefPtrVector<CSSRule> rules;
+    style_sheet.rules = consume_a_list_of_rules(tokens, TopLevel::Yes);
 
-    for (auto& raw_rule : parser_rules) {
+    // 5. Return the stylesheet.
+    return style_sheet;
+}
+
+// https://www.w3.org/TR/css-syntax-3/#parse-a-css-stylesheet
+NonnullRefPtr<CSSStyleSheet> Parser::parse_as_css_stylesheet(Optional<AK::URL> location)
+{
+    // To parse a CSS stylesheet, first parse a stylesheet.
+    auto style_sheet = parse_a_stylesheet(m_token_stream, {});
+
+    // Interpret all of the resulting top-level qualified rules as style rules, defined below.
+    NonnullRefPtrVector<CSSRule> rules;
+    for (auto& raw_rule : style_sheet.rules) {
         auto rule = convert_to_rule(raw_rule);
+        // If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, it’s a parse error. Discard that rule.
         if (rule)
             rules.append(*rule);
     }
 
-    // 5. Return the stylesheet.
     return CSSStyleSheet::create(move(rules), move(location));
 }
 
@@ -5242,7 +5249,7 @@ RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const& conte
     if (css.is_empty())
         return CSS::CSSStyleSheet::create({}, location);
     CSS::Parser parser(context, css);
-    return parser.parse_as_stylesheet(location);
+    return parser.parse_as_css_stylesheet(location);
 }
 
 RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element)

+ 8 - 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;
 
-    // The normal parser entry point, for parsing stylesheets.
-    NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet(Optional<AK::URL> location);
     // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
     NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
     // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
@@ -105,6 +103,7 @@ public:
     Vector<StyleComponentValueRule> parse_as_list_of_component_values();
     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&);
 
     enum class SelectorParsingMode {
@@ -134,8 +133,14 @@ private:
         SyntaxError,
     };
 
+    // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets.
+    struct ParsedStyleSheet {
+        Optional<AK::URL> location;
+        NonnullRefPtrVector<StyleRule> rules;
+    };
     template<typename T>
-    NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
+    ParsedStyleSheet parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
+
     template<typename T>
     NonnullRefPtrVector<CSSRule> parse_a_list_of_rules(TokenStream<T>&);
     template<typename T>