1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

LibWeb: Spec-comment parse_a_component_value()

This commit is contained in:
Sam Atkins 2022-03-30 12:32:42 +01:00 committed by Andreas Kling
parent bcf4254331
commit 34b3c09462
2 changed files with 16 additions and 15 deletions

View file

@ -1979,31 +1979,32 @@ Vector<DeclarationOrAtRule> Parser::parse_a_list_of_declarations(TokenStream<T>&
return consume_a_list_of_declarations(tokens);
}
Optional<StyleComponentValueRule> Parser::parse_as_component_value()
{
return parse_a_component_value(m_token_stream);
}
// 5.3.9. Parse a component value
// https://www.w3.org/TR/css-syntax-3/#parse-component-value
template<typename T>
Optional<StyleComponentValueRule> Parser::parse_a_component_value(TokenStream<T>& tokens)
{
// To parse a component value from input:
// 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();
auto& token = tokens.peek_token();
if (token.is(Token::Type::EndOfFile)) {
// 3. If the next input token from input is an <EOF-token>, return a syntax error.
if (tokens.peek_token().is(Token::Type::EndOfFile))
return {};
}
// 4. Consume a component value from input and let value be the return value.
auto value = consume_a_component_value(tokens);
// 5. While the next input token from input is a <whitespace-token>, consume the next input token.
tokens.skip_whitespace();
auto& maybe_eof = tokens.peek_token();
if (maybe_eof.is(Token::Type::EndOfFile)) {
// 6. If the next input token from input is an <EOF-token>, return value. Otherwise, return a syntax error.
if (tokens.peek_token().is(Token::Type::EndOfFile))
return value;
}
return {};
}

View file

@ -89,8 +89,6 @@ public:
Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8");
~Parser() = default;
// For things that need to consume a single value, like the parsing rules for attr().
Optional<StyleComponentValueRule> parse_as_component_value();
// For the contents of presentational attributes, which parse text into a single declarations value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute.
Vector<StyleComponentValueRule> parse_as_list_of_component_values();
Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
@ -150,6 +148,8 @@ private:
// "Parse a list of declarations" is for the contents of a style attribute, which parses text into the contents of a single style rule.
template<typename T>
Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&);
// "Parse a component value" is for things that need to consume a single value, like the parsing rules for attr().
template<typename T>
Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
template<typename T>