LibWeb: Use parsing helpers in parse_media_feature_value()

This commit is contained in:
Sam Atkins 2023-12-28 13:47:52 +00:00 committed by Andreas Kling
parent 12bcd029ff
commit 30a11dc133
Notes: sideshowbarker 2024-07-17 18:08:55 +09:00

View file

@ -10,6 +10,7 @@
#include <AK/Debug.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CalculatedOr.h>
#include <LibWeb/CSS/MediaList.h>
#include <LibWeb/CSS/MediaQuery.h>
#include <LibWeb/CSS/Parser/Parser.h>
@ -543,6 +544,8 @@ OwnPtr<MediaCondition> Parser::parse_media_in_parens(TokenStream<ComponentValue>
// `<mf-value>`, https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value
Optional<MediaFeatureValue> Parser::parse_media_feature_value(MediaFeatureID media_feature, TokenStream<ComponentValue>& tokens)
{
// NOTE: Calculations are not allowed for media feature values, at least in the current spec, so we reject them.
// Identifiers
if (tokens.peek_token().is(Token::Type::Ident)) {
auto transaction = tokens.begin_transaction();
@ -560,22 +563,21 @@ Optional<MediaFeatureValue> Parser::parse_media_feature_value(MediaFeatureID med
if (media_feature_accepts_type(media_feature, MediaFeatureValueType::Boolean)) {
auto transaction = tokens.begin_transaction();
tokens.skip_whitespace();
auto const& first = tokens.next_token();
if (first.is(Token::Type::Number) && first.token().number().is_integer()
&& (first.token().number_value() == 0 || first.token().number_value() == 1)) {
transaction.commit();
return MediaFeatureValue(first.token().number_value());
if (auto integer = parse_integer(tokens); integer.has_value() && !integer->is_calculated()) {
auto integer_value = integer->value();
if (integer_value == 0 || integer_value == 1) {
transaction.commit();
return MediaFeatureValue(integer_value);
}
}
}
// Integer
if (media_feature_accepts_type(media_feature, MediaFeatureValueType::Integer)) {
auto transaction = tokens.begin_transaction();
tokens.skip_whitespace();
auto const& first = tokens.next_token();
if (first.is(Token::Type::Number) && first.token().number().is_integer()) {
if (auto integer = parse_integer(tokens); integer.has_value() && !integer->is_calculated()) {
transaction.commit();
return MediaFeatureValue(first.token().number_value());
return MediaFeatureValue(integer->value());
}
}
@ -583,10 +585,9 @@ Optional<MediaFeatureValue> Parser::parse_media_feature_value(MediaFeatureID med
if (media_feature_accepts_type(media_feature, MediaFeatureValueType::Length)) {
auto transaction = tokens.begin_transaction();
tokens.skip_whitespace();
auto const& first = tokens.next_token();
if (auto length = parse_length(first); length.has_value()) {
if (auto length = parse_length(tokens); length.has_value() && !length->is_calculated()) {
transaction.commit();
return MediaFeatureValue(length.release_value());
return MediaFeatureValue(length->value());
}
}
@ -604,10 +605,9 @@ Optional<MediaFeatureValue> Parser::parse_media_feature_value(MediaFeatureID med
if (media_feature_accepts_type(media_feature, MediaFeatureValueType::Resolution)) {
auto transaction = tokens.begin_transaction();
tokens.skip_whitespace();
auto const& first = tokens.next_token();
if (auto resolution = parse_dimension(first); resolution.has_value() && resolution->is_resolution()) {
if (auto resolution = parse_resolution(tokens); resolution.has_value() && !resolution->is_calculated()) {
transaction.commit();
return MediaFeatureValue(resolution->resolution());
return MediaFeatureValue(resolution->value());
}
}