1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 18:35:07 +00:00

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.
This commit is contained in:
Sam Atkins 2022-03-30 11:48:54 +01:00 committed by Andreas Kling
parent 12a787ef8a
commit 239c36a19e
2 changed files with 32 additions and 18 deletions

View file

@ -1866,40 +1866,52 @@ Vector<DeclarationOrAtRule> Parser::consume_a_list_of_declarations(TokenStream<T
return list; 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> 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(); tokens.skip_whitespace();
// 3. If the next input token from input is an <EOF-token>, return a syntax error.
auto& token = tokens.peek_token(); auto& token = tokens.peek_token();
if (token.is(Token::Type::EndOfFile)) { if (token.is(Token::Type::EndOfFile)) {
return {}; return {};
} else if (token.is(Token::Type::AtKeyword)) { }
auto at_rule = consume_an_at_rule(m_token_stream); // 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.
rule = convert_to_rule(at_rule); else if (token.is(Token::Type::AtKeyword)) {
} else { 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); auto qualified_rule = consume_a_qualified_rule(tokens);
if (!qualified_rule) if (!qualified_rule)
return {}; 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(); tokens.skip_whitespace();
auto& maybe_eof = tokens.peek_token(); // 5. If the next input token from input is an <EOF-token>, return rule. Otherwise, return a syntax error.
if (maybe_eof.is(Token::Type::EndOfFile)) { if (tokens.peek_token().is(Token::Type::EndOfFile))
return rule; return rule;
}
return {}; 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) RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const& context, StringView css_text)
{ {
CSS::Parser parser(context, 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) Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const& context, StringView selector_text)

View file

@ -89,8 +89,6 @@ public:
Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8"); Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8");
~Parser() = default; ~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] // Used in @supports conditions. [CSS3-CONDITIONAL]
Optional<StyleProperty> parse_as_declaration(); Optional<StyleProperty> parse_as_declaration();
// For the contents of a style attribute, which parses text into the contents of a single style rule. // 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); NonnullRefPtr<CSSStyleSheet> parse_as_css_stylesheet(Optional<AK::URL> location);
RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&); RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
RefPtr<CSSRule> parse_as_css_rule();
enum class SelectorParsingMode { enum class SelectorParsingMode {
Standard, 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>. // "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> template<typename T>
NonnullRefPtrVector<StyleRule> parse_a_list_of_rules(TokenStream<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> template<typename T>
RefPtr<CSSRule> parse_a_rule(TokenStream<T>&); RefPtr<StyleRule> parse_a_rule(TokenStream<T>&);
template<typename T> template<typename T>
Optional<StyleProperty> parse_a_declaration(TokenStream<T>&); Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
template<typename T> template<typename T>