diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 78a50680eb..7fbc91e414 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1866,40 +1866,52 @@ Vector Parser::consume_a_list_of_declarations(TokenStream Parser::parse_as_rule() +RefPtr 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 -RefPtr Parser::parse_a_rule(TokenStream& tokens) +RefPtr Parser::parse_a_rule(TokenStream& tokens) { - RefPtr rule; + // To parse a rule from input: + RefPtr 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 , consume the next input token from input. tokens.skip_whitespace(); + // 3. If the next input token from input is an , 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 , 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 , 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 , return rule. Otherwise, return a syntax error. + if (tokens.peek_token().is(Token::Type::EndOfFile)) return rule; - } - return {}; } @@ -5268,7 +5280,7 @@ RefPtr parse_css_value(CSS::ParsingContext const& context, Stri RefPtr 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 parse_selector(CSS::ParsingContext const& context, StringView selector_text) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index cea43aae45..a3af04074f 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; - // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule. - RefPtr parse_as_rule(); // Used in @supports conditions. [CSS3-CONDITIONAL] Optional 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 parse_as_css_stylesheet(Optional location); RefPtr parse_as_style_attribute(DOM::Element&); + RefPtr 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 and . template NonnullRefPtrVector parse_a_list_of_rules(TokenStream&); + + // "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 - RefPtr parse_a_rule(TokenStream&); + RefPtr parse_a_rule(TokenStream&); + template Optional parse_a_declaration(TokenStream&); template