diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-02-22 14:09:19 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-24 08:04:25 +0100 |
commit | fd2ef43cb488f3236958df13e3059b55688e5c9e (patch) | |
tree | 52e553fe6ed4e5b167b15cf15cd798118ba7b4a0 /Userland | |
parent | 53a3937c34bdc9d8a2a3df8b0cf474571c25d94c (diff) | |
download | serenity-fd2ef43cb488f3236958df13e3059b55688e5c9e.zip |
LibWeb: Implement `<resolution>` as a media feature type
This is the only dimension type besides `<length>` that is used in any
media queries in levels 4 or 5 right now. Others can be included
if/when they're needed.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/MediaQuery.cpp | 24 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/MediaQuery.h | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 8 |
3 files changed, 42 insertions, 4 deletions
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<String>(); } bool is_length() const { return m_value.has<Length>(); } bool is_number() const { return m_value.has<double>(); } + bool is_resolution() const { return m_value.has<Resolution>(); } bool is_same_type(MediaFeatureValue const& other) const; String const& ident() const @@ -54,6 +60,12 @@ public: return m_value.get<Length>(); } + Resolution const& resolution() const + { + VERIFY(is_resolution()); + return m_value.get<Resolution>(); + } + double number() const { VERIFY(is_number()); @@ -62,7 +74,7 @@ public: private: // TODO: Support <ratio> once we have that. - Variant<String, Length, double> m_value; + Variant<String, Length, Resolution, double> 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<MediaFeatureValue> Parser::parse_media_feature_value(TokenStream<StyleC return MediaFeatureValue(first.token().number_value()); // `<dimension>` - 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()); + } // `<ident>` if (first.is(Token::Type::Ident)) |