diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp index ae5a37c9dd..73c9827927 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp @@ -25,6 +25,7 @@ String MediaFeatureValue::to_string() const return m_value.visit( [](String const& ident) { return serialize_an_identifier(ident); }, [](Length const& length) { return length.to_string(); }, + [](Resolution const& resolution) { return resolution.to_string(); }, [](double number) { return String::number(number); }); } @@ -33,6 +34,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const return m_value.visit( [&](String const&) { return other.is_ident(); }, [&](Length const&) { return other.is_length(); }, + [&](Resolution const&) { return other.is_resolution(); }, [&](double) { return other.is_number(); }); } @@ -86,6 +88,8 @@ bool MediaFeature::evaluate(DOM::Window const& window) const return queried_value.number() != 0; if (queried_value.is_length()) return queried_value.length().raw_value() != 0; + if (queried_value.is_resolution()) + return queried_value.resolution().to_dots_per_pixel() != 0; if (queried_value.is_ident()) return queried_value.ident() != "none"; return false; @@ -141,7 +145,6 @@ bool MediaFeature::compare(DOM::Window const& window, MediaFeatureValue left, Co } if (left.is_length()) { - float left_px; float right_px; // Save ourselves some work if neither side is a relative length. @@ -178,6 +181,25 @@ bool MediaFeature::compare(DOM::Window const& window, MediaFeatureValue left, Co VERIFY_NOT_REACHED(); } + if (left.is_resolution()) { + auto left_dppx = left.resolution().to_dots_per_pixel(); + auto right_dppx = right.resolution().to_dots_per_pixel(); + + switch (comparison) { + case Comparison::Equal: + return left_dppx == right_dppx; + case Comparison::LessThan: + return left_dppx < right_dppx; + case Comparison::LessThanOrEqual: + return left_dppx <= right_dppx; + case Comparison::GreaterThan: + return left_dppx > right_dppx; + case Comparison::GreaterThanOrEqual: + return left_dppx >= right_dppx; + } + VERIFY_NOT_REACHED(); + } + VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.h b/Userland/Libraries/LibWeb/CSS/MediaQuery.h index ed8adbfa43..93392e31c5 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.h +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.h @@ -30,6 +30,11 @@ public: { } + explicit MediaFeatureValue(Resolution resolution) + : m_value(move(resolution)) + { + } + explicit MediaFeatureValue(double number) : m_value(number) { @@ -40,6 +45,7 @@ public: bool is_ident() const { return m_value.has(); } bool is_length() const { return m_value.has(); } bool is_number() const { return m_value.has(); } + bool is_resolution() const { return m_value.has(); } bool is_same_type(MediaFeatureValue const& other) const; String const& ident() const @@ -54,6 +60,12 @@ public: return m_value.get(); } + Resolution const& resolution() const + { + VERIFY(is_resolution()); + return m_value.get(); + } + double number() const { VERIFY(is_number()); @@ -62,7 +74,7 @@ public: private: // TODO: Support once we have that. - Variant m_value; + Variant m_value; }; // https://www.w3.org/TR/mediaqueries-4/#mq-features diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 66a1771a60..69b216a74a 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1149,8 +1149,12 @@ Optional Parser::parse_media_feature_value(TokenStream` - if (auto length = parse_length(first); length.has_value()) - return MediaFeatureValue(length.release_value()); + if (auto dimension = parse_dimension(first); dimension.has_value()) { + if (dimension->is_length()) + return MediaFeatureValue(dimension->length()); + if (dimension->is_resolution()) + return MediaFeatureValue(dimension->resolution()); + } // `` if (first.is(Token::Type::Ident))