Skip to content

Commit c8f2cfe

Browse files
committed
Print dppx when x unit alias is unsupported
Fixes parcel-bundler#229
1 parent 6a2bc1d commit c8f2cfe

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

scripts/build-prefixes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ let mdnFeatures = {
246246
}
247247
})
248248
),
249-
imageSet: mdn.css.types.image['image-set'].__compat.support
249+
imageSet: mdn.css.types.image['image-set'].__compat.support,
250+
xResolutionUnit: mdn.css.types.resolution.x.__compat.support
250251
};
251252

252253
for (let feature in mdnFeatures) {

src/compat.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub enum Feature {
6666
SpaceSeparatedColorFunction,
6767
TextDecorationThicknessPercent,
6868
TextDecorationThicknessShorthand,
69+
XResolutionUnit,
6970
}
7071

7172
impl Feature {
@@ -2177,6 +2178,41 @@ impl Feature {
21772178
return false;
21782179
}
21792180
}
2181+
Feature::XResolutionUnit => {
2182+
if let Some(version) = browsers.chrome {
2183+
if version < 4456448 {
2184+
return false;
2185+
}
2186+
}
2187+
if let Some(version) = browsers.edge {
2188+
if version < 5177344 {
2189+
return false;
2190+
}
2191+
}
2192+
if let Some(version) = browsers.firefox {
2193+
if version < 4063232 {
2194+
return false;
2195+
}
2196+
}
2197+
if let Some(version) = browsers.opera {
2198+
if version < 3145728 {
2199+
return false;
2200+
}
2201+
}
2202+
if let Some(version) = browsers.samsung {
2203+
if version < 655360 {
2204+
return false;
2205+
}
2206+
}
2207+
if let Some(version) = browsers.android {
2208+
if version < 4456448 {
2209+
return false;
2210+
}
2211+
}
2212+
if browsers.ie.is_some() || browsers.ios_saf.is_some() || browsers.safari.is_some() {
2213+
return false;
2214+
}
2215+
}
21802216
Feature::P3Colors | Feature::LangList => {
21812217
if let Some(version) = browsers.safari {
21822218
if version < 655616 {

src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20102,4 +20102,49 @@ mod tests {
2010220102
"@foo{foo: bar;}",
2010320103
);
2010420104
}
20105+
20106+
#[test]
20107+
fn test_resolution() {
20108+
prefix_test(
20109+
r#"
20110+
@media (resolution: 1dppx) {
20111+
body {
20112+
background: red;
20113+
}
20114+
}
20115+
"#,
20116+
indoc! { r#"
20117+
@media (resolution: 1dppx) {
20118+
body {
20119+
background: red;
20120+
}
20121+
}
20122+
"#},
20123+
Browsers {
20124+
chrome: Some(50 << 16),
20125+
..Browsers::default()
20126+
},
20127+
);
20128+
20129+
prefix_test(
20130+
r#"
20131+
@media (resolution: 1dppx) {
20132+
body {
20133+
background: red;
20134+
}
20135+
}
20136+
"#,
20137+
indoc! { r#"
20138+
@media (resolution: 1x) {
20139+
body {
20140+
background: red;
20141+
}
20142+
}
20143+
"#},
20144+
Browsers {
20145+
chrome: Some(95 << 16),
20146+
..Browsers::default()
20147+
},
20148+
);
20149+
}
2010520150
}

src/values/image.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,13 @@ impl<'i> ImageSetOption<'i> {
480480

481481
if self.resolution != Resolution::Dppx(1.0) {
482482
dest.write_char(' ')?;
483+
484+
// Safari only supports the x resolution unit in image-set().
485+
// In other places, x was added as an alias later.
486+
// Temporarily ignore the targets while printing here.
487+
let targets = std::mem::take(&mut dest.targets);
483488
self.resolution.to_css(dest)?;
489+
dest.targets = targets;
484490
}
485491

486492
if let Some(file_type) = &self.file_type {

src/values/resolution.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use super::length::serialize_dimension;
44
use super::number::CSSNumber;
5+
use crate::compat::Feature;
56
use crate::error::{ParserError, PrinterError};
67
use crate::printer::Printer;
78
use crate::traits::{Parse, ToCss};
@@ -49,7 +50,17 @@ impl ToCss for Resolution {
4950
let (value, unit) = match self {
5051
Resolution::Dpi(dpi) => (*dpi, "dpi"),
5152
Resolution::Dpcm(dpcm) => (*dpcm, "dpcm"),
52-
Resolution::Dppx(dppx) => (*dppx, "x"),
53+
Resolution::Dppx(dppx) => {
54+
if let Some(targets) = dest.targets {
55+
if Feature::XResolutionUnit.is_compatible(targets) {
56+
(*dppx, "x")
57+
} else {
58+
(*dppx, "dppx")
59+
}
60+
} else {
61+
(*dppx, "x")
62+
}
63+
}
5364
};
5465

5566
serialize_dimension(value, unit, dest)

0 commit comments

Comments
 (0)