Skip to content

Commit

Permalink
fix minifying predefined color spaces
Browse files Browse the repository at this point in the history
fixes #727
  • Loading branch information
devongovett committed May 14, 2024
1 parent 7c34ba5 commit 445def9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
31 changes: 17 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16040,11 +16040,11 @@ mod tests {
);
minify_test(
".foo { color: color(display-p3 1 0.5 0); }",
".foo{color:color(display-p3 1 .5)}",
".foo{color:color(display-p3 1 .5 0)}",
);
minify_test(
".foo { color: color(display-p3 100% 50% 0%); }",
".foo{color:color(display-p3 1 .5)}",
".foo{color:color(display-p3 1 .5 0)}",
);
minify_test(
".foo { color: color(xyz-d50 0.2005 0.14089 0.4472); }",
Expand All @@ -16070,24 +16070,27 @@ mod tests {
".foo { color: color(xyz 20.05% 14.089% 44.72%); }",
".foo{color:color(xyz .2005 .14089 .4472)}",
);
minify_test(".foo { color: color(xyz 0.2005 0 0); }", ".foo{color:color(xyz .2005)}");
minify_test(".foo { color: color(xyz 0 0 0); }", ".foo{color:color(xyz)}");
minify_test(".foo { color: color(xyz 0 1 0); }", ".foo{color:color(xyz 0 1)}");
minify_test(".foo { color: color(xyz 0 1); }", ".foo{color:color(xyz 0 1)}");
minify_test(".foo { color: color(xyz 1); }", ".foo{color:color(xyz 1)}");
minify_test(".foo { color: color(xyz); }", ".foo{color:color(xyz)}");
minify_test(
".foo { color: color(xyz 0.2005 0 0); }",
".foo{color:color(xyz .2005 0 0)}",
);
minify_test(".foo { color: color(xyz 0 0 0); }", ".foo{color:color(xyz 0 0 0)}");
minify_test(".foo { color: color(xyz 0 1 0); }", ".foo{color:color(xyz 0 1 0)}");
minify_test(
".foo { color: color(xyz 0 1 0 / 20%); }",
".foo{color:color(xyz 0 1/.2)}",
".foo{color:color(xyz 0 1 0/.2)}",
);
minify_test(
".foo { color: color(xyz 0 0 0 / 20%); }",
".foo{color:color(xyz 0 0 0/.2)}",
);
minify_test(".foo { color: color(xyz / 20%); }", ".foo{color:color(xyz/.2)}");
minify_test(
".foo { color: color(display-p3 100% 50% 0 / 20%); }",
".foo{color:color(display-p3 1 .5/.2)}",
".foo{color:color(display-p3 1 .5 0/.2)}",
);
minify_test(
".foo { color: color(display-p3 100% / 20%); }",
".foo{color:color(display-p3 1/.2)}",
".foo { color: color(display-p3 100% 0 0 / 20%); }",
".foo{color:color(display-p3 1 0 0/.2)}",
);
minify_test(".foo { color: hsl(none none none) }", ".foo{color:#000}");
minify_test(".foo { color: hwb(none none none) }", ".foo{color:red}");
Expand Down Expand Up @@ -20034,7 +20037,7 @@ mod tests {
".foo {{ color: color-mix(in {0}, color({0} -2 -3 -4 / -5), color({0} -4 -6 -8 / -10)) }}",
color_space
),
&format!(".foo{{color:color({}/0)}}", result_color_space),
&format!(".foo{{color:color({} 0 0 0/0)}}", result_color_space),
);

minify_test(
Expand Down
12 changes: 10 additions & 2 deletions src/properties/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,11 @@ impl ToCss for Perspective {
/// A value for the [translate](https://drafts.csswg.org/css-transforms-2/#propdef-translate) property.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "lowercase"))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum Translate {
Expand Down Expand Up @@ -1623,7 +1627,11 @@ impl Rotate {
/// A value for the [scale](https://drafts.csswg.org/css-transforms-2/#propdef-scale) property.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "lowercase"))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub enum Scale {
Expand Down
30 changes: 9 additions & 21 deletions src/values/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,15 +1169,9 @@ fn parse_predefined_relative<'i, 't>(

// Out of gamut values should not be clamped, i.e. values < 0 or > 1 should be preserved.
// The browser will gamut-map the color for the target device that it is rendered on.
let a = input
.try_parse(|input| parse_number_or_percentage(input, parser))
.unwrap_or(0.0);
let b = input
.try_parse(|input| parse_number_or_percentage(input, parser))
.unwrap_or(0.0);
let c = input
.try_parse(|input| parse_number_or_percentage(input, parser))
.unwrap_or(0.0);
let a = input.try_parse(|input| parse_number_or_percentage(input, parser))?;
let b = input.try_parse(|input| parse_number_or_percentage(input, parser))?;
let c = input.try_parse(|input| parse_number_or_percentage(input, parser))?;
let alpha = parse_alpha(input, parser)?;

let res = match_ignore_ascii_case! { &*&colorspace,
Expand Down Expand Up @@ -1429,18 +1423,12 @@ where

dest.write_str("color(")?;
dest.write_str(name)?;
if !dest.minify || a != 0.0 || b != 0.0 || c != 0.0 {
dest.write_char(' ')?;
write_component(a, dest)?;
if !dest.minify || b != 0.0 || c != 0.0 {
dest.write_char(' ')?;
write_component(b, dest)?;
if !dest.minify || c != 0.0 {
dest.write_char(' ')?;
write_component(c, dest)?;
}
}
}
dest.write_char(' ')?;
write_component(a, dest)?;
dest.write_char(' ')?;
write_component(b, dest)?;
dest.write_char(' ')?;
write_component(c, dest)?;

if alpha.is_nan() || (alpha - 1.0).abs() > f32::EPSILON {
dest.delim('/', true)?;
Expand Down

0 comments on commit 445def9

Please sign in to comment.