1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-02 23:22:07 +00:00

LibWeb: Implement MediaQuery matching :^)

Currently, `evaluate()` recalculates whether the MediaQuery matches or
not, and stores it in `m_matches`, which users can query using
`matches()`. This allows us to know when the match-state changes, which
is required to fire MediaQueryList's change event.
This commit is contained in:
Sam Atkins 2021-10-03 19:39:48 +01:00 committed by Andreas Kling
parent f354fd72f1
commit 1c829e0417
6 changed files with 163 additions and 1 deletions

View file

@ -5,6 +5,8 @@
*/
#include <LibWeb/CSS/MediaQuery.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Window.h>
namespace Web::CSS {
@ -33,6 +35,59 @@ String MediaQuery::MediaFeature::to_string() const
VERIFY_NOT_REACHED();
}
bool MediaQuery::MediaFeature::evaluate(DOM::Window const& window) const
{
auto queried_value = window.query_media_feature(name);
if (!queried_value)
return false;
switch (type) {
case Type::IsTrue:
if (queried_value->has_number())
return queried_value->to_number() != 0;
if (queried_value->has_length())
return queried_value->to_length().raw_value() != 0;
if (queried_value->has_identifier())
return queried_value->to_identifier() != ValueID::None;
return false;
case Type::ExactValue:
return queried_value->equals(*value);
case Type::MinValue:
if (queried_value->has_number() && value->has_number())
return queried_value->to_number() >= value->to_number();
if (queried_value->has_length() && value->has_length()) {
auto queried_length = queried_value->to_length();
auto value_length = value->to_length();
// FIXME: We should be checking that lengths are valid during parsing
if (!value_length.is_absolute()) {
dbgln("Media feature was given a non-absolute length, which is invalid! {}", value_length.to_string());
return false;
}
return queried_length.absolute_length_to_px() >= value_length.absolute_length_to_px();
}
return false;
case Type::MaxValue:
if (queried_value->has_number() && value->has_number())
return queried_value->to_number() <= value->to_number();
if (queried_value->has_length() && value->has_length()) {
auto queried_length = queried_value->to_length();
auto value_length = value->to_length();
// FIXME: We should be checking that lengths are valid during parsing
if (!value_length.is_absolute()) {
dbgln("Media feature was given a non-absolute length, which is invalid! {}", value_length.to_string());
return false;
}
return queried_length.absolute_length_to_px() <= value_length.absolute_length_to_px();
}
return false;
}
VERIFY_NOT_REACHED();
}
String MediaQuery::MediaCondition::to_string() const
{
StringBuilder builder;
@ -56,6 +111,33 @@ String MediaQuery::MediaCondition::to_string() const
return builder.to_string();
}
bool MediaQuery::MediaCondition::evaluate(DOM::Window const& window) const
{
switch (type) {
case Type::Single:
return feature.evaluate(window);
case Type::Not:
return !conditions.first().evaluate(window);
case Type::And:
for (auto& child : conditions) {
if (!child.evaluate(window))
return false;
}
return true;
case Type::Or:
for (auto& child : conditions) {
if (child.evaluate(window))
return true;
}
return false;
}
VERIFY_NOT_REACHED();
}
String MediaQuery::to_string() const
{
StringBuilder builder;
@ -110,4 +192,39 @@ String MediaQuery::to_string() const
return builder.to_string();
}
bool MediaQuery::evaluate(DOM::Window const& window)
{
auto matches_media = [](MediaType media) -> bool {
switch (media) {
case MediaType::All:
return true;
case MediaType::Print:
// FIXME: Enable for printing, when we have printing!
return false;
case MediaType::Screen:
// FIXME: Disable for printing, when we have printing!
return true;
// Deprecated, must never match:
case MediaType::TTY:
case MediaType::TV:
case MediaType::Projection:
case MediaType::Handheld:
case MediaType::Braille:
case MediaType::Embossed:
case MediaType::Aural:
case MediaType::Speech:
return false;
}
VERIFY_NOT_REACHED();
};
bool result = matches_media(m_media_type);
if (result && m_media_condition)
result = m_media_condition->evaluate(window);
m_matches = m_negated ? !result : result;
return m_matches;
}
}