1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

LibWeb: Use MatchResults for MediaQuery evaluation

This commit is contained in:
Sam Atkins 2021-11-24 14:59:31 +00:00 committed by Andreas Kling
parent 99e18f40fb
commit b03dac99a5
2 changed files with 18 additions and 26 deletions

View file

@ -111,30 +111,18 @@ String MediaQuery::MediaCondition::to_string() const
return builder.to_string(); return builder.to_string();
} }
bool MediaQuery::MediaCondition::evaluate(DOM::Window const& window) const MatchResult MediaQuery::MediaCondition::evaluate(DOM::Window const& window) const
{ {
switch (type) { switch (type) {
case Type::Single: case Type::Single:
return feature.evaluate(window); return as_match_result(feature.evaluate(window));
case Type::Not: case Type::Not:
return !conditions.first().evaluate(window); return negate(conditions.first().evaluate(window));
case Type::And: case Type::And:
for (auto& child : conditions) { return evaluate_and(conditions, [&](auto& child) { return child.evaluate(window); });
if (!child.evaluate(window))
return false;
}
return true;
case Type::Or: case Type::Or:
for (auto& child : conditions) { return evaluate_or(conditions, [&](auto& child) { return child.evaluate(window); });
if (child.evaluate(window))
return true;
}
return false;
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
@ -194,16 +182,16 @@ String MediaQuery::to_string() const
bool MediaQuery::evaluate(DOM::Window const& window) bool MediaQuery::evaluate(DOM::Window const& window)
{ {
auto matches_media = [](MediaType media) -> bool { auto matches_media = [](MediaType media) -> MatchResult {
switch (media) { switch (media) {
case MediaType::All: case MediaType::All:
return true; return MatchResult::True;
case MediaType::Print: case MediaType::Print:
// FIXME: Enable for printing, when we have printing! // FIXME: Enable for printing, when we have printing!
return false; return MatchResult::False;
case MediaType::Screen: case MediaType::Screen:
// FIXME: Disable for printing, when we have printing! // FIXME: Disable for printing, when we have printing!
return true; return MatchResult::True;
// Deprecated, must never match: // Deprecated, must never match:
case MediaType::TTY: case MediaType::TTY:
case MediaType::TV: case MediaType::TV:
@ -213,17 +201,20 @@ bool MediaQuery::evaluate(DOM::Window const& window)
case MediaType::Embossed: case MediaType::Embossed:
case MediaType::Aural: case MediaType::Aural:
case MediaType::Speech: case MediaType::Speech:
return false; return MatchResult::False;
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
}; };
bool result = matches_media(m_media_type); MatchResult result = matches_media(m_media_type);
if (result && m_media_condition) if ((result == MatchResult::True) && m_media_condition)
result = m_media_condition->evaluate(window); result = m_media_condition->evaluate(window);
m_matches = m_negated ? !result : result; if (m_negated)
result = negate(result);
m_matches = result == MatchResult::True;
return m_matches; return m_matches;
} }

View file

@ -12,6 +12,7 @@
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <LibWeb/CSS/GeneralEnclosed.h>
#include <LibWeb/CSS/StyleValue.h> #include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS { namespace Web::CSS {
@ -70,7 +71,7 @@ public:
MediaFeature feature; MediaFeature feature;
NonnullOwnPtrVector<MediaCondition> conditions; NonnullOwnPtrVector<MediaCondition> conditions;
bool evaluate(DOM::Window const&) const; MatchResult evaluate(DOM::Window const&) const;
String to_string() const; String to_string() const;
}; };