LibWeb: Add <general-enclosed> support to Media Queries

This commit is contained in:
Sam Atkins 2021-11-24 15:11:59 +00:00 committed by Andreas Kling
parent b03dac99a5
commit 7d5c626276
Notes: sideshowbarker 2024-07-18 00:42:01 +09:00
3 changed files with 19 additions and 1 deletions

View file

@ -106,6 +106,9 @@ String MediaQuery::MediaCondition::to_string() const
case Type::Or:
builder.join(" or ", conditions);
break;
case Type::GeneralEnclosed:
builder.append(general_enclosed->to_string());
break;
}
builder.append(')');
return builder.to_string();
@ -122,6 +125,8 @@ MatchResult MediaQuery::MediaCondition::evaluate(DOM::Window const& window) cons
return evaluate_and(conditions, [&](auto& child) { return child.evaluate(window); });
case Type::Or:
return evaluate_or(conditions, [&](auto& child) { return child.evaluate(window); });
case Type::GeneralEnclosed:
return general_enclosed->evaluate();
}
VERIFY_NOT_REACHED();
}

View file

@ -65,11 +65,13 @@ public:
And,
Or,
Not,
GeneralEnclosed,
};
Type type;
MediaFeature feature;
NonnullOwnPtrVector<MediaCondition> conditions;
Optional<GeneralEnclosed> general_enclosed;
MatchResult evaluate(DOM::Window const&) const;
String to_string() const;

View file

@ -752,6 +752,7 @@ NonnullRefPtr<MediaQuery> Parser::parse_media_query(TokenStream<StyleComponentVa
OwnPtr<MediaQuery::MediaCondition> Parser::consume_media_condition(TokenStream<StyleComponentValueRule>& tokens)
{
// "not <media-condition>"
// ( `<media-not>` in the grammar )
auto position = tokens.position();
auto& first_token = tokens.peek_token();
if (first_token.is(Token::Type::Ident) && first_token.token().ident().equals_ignoring_case("not"sv)) {
@ -770,6 +771,7 @@ OwnPtr<MediaQuery::MediaCondition> Parser::consume_media_condition(TokenStream<S
}
// "<media-condition> ([and | or] <media-condition>)*"
// ( `<media-in-parens> [ <media-and>* | <media-or>* ]` in the grammar )
NonnullOwnPtrVector<MediaQuery::MediaCondition> child_conditions;
Optional<MediaQuery::MediaCondition::Type> condition_type {};
auto as_condition_type = [](auto& token) -> Optional<MediaQuery::MediaCondition::Type> {
@ -836,7 +838,7 @@ OwnPtr<MediaQuery::MediaCondition> Parser::consume_media_condition(TokenStream<S
return adopt_own(*condition);
}
// "<media-feature>"
// `<media-feature>`
tokens.rewind_to_position(position);
if (auto feature = consume_media_feature(tokens); feature.has_value()) {
auto condition = new MediaQuery::MediaCondition;
@ -845,6 +847,15 @@ OwnPtr<MediaQuery::MediaCondition> Parser::consume_media_condition(TokenStream<S
return adopt_own(*condition);
}
// `<general-enclosed>`
tokens.rewind_to_position(position);
if (auto general_enclosed = parse_general_enclosed(tokens); general_enclosed.has_value()) {
auto condition = new MediaQuery::MediaCondition;
condition->type = MediaQuery::MediaCondition::Type::GeneralEnclosed;
condition->general_enclosed = general_enclosed.release_value();
return adopt_own(*condition);
}
tokens.rewind_to_position(position);
return {};
}