Skip to content

Truly independent web browser

LibWeb: Match prefixed device pixel ratio media queries

sideshowbarker859424e4b264
diff --git a/Libraries/LibWeb/CSS/MediaFeatures.json b/Libraries/LibWeb/CSS/MediaFeatures.json
index f5c0eab041a..26458c0768d 100644
--- a/Libraries/LibWeb/CSS/MediaFeatures.json
+++ b/Libraries/LibWeb/CSS/MediaFeatures.json
@@ -1,4 +1,10 @@
{
+ "-webkit-device-pixel-ratio": {
+ "type": "range",
+ "values": [
+ "<number>"
+ ]
+ },
"-webkit-transform-3d": {
"type": "discrete",
"values": [
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index 0eef7734140..abe83bf0d6c 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -728,6 +728,8 @@ CSS::Parser::ValueParserFFI::FfiMediaFeatureValue Window::query_media_feature(CS
return ident_media_feature_value(CSS::Keyword::Srgb);
case CSS::MediaFeatureID::VideoDynamicRange:
return ident_media_feature_value(page().client().screen_shows_high_dynamic_range() ? CSS::Keyword::High : CSS::Keyword::Standard);
+ case CSS::MediaFeatureID::WebkitDevicePixelRatio:
+ return { .kind = CSS::Parser::ValueParserFFI::FfiMediaFeatureValueKind::Number, .keyword = 0, .value = device_pixel_ratio(), .second_value = 0 };
case CSS::MediaFeatureID::WebkitTransform3d:
// https://compat.spec.whatwg.org/#css-media-queries-webkit-transform-3d
// If the user agent supports 3D transforms, the value will be 1. Otherwise the value is 0.
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 7d342e1af0e..f558bb01d84 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -71,7 +71,8 @@ fn generate_media_features(manifest_dir: &Path, out_dir: &Path) -> Result<(), Bo
pub(crate) const MEDIA_FEATURE_VALUE_INTEGER: u8 = 1 << 1;\n\
pub(crate) const MEDIA_FEATURE_VALUE_LENGTH: u8 = 1 << 2;\n\
pub(crate) const MEDIA_FEATURE_VALUE_RATIO: u8 = 1 << 3;\n\
- pub(crate) const MEDIA_FEATURE_VALUE_RESOLUTION: u8 = 1 << 4;\n\n\
+ pub(crate) const MEDIA_FEATURE_VALUE_RESOLUTION: u8 = 1 << 4;\n\
+ pub(crate) const MEDIA_FEATURE_VALUE_NUMBER: u8 = 1 << 5;\n\n\
pub(crate) struct MediaFeatureMetadata {\n\
pub name: &'static str,\n\
pub allows_range: bool,\n\
@@ -98,6 +99,7 @@ fn generate_media_features(manifest_dir: &Path, out_dir: &Path) -> Result<(), Bo
match value {
"<mq-boolean>" => value_types.push("MEDIA_FEATURE_VALUE_BOOLEAN"),
"<integer>" => value_types.push("MEDIA_FEATURE_VALUE_INTEGER"),
+ "<number>" => value_types.push("MEDIA_FEATURE_VALUE_NUMBER"),
"<length>" => value_types.push("MEDIA_FEATURE_VALUE_LENGTH"),
"<ratio>" => value_types.push("MEDIA_FEATURE_VALUE_RATIO"),
"<resolution>" => value_types.push("MEDIA_FEATURE_VALUE_RESOLUTION"),
diff --git a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
index dd92b727884..6e653e20f26 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/query_parser.rs
@@ -164,6 +164,7 @@ pub enum FfiMediaFeatureValueKind {
Length,
Ratio,
Resolution,
+ Number,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -793,6 +794,24 @@ where
if !allow_min_max_prefix || name.len() <= 4 {
return None;
}
+ // https://compat.spec.whatwg.org/#css-media-queries-webkit-device-pixel-ratio
+ // The range prefix goes after "-webkit-" for this feature.
+ if kind == QueryKind::Media
+ && name.len() > 12
+ && equals_ascii_case_insensitive(&name[..8], b"-webkit-")
+ && equals_ascii_case_insensitive(&name[12..], b"device-pixel-ratio")
+ {
+ let name_type = if equals_ascii_case_insensitive(&name[8..12], b"min-") {
+ FeatureNameType::Min
+ } else if equals_ascii_case_insensitive(&name[8..12], b"max-") {
+ FeatureNameType::Max
+ } else {
+ return None;
+ };
+ let (id, allows_range) = resolve_feature(kind, &[&name[..8], &name[12..]].concat())?;
+ transaction.commit();
+ return Some((name_type, id, allows_range));
+ }
let name_type = if equals_ascii_case_insensitive(&name[..4], b"min-") {
FeatureNameType::Min
} else if equals_ascii_case_insensitive(&name[..4], b"max-") {
@@ -800,6 +819,9 @@ where
} else {
return None;
};
+ if kind == QueryKind::Media && equals_ascii_case_insensitive(&name[4..], b"-webkit-device-pixel-ratio") {
+ return None;
+ }
let (id, allows_range) = resolve_feature(kind, &name[4..])?;
if !allows_range {
return None;
@@ -1700,6 +1722,12 @@ fn serialize_query_feature_value(sink: &mut TextSink, value: &QueryFeatureValue,
} else {
0
};
+ if media_feature_value_types & MEDIA_FEATURE_VALUE_NUMBER != 0
+ && let Some(parsed) = value.parsed_as(FeatureValueParse::Number)
+ {
+ push_utf16(sink, &crate::css::serialize::serialize_style_value_to_utf16(parsed));
+ return;
+ }
let is_ratio = media_feature_value_types & MEDIA_FEATURE_VALUE_RATIO != 0 || (kind == QueryKind::Size && id == 0);
if is_ratio {
let components = components
@@ -1789,6 +1817,12 @@ fn serialize_query_feature(sink: &mut TextSink, feature: &QueryFeature, kind: Qu
match feature {
QueryFeature::Boolean { .. } => sink.push_ascii(name),
QueryFeature::Plain { name_type, value, .. } => {
+ let name = if name == "-webkit-device-pixel-ratio" {
+ sink.push_ascii("-webkit-");
+ "device-pixel-ratio"
+ } else {
+ name
+ };
match name_type {
FeatureNameType::Normal => {}
FeatureNameType::Min => sink.push_ascii("min-"),
@@ -1993,6 +2027,7 @@ fn serialize_media_query(query: &MediaQuery) -> Vec<u16> {
enum ResolvedFeatureValue {
Ident(u16),
Integer(i32),
+ Number(f64),
Length(f64),
Ratio { numerator: f64, denominator: f64 },
Resolution(f64),
@@ -2211,6 +2246,12 @@ fn parse_media_feature_value(
return resolve_parsed_media_feature_value(integer, length_context, None)
.unwrap_or(ResolvedFeatureValue::Unknown);
}
+ if types & MEDIA_FEATURE_VALUE_NUMBER != 0
+ && let Some(number) = value.parsed_as(FeatureValueParse::Number)
+ && let Some(resolved) = resolve_number(number, length_context, None)
+ {
+ return ResolvedFeatureValue::Number(resolved);
+ }
if types & MEDIA_FEATURE_VALUE_INTEGER != 0
&& let Some(resolved) = resolve_feature_value_parsed_as(value, FeatureValueParse::Integer, length_context)
{
@@ -2280,6 +2321,7 @@ fn resolved_environment_value(value: &FfiMediaFeatureValue) -> Option<ResolvedFe
FfiMediaFeatureValueKind::Absent => None,
FfiMediaFeatureValueKind::Ident => Some(ResolvedFeatureValue::Ident(value.keyword)),
FfiMediaFeatureValueKind::Integer => Some(ResolvedFeatureValue::Integer(value.value as i32)),
+ FfiMediaFeatureValueKind::Number => Some(ResolvedFeatureValue::Number(value.value)),
FfiMediaFeatureValueKind::Length => Some(ResolvedFeatureValue::Length(value.value)),
FfiMediaFeatureValueKind::Ratio => Some(ResolvedFeatureValue::Ratio {
numerator: value.value,
@@ -2315,7 +2357,8 @@ fn compare_feature_values(
(ResolvedFeatureValue::Integer(left), ResolvedFeatureValue::Integer(right)) => {
comparison_matches(Some(left.cmp(right)))
}
- (ResolvedFeatureValue::Length(left), ResolvedFeatureValue::Length(right))
+ (ResolvedFeatureValue::Number(left), ResolvedFeatureValue::Number(right))
+ | (ResolvedFeatureValue::Length(left), ResolvedFeatureValue::Length(right))
| (ResolvedFeatureValue::Resolution(left), ResolvedFeatureValue::Resolution(right)) => {
comparison_matches(left.partial_cmp(right))
}
@@ -2373,7 +2416,9 @@ fn evaluate_query_feature(
MatchResult::False
}
}
- ResolvedFeatureValue::Length(value) | ResolvedFeatureValue::Resolution(value) => {
+ ResolvedFeatureValue::Number(value)
+ | ResolvedFeatureValue::Length(value)
+ | ResolvedFeatureValue::Resolution(value) => {
if *value != 0.0 {
MatchResult::True
} else {
diff --git a/Tests/LibWeb/Text/expected/prefixed-device-pixel-ratio.txt b/Tests/LibWeb/Text/expected/prefixed-device-pixel-ratio.txt
new file mode 100644
index 00000000000..7ab837dd84b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/prefixed-device-pixel-ratio.txt
@@ -0,0 +1,21 @@
+1 (-webkit-device-pixel-ratio) true
+1 (-webkit-min-device-pixel-ratio: 1.5) false
+1 (-webkit-device-pixel-ratio: 1.5) false
+1 (-webkit-max-device-pixel-ratio: 1.5) true
+1 (1 < -webkit-device-pixel-ratio <= 2) false
+1 (-webkit-device-pixel-ratio: 1dppx) false
+1 (min--webkit-device-pixel-ratio: 0) false
+1.5 (-webkit-device-pixel-ratio) true
+1.5 (-webkit-min-device-pixel-ratio: 1.5) true
+1.5 (-webkit-device-pixel-ratio: 1.5) true
+1.5 (-webkit-max-device-pixel-ratio: 1.5) true
+1.5 (1 < -webkit-device-pixel-ratio <= 2) true
+1.5 (-webkit-device-pixel-ratio: 1dppx) false
+1.5 (min--webkit-device-pixel-ratio: 0) false
+2 (-webkit-device-pixel-ratio) true
+2 (-webkit-min-device-pixel-ratio: 1.5) true
+2 (-webkit-device-pixel-ratio: 1.5) false
+2 (-webkit-max-device-pixel-ratio: 1.5) false
+2 (1 < -webkit-device-pixel-ratio <= 2) true
+2 (-webkit-device-pixel-ratio: 1dppx) false
+2 (min--webkit-device-pixel-ratio: 0) false
diff --git a/Tests/LibWeb/Text/expected/wpt-import/compat/webkit-device-pixel-ratio-serialization.txt b/Tests/LibWeb/Text/expected/wpt-import/compat/webkit-device-pixel-ratio-serialization.txt
new file mode 100644
index 00000000000..a7fba59569e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/compat/webkit-device-pixel-ratio-serialization.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Pass
+Pass matchMedia("(-webkit-device-pixel-ratio: 1)").media
+Pass matchMedia("(-webkit-min-device-pixel-ratio: 1)").media
+Pass matchMedia("(-webkit-max-device-pixel-ratio: 2)").media
+Pass matchMedia("screen and (-webkit-min-device-pixel-ratio: 1.5)").media
+Pass CSSMediaRule serialization of (-webkit-min-device-pixel-ratio: 1)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/prefixed-device-pixel-ratio.html b/Tests/LibWeb/Text/input/prefixed-device-pixel-ratio.html
new file mode 100644
index 00000000000..cbff09fe44f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/prefixed-device-pixel-ratio.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ for (const dpi of [1, 1.5, 2]) {
+ internals.setDevicePixelRatio(dpi);
+ for (const query of ["(-webkit-device-pixel-ratio)", "(-webkit-min-device-pixel-ratio: 1.5)",
+ "(-webkit-device-pixel-ratio: 1.5)", "(-webkit-max-device-pixel-ratio: 1.5)",
+ "(1 < -webkit-device-pixel-ratio <= 2)", "(-webkit-device-pixel-ratio: 1dppx)",
+ "(min--webkit-device-pixel-ratio: 0)"])
+ println(`${dpi} ${query} ${matchMedia(query).matches}`);
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/compat/webkit-device-pixel-ratio-serialization.html b/Tests/LibWeb/Text/input/wpt-import/compat/webkit-device-pixel-ratio-serialization.html
new file mode 100644
index 00000000000..121e5fe2815
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/compat/webkit-device-pixel-ratio-serialization.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Compat: -webkit-device-pixel-ratio media feature serialization</title>
+<link rel="help" href="https://compat.spec.whatwg.org/#css-media-queries-webkit-device-pixel-ratio">
+<link rel="help" href="https://drafts.csswg.org/cssom/#serialize-a-media-query">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<style id="style">
+@media (-webkit-min-device-pixel-ratio: 1) {}
+</style>
+<script>
+const queries = [
+ "(-webkit-device-pixel-ratio: 1)",
+ "(-webkit-min-device-pixel-ratio: 1)",
+ "(-webkit-max-device-pixel-ratio: 2)",
+ "screen and (-webkit-min-device-pixel-ratio: 1.5)",
+];
+
+for (const query of queries) {
+ test(() => {
+ assert_equals(matchMedia(query).media, query);
+ }, `matchMedia("${query}").media`);
+}
+
+test(() => {
+ const rule = document.getElementById("style").sheet.cssRules[0];
+ assert_equals(rule.media.mediaText, "(-webkit-min-device-pixel-ratio: 1)");
+ assert_equals(rule.conditionText, "(-webkit-min-device-pixel-ratio: 1)");
+}, "CSSMediaRule serialization of (-webkit-min-device-pixel-ratio: 1)");
+</script>