Skip to content

Commit 1e7d0f9

Browse files
authored
fix: if the browser doesn't support #rrggbbaa color syntax, it is converted to transparent (#296)
1 parent 16c498b commit 1e7d0f9

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12801,6 +12801,67 @@ mod tests {
1280112801
minify_test(".foo { color: hwb(none none none) }", ".foo{color:red}");
1280212802
minify_test(".foo { color: rgb(none none none) }", ".foo{color:#000}");
1280312803

12804+
// If the browser doesn't support `#rrggbbaa` color syntax, it is converted to `transparent`.
12805+
attr_test(
12806+
"color: rgba(0, 0, 0, 0)",
12807+
"color:transparent",
12808+
true,
12809+
Some(Browsers {
12810+
chrome: Some(61 << 16), // Chrome >= 62 supports `#rrggbbaa` color.
12811+
..Browsers::default()
12812+
}),
12813+
);
12814+
12815+
attr_test(
12816+
"color: #0000",
12817+
"color:transparent",
12818+
true,
12819+
Some(Browsers {
12820+
chrome: Some(61 << 16), // Chrome >= 62 supports `#rrggbbaa` color.
12821+
..Browsers::default()
12822+
}),
12823+
);
12824+
12825+
attr_test(
12826+
"color: transparent",
12827+
"color:transparent",
12828+
true,
12829+
Some(Browsers {
12830+
chrome: Some(61 << 16),
12831+
..Browsers::default()
12832+
}),
12833+
);
12834+
12835+
attr_test(
12836+
"color: rgba(0, 0, 0, 0)",
12837+
"color: rgba(0, 0, 0, 0)",
12838+
false,
12839+
Some(Browsers {
12840+
chrome: Some(61 << 16),
12841+
..Browsers::default()
12842+
}),
12843+
);
12844+
12845+
attr_test(
12846+
"color: rgba(255, 0, 0, 0)",
12847+
"color:rgba(255,0,0,0)",
12848+
true,
12849+
Some(Browsers {
12850+
chrome: Some(61 << 16),
12851+
..Browsers::default()
12852+
}),
12853+
);
12854+
12855+
attr_test(
12856+
"color: rgba(255, 0, 0, 0)",
12857+
"color:#f000",
12858+
true,
12859+
Some(Browsers {
12860+
chrome: Some(62 << 16),
12861+
..Browsers::default()
12862+
}),
12863+
);
12864+
1280412865
prefix_test(
1280512866
".foo { color: rgba(123, 456, 789, 0.5) }",
1280612867
indoc! { r#"

src/values/color.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,24 +370,30 @@ impl ToCss for CssColor {
370370
// If the #rrggbbaa syntax is not supported by the browser targets, output rgba()
371371
if let Some(targets) = dest.targets {
372372
if !Feature::CssRrggbbaa.is_compatible(targets) {
373-
dest.write_str("rgba(")?;
374-
write!(dest, "{}", color.red)?;
375-
dest.delim(',', false)?;
376-
write!(dest, "{}", color.green)?;
377-
dest.delim(',', false)?;
378-
write!(dest, "{}", color.blue)?;
379-
dest.delim(',', false)?;
380-
381-
// Try first with two decimal places, then with three.
382-
let mut rounded_alpha = (color.alpha_f32() * 100.0).round() / 100.0;
383-
let clamped = (rounded_alpha * 255.0).round().max(0.).min(255.0) as u8;
384-
if clamped != color.alpha {
385-
rounded_alpha = (color.alpha_f32() * 1000.).round() / 1000.;
373+
// If the browser doesn't support `#rrggbbaa` color syntax, it is converted to `transparent` when compressed(minify = true).
374+
// https://www.w3.org/TR/css-color-4/#transparent-black
375+
if dest.minify && color.red == 0 && color.green == 0 && color.blue == 0 && color.alpha == 0 {
376+
return dest.write_str("transparent");
377+
} else {
378+
dest.write_str("rgba(")?;
379+
write!(dest, "{}", color.red)?;
380+
dest.delim(',', false)?;
381+
write!(dest, "{}", color.green)?;
382+
dest.delim(',', false)?;
383+
write!(dest, "{}", color.blue)?;
384+
dest.delim(',', false)?;
385+
386+
// Try first with two decimal places, then with three.
387+
let mut rounded_alpha = (color.alpha_f32() * 100.0).round() / 100.0;
388+
let clamped = (rounded_alpha * 255.0).round().max(0.).min(255.0) as u8;
389+
if clamped != color.alpha {
390+
rounded_alpha = (color.alpha_f32() * 1000.).round() / 1000.;
391+
}
392+
393+
rounded_alpha.to_css(dest)?;
394+
dest.write_char(')')?;
395+
return Ok(());
386396
}
387-
388-
rounded_alpha.to_css(dest)?;
389-
dest.write_char(')')?;
390-
return Ok(());
391397
}
392398
}
393399

0 commit comments

Comments
 (0)