mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-05 16:41:52 +00:00
LibWeb/CSS: Make media type and feature evaluation combination explicit
Functionally this is the same before, as result is always True or False before this point, and `True && Foo` evaluates to `Foo`. But this is more clearly correct, instead of correct by coincidence.
This commit is contained in:
parent
a4d3c62524
commit
b577302f07
Notes:
github-actions[bot]
2025-05-23 09:19:31 +00:00
Author: https://github.com/AtkinsSJ
Commit: b577302f07
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4817
2 changed files with 42 additions and 5 deletions
|
@ -14,18 +14,42 @@
|
||||||
namespace Web::CSS {
|
namespace Web::CSS {
|
||||||
|
|
||||||
// Corresponds to Kleene 3-valued logic.
|
// Corresponds to Kleene 3-valued logic.
|
||||||
enum class MatchResult {
|
enum class MatchResult : u8 {
|
||||||
False,
|
False,
|
||||||
True,
|
True,
|
||||||
Unknown,
|
Unknown,
|
||||||
};
|
};
|
||||||
|
|
||||||
inline MatchResult as_match_result(bool value)
|
constexpr MatchResult operator&&(MatchResult const a, MatchResult const b)
|
||||||
|
{
|
||||||
|
// If either is false, false.
|
||||||
|
if (a == MatchResult::False || b == MatchResult::False)
|
||||||
|
return MatchResult::False;
|
||||||
|
// If both are true, true.
|
||||||
|
if (a == MatchResult::True && b == MatchResult::True)
|
||||||
|
return MatchResult::True;
|
||||||
|
// Otherwise, unknown.
|
||||||
|
return MatchResult::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr MatchResult operator||(MatchResult const a, MatchResult const b)
|
||||||
|
{
|
||||||
|
// If either is true, true.
|
||||||
|
if (a == MatchResult::True || b == MatchResult::True)
|
||||||
|
return MatchResult::True;
|
||||||
|
// If both are false, false.
|
||||||
|
if (a == MatchResult::False && b == MatchResult::False)
|
||||||
|
return MatchResult::False;
|
||||||
|
// Otherwise, unknown.
|
||||||
|
return MatchResult::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr MatchResult as_match_result(bool value)
|
||||||
{
|
{
|
||||||
return value ? MatchResult::True : MatchResult::False;
|
return value ? MatchResult::True : MatchResult::False;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline MatchResult negate(MatchResult value)
|
constexpr MatchResult negate(MatchResult value)
|
||||||
{
|
{
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case MatchResult::False:
|
case MatchResult::False:
|
||||||
|
@ -38,6 +62,19 @@ inline MatchResult negate(MatchResult value)
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr StringView to_string(MatchResult result)
|
||||||
|
{
|
||||||
|
switch (result) {
|
||||||
|
case MatchResult::False:
|
||||||
|
return "false"sv;
|
||||||
|
case MatchResult::True:
|
||||||
|
return "true"sv;
|
||||||
|
case MatchResult::Unknown:
|
||||||
|
return "unknown"sv;
|
||||||
|
}
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
// The contents of this file implement the `<boolean-expr>` concept.
|
// The contents of this file implement the `<boolean-expr>` concept.
|
||||||
// https://drafts.csswg.org/css-values-5/#typedef-boolean-expr
|
// https://drafts.csswg.org/css-values-5/#typedef-boolean-expr
|
||||||
class BooleanExpression {
|
class BooleanExpression {
|
||||||
|
|
|
@ -305,8 +305,8 @@ bool MediaQuery::evaluate(HTML::Window const& window)
|
||||||
|
|
||||||
MatchResult result = matches_media(m_media_type);
|
MatchResult result = matches_media(m_media_type);
|
||||||
|
|
||||||
if ((result == MatchResult::True) && m_media_condition)
|
if ((result != MatchResult::False) && m_media_condition)
|
||||||
result = m_media_condition->evaluate(&window);
|
result = result && m_media_condition->evaluate(&window);
|
||||||
|
|
||||||
if (m_negated)
|
if (m_negated)
|
||||||
result = negate(result);
|
result = negate(result);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue