LibWeb/CSS: Add method for parsing <custom-ident> directly

We specifically want to parse one inside a selector, where we only care
about the string itself and don't want a whole style value.
This commit is contained in:
Sam Atkins 2025-03-24 13:44:05 +00:00 committed by Andreas Kling
parent 285fbc8f1c
commit 5cf04a33ad
Notes: github-actions[bot] 2025-03-25 07:56:19 +00:00
2 changed files with 14 additions and 6 deletions

View file

@ -301,6 +301,7 @@ private:
Optional<PropertyAndValue> parse_css_value_for_properties(ReadonlySpan<PropertyID>, TokenStream<ComponentValue>&);
RefPtr<CSSStyleValue> parse_builtin_value(TokenStream<ComponentValue>&);
RefPtr<CSSStyleValue> parse_calculated_value(ComponentValue const&);
Optional<FlyString> parse_custom_ident(TokenStream<ComponentValue>&, ReadonlySpan<StringView> blacklist);
RefPtr<CustomIdentStyleValue> parse_custom_ident_value(TokenStream<ComponentValue>&, ReadonlySpan<StringView> blacklist);
// NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp)
RefPtr<CalculationNode> parse_math_function(Function const&, CalculationContext const&);

View file

@ -2907,34 +2907,41 @@ RefPtr<CSSStyleValue> Parser::parse_builtin_value(TokenStream<ComponentValue>& t
}
// https://www.w3.org/TR/css-values-4/#custom-idents
RefPtr<CustomIdentStyleValue> Parser::parse_custom_ident_value(TokenStream<ComponentValue>& tokens, ReadonlySpan<StringView> blacklist)
Optional<FlyString> Parser::parse_custom_ident(TokenStream<ComponentValue>& tokens, ReadonlySpan<StringView> blacklist)
{
auto transaction = tokens.begin_transaction();
tokens.discard_whitespace();
auto const& token = tokens.consume_a_token();
if (!token.is(Token::Type::Ident))
return nullptr;
return {};
auto custom_ident = token.token().ident();
// The CSS-wide keywords are not valid <custom-ident>s.
if (is_css_wide_keyword(custom_ident))
return nullptr;
return {};
// The default keyword is reserved and is also not a valid <custom-ident>.
if (custom_ident.equals_ignoring_ascii_case("default"sv))
return nullptr;
return {};
// Specifications using <custom-ident> must specify clearly what other keywords are excluded from <custom-ident>,
// if any—for example by saying that any pre-defined keywords in that propertys value definition are excluded.
// Excluded keywords are excluded in all ASCII case permutations.
for (auto& value : blacklist) {
if (custom_ident.equals_ignoring_ascii_case(value))
return nullptr;
return {};
}
transaction.commit();
return CustomIdentStyleValue::create(custom_ident);
return custom_ident;
}
RefPtr<CustomIdentStyleValue> Parser::parse_custom_ident_value(TokenStream<ComponentValue>& tokens, ReadonlySpan<StringView> blacklist)
{
if (auto custom_ident = parse_custom_ident(tokens, blacklist); custom_ident.has_value())
return CustomIdentStyleValue::create(custom_ident.release_value());
return nullptr;
}
Optional<CSS::GridSize> Parser::parse_grid_size(ComponentValue const& component_value)