diff --git a/Libraries/LibWeb/CSS/BooleanExpression.h b/Libraries/LibWeb/CSS/BooleanExpression.h index 85b86ae032d..0ded122c9b5 100644 --- a/Libraries/LibWeb/CSS/BooleanExpression.h +++ b/Libraries/LibWeb/CSS/BooleanExpression.h @@ -14,18 +14,42 @@ namespace Web::CSS { // Corresponds to Kleene 3-valued logic. -enum class MatchResult { +enum class MatchResult : u8 { False, True, 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; } -inline MatchResult negate(MatchResult value) +constexpr MatchResult negate(MatchResult value) { switch (value) { case MatchResult::False: @@ -38,6 +62,19 @@ inline MatchResult negate(MatchResult value) 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 `` concept. // https://drafts.csswg.org/css-values-5/#typedef-boolean-expr class BooleanExpression { diff --git a/Libraries/LibWeb/CSS/MediaQuery.cpp b/Libraries/LibWeb/CSS/MediaQuery.cpp index 0aed3be483d..7f41aca933b 100644 --- a/Libraries/LibWeb/CSS/MediaQuery.cpp +++ b/Libraries/LibWeb/CSS/MediaQuery.cpp @@ -305,8 +305,8 @@ bool MediaQuery::evaluate(HTML::Window const& window) MatchResult result = matches_media(m_media_type); - if ((result == MatchResult::True) && m_media_condition) - result = m_media_condition->evaluate(&window); + if ((result != MatchResult::False) && m_media_condition) + result = result && m_media_condition->evaluate(&window); if (m_negated) result = negate(result);