ladybird/Libraries/LibWeb/CSS/Supports.cpp
Sam Atkins 0f5e054f97 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!
2025-03-17 10:00:19 +00:00

61 lines
1.4 KiB
C++

/*
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/CSS/Supports.h>
namespace Web::CSS {
Supports::Supports(NonnullOwnPtr<BooleanExpression>&& condition)
: m_condition(move(condition))
{
m_matches = m_condition->evaluate_to_boolean(nullptr);
}
MatchResult Supports::Declaration::evaluate(HTML::Window const*) const
{
return as_match_result(m_matches);
}
String Supports::Declaration::to_string() const
{
return MUST(String::formatted("({})", m_declaration));
}
void Supports::Declaration::dump(StringBuilder& builder, int indent_levels) const
{
indent(builder, indent_levels);
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: `{}` matches={}\n", m_selector, m_matches);
}
String Supports::to_string() const
{
return m_condition->to_string();
}
void Supports::dump(StringBuilder& builder, int indent_levels) const
{
m_condition->dump(builder, indent_levels);
}
}