diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-03-29 14:13:39 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-30 18:43:07 +0200 |
commit | 85d8c652e9a576064b1198adde2eda047d2a59e8 (patch) | |
tree | 600ebbb886dc7271d6682d01a75488a0659b0795 | |
parent | fc3d51c59ec89d1d97f82e984824137e692a2c2f (diff) | |
download | serenity-85d8c652e9a576064b1198adde2eda047d2a59e8.zip |
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. :^)
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 11 |
2 files changed, 27 insertions, 15 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 05fa9ed723..9fc0f098c6 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/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) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 56bf66006a..20ac61d0ca 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; - // 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> |