LibWeb: Implement generic boolean logic for media/supports queries

CSS Values 5 now defines a `<boolean-expr[]>` type that is used in place
of the bespoke grammar that previously existed for `@media` and
`@supports` queries. This commit implements some BooleanExpression
types to represent the nodes in a `<boolean-expr[]>`, and reimplements
`@media` and `@supports` queries using this.

The one part of this implementation I'm not convinced on is that the
`evaluate()` methods take a `HTML::Window*`. This is a compromise
because `@media` requires a Window, and `@supports` does not require
anything at all. As more users of `<boolean-expr[]>` get implemented in
the future, it will become clear if this is sufficient, or if we need
to do something smarter.

As a bonus, this actually improves our serialization of media queries!
This commit is contained in:
Sam Atkins 2025-03-14 10:31:27 +00:00
commit 0f5e054f97
Notes: github-actions[bot] 2025-03-17 10:01:42 +00:00
13 changed files with 526 additions and 663 deletions

View file

@ -9,150 +9,47 @@
namespace Web::CSS {
static void indent(StringBuilder& builder, int levels)
{
for (int i = 0; i < levels; i++)
builder.append(" "sv);
}
Supports::Supports(NonnullOwnPtr<Condition>&& condition)
Supports::Supports(NonnullOwnPtr<BooleanExpression>&& condition)
: m_condition(move(condition))
{
m_matches = m_condition->evaluate();
m_matches = m_condition->evaluate_to_boolean(nullptr);
}
bool Supports::Condition::evaluate() const
MatchResult Supports::Declaration::evaluate(HTML::Window const*) const
{
switch (type) {
case Type::Not:
return !children.first().evaluate();
case Type::And:
for (auto& child : children) {
if (!child.evaluate())
return false;
}
return true;
case Type::Or:
for (auto& child : children) {
if (child.evaluate())
return true;
}
return false;
}
VERIFY_NOT_REACHED();
}
bool Supports::InParens::evaluate() const
{
return value.visit(
[&](NonnullOwnPtr<Condition> const& condition) {
return condition->evaluate();
},
[&](Feature const& feature) {
return feature.evaluate();
},
[&](GeneralEnclosed const&) {
return false;
});
}
bool Supports::Feature::evaluate() const
{
return value.visit(
[&](Declaration const& declaration) {
return declaration.evaluate();
},
[&](Selector const& selector) {
return selector.evaluate();
});
return as_match_result(m_matches);
}
String Supports::Declaration::to_string() const
{
return MUST(String::formatted("({})", declaration));
}
String Supports::Selector::to_string() const
{
return MUST(String::formatted("selector({})", selector));
}
String Supports::Feature::to_string() const
{
return value.visit([](auto& it) { return it.to_string(); });
}
String Supports::InParens::to_string() const
{
return value.visit(
[](NonnullOwnPtr<Condition> const& condition) { return MUST(String::formatted("({})", condition->to_string())); },
[](Supports::Feature const& it) { return it.to_string(); },
[](GeneralEnclosed const& it) { return it.to_string(); });
}
String Supports::Condition::to_string() const
{
switch (type) {
case Type::Not:
return MUST(String::formatted("not {}", children.first().to_string()));
case Type::And:
return MUST(String::join(" and "sv, children));
case Type::Or:
return MUST(String::join(" or "sv, children));
}
VERIFY_NOT_REACHED();
}
String Supports::to_string() const
{
return m_condition->to_string();
return MUST(String::formatted("({})", m_declaration));
}
void Supports::Declaration::dump(StringBuilder& builder, int indent_levels) const
{
indent(builder, indent_levels);
builder.appendff("Declaration: {}\n", declaration);
builder.appendff("Declaration: `{}`, matches={}\n", m_declaration, m_matches);
}
MatchResult Supports::Selector::evaluate(HTML::Window const*) const
{
return as_match_result(m_matches);
}
String Supports::Selector::to_string() const
{
return MUST(String::formatted("selector({})", m_selector));
}
void Supports::Selector::dump(StringBuilder& builder, int indent_levels) const
{
indent(builder, indent_levels);
builder.appendff("Selector: {}\n", selector);
builder.appendff("Selector: `{}` matches={}\n", m_selector, m_matches);
}
void Supports::Feature::dump(StringBuilder& builder, int indent_levels) const
String Supports::to_string() const
{
value.visit([&](auto& it) { it.dump(builder, indent_levels); });
}
void Supports::InParens::dump(StringBuilder& builder, int indent_levels) const
{
value.visit(
[&](NonnullOwnPtr<Condition> const& condition) { condition->dump(builder, indent_levels); },
[&](Supports::Feature const& it) { it.dump(builder, indent_levels); },
[&](GeneralEnclosed const& it) {
indent(builder, indent_levels);
builder.appendff("GeneralEnclosed: {}\n", it.to_string());
});
}
void Supports::Condition::dump(StringBuilder& builder, int indent_levels) const
{
indent(builder, indent_levels);
StringView type_name = [](Type type) {
switch (type) {
case Type::And:
return "AND"sv;
case Type::Or:
return "OR"sv;
case Type::Not:
return "NOT"sv;
}
VERIFY_NOT_REACHED();
}(type);
builder.appendff("Condition: {}\n", type_name);
for (auto const& child : children)
child.dump(builder, indent_levels + 1);
return m_condition->to_string();
}
void Supports::dump(StringBuilder& builder, int indent_levels) const