Skip to content

Commit

Permalink
Print dppx when x unit alias is unsupported
Browse files Browse the repository at this point in the history
Fixes #229
  • Loading branch information
devongovett committed Aug 22, 2022
1 parent 6a2bc1d commit c8f2cfe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
3 changes: 2 additions & 1 deletion scripts/build-prefixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ let mdnFeatures = {
}
})
),
imageSet: mdn.css.types.image['image-set'].__compat.support
imageSet: mdn.css.types.image['image-set'].__compat.support,
xResolutionUnit: mdn.css.types.resolution.x.__compat.support
};

for (let feature in mdnFeatures) {
Expand Down
36 changes: 36 additions & 0 deletions src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum Feature {
SpaceSeparatedColorFunction,
TextDecorationThicknessPercent,
TextDecorationThicknessShorthand,
XResolutionUnit,
}

impl Feature {
Expand Down Expand Up @@ -2177,6 +2178,41 @@ impl Feature {
return false;
}
}
Feature::XResolutionUnit => {
if let Some(version) = browsers.chrome {
if version < 4456448 {
return false;
}
}
if let Some(version) = browsers.edge {
if version < 5177344 {
return false;
}
}
if let Some(version) = browsers.firefox {
if version < 4063232 {
return false;
}
}
if let Some(version) = browsers.opera {
if version < 3145728 {
return false;
}
}
if let Some(version) = browsers.samsung {
if version < 655360 {
return false;
}
}
if let Some(version) = browsers.android {
if version < 4456448 {
return false;
}
}
if browsers.ie.is_some() || browsers.ios_saf.is_some() || browsers.safari.is_some() {
return false;
}
}
Feature::P3Colors | Feature::LangList => {
if let Some(version) = browsers.safari {
if version < 655616 {
Expand Down
45 changes: 45 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20102,4 +20102,49 @@ mod tests {
"@foo{foo: bar;}",
);
}

#[test]
fn test_resolution() {
prefix_test(
r#"
@media (resolution: 1dppx) {
body {
background: red;
}
}
"#,
indoc! { r#"
@media (resolution: 1dppx) {
body {
background: red;
}
}
"#},
Browsers {
chrome: Some(50 << 16),
..Browsers::default()
},
);

prefix_test(
r#"
@media (resolution: 1dppx) {
body {
background: red;
}
}
"#,
indoc! { r#"
@media (resolution: 1x) {
body {
background: red;
}
}
"#},
Browsers {
chrome: Some(95 << 16),
..Browsers::default()
},
);
}
}
6 changes: 6 additions & 0 deletions src/values/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,13 @@ impl<'i> ImageSetOption<'i> {

if self.resolution != Resolution::Dppx(1.0) {
dest.write_char(' ')?;

// Safari only supports the x resolution unit in image-set().
// In other places, x was added as an alias later.
// Temporarily ignore the targets while printing here.
let targets = std::mem::take(&mut dest.targets);
self.resolution.to_css(dest)?;
dest.targets = targets;
}

if let Some(file_type) = &self.file_type {
Expand Down
13 changes: 12 additions & 1 deletion src/values/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use super::length::serialize_dimension;
use super::number::CSSNumber;
use crate::compat::Feature;
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::traits::{Parse, ToCss};
Expand Down Expand Up @@ -49,7 +50,17 @@ impl ToCss for Resolution {
let (value, unit) = match self {
Resolution::Dpi(dpi) => (*dpi, "dpi"),
Resolution::Dpcm(dpcm) => (*dpcm, "dpcm"),
Resolution::Dppx(dppx) => (*dppx, "x"),
Resolution::Dppx(dppx) => {
if let Some(targets) = dest.targets {
if Feature::XResolutionUnit.is_compatible(targets) {
(*dppx, "x")
} else {
(*dppx, "dppx")
}
} else {
(*dppx, "x")
}
}
};

serialize_dimension(value, unit, dest)
Expand Down

0 comments on commit c8f2cfe

Please sign in to comment.