1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibWeb: Introduce MediaFeatureValue type for use in media queries

Previously, we were using StyleValues for this, which was a bit of a
hack and was brittle, breaking when I modified how custom properties
were parsed. This is better and also lets us limit the kinds of value
that can be used here, to match the spec.
This commit is contained in:
Sam Atkins 2021-12-29 17:52:14 +00:00 committed by Andreas Kling
parent 0a8e289f37
commit 6299d68e45
6 changed files with 176 additions and 46 deletions

View file

@ -895,8 +895,8 @@ Optional<MediaQuery::MediaFeature> Parser::consume_media_feature(TokenStream<Sty
return invalid_feature();
tokens.skip_whitespace();
auto value = parse_css_value(PropertyID::Custom, tokens);
if (value.is_error())
auto value = parse_media_feature_value(tokens);
if (!value.has_value())
return invalid_feature();
if (tokens.has_next_token())
@ -964,6 +964,32 @@ Optional<MediaQuery::MediaType> Parser::consume_media_type(TokenStream<StyleComp
return {};
}
// `<mf-value>`, https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value
Optional<MediaFeatureValue> Parser::parse_media_feature_value(TokenStream<StyleComponentValueRule>& tokens)
{
// `<number> | <dimension> | <ident> | <ratio>`
auto position = tokens.position();
tokens.skip_whitespace();
auto& first = tokens.next_token();
// `<number>`
if (first.is(Token::Type::Number))
return MediaFeatureValue(first.token().number_value());
// `<dimension>`
if (auto length = parse_length(first); length.has_value())
return MediaFeatureValue(length.release_value());
// `<ident>`
if (first.is(Token::Type::Ident))
return MediaFeatureValue(first.token().ident());
// FIXME: `<ratio>`, once we have ratios.
tokens.rewind_to_position(position);
return {};
}
RefPtr<Supports> Parser::parse_as_supports()
{
return parse_a_supports(m_token_stream);