Skip to content

Commit ac660f1

Browse files
committed
Don't serialize unitless zero inside calc() expressions
1 parent a218797 commit ac660f1

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,8 @@ mod tests {
25322532
minify_test(".foo { top: calc(1 * clamp(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem))}");
25332533
minify_test(".foo { top: calc(2 * clamp(1.75rem, 8vw, 4rem) / 2) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem))}");
25342534

2535+
minify_test(".foo { width: max(0px, 1vw) }", ".foo{width:max(0px,1vw)}");
2536+
25352537
prefix_test(
25362538
".foo { border-width: clamp(1em, 2px, 4vh) }",
25372539
indoc! { r#"

src/printer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub struct Printer<'a, W> {
1414
pub targets: Option<Browsers>,
1515
/// Vendor prefix override. When non-empty, it overrides
1616
/// the vendor prefix of whatever is being printed.
17-
pub vendor_prefix: VendorPrefix
17+
pub vendor_prefix: VendorPrefix,
18+
pub in_calc: bool
1819
}
1920

2021
impl<'a, W: Write + Sized> Printer<'a, W> {
@@ -32,7 +33,8 @@ impl<'a, W: Write + Sized> Printer<'a, W> {
3233
col: 0,
3334
minify,
3435
targets,
35-
vendor_prefix: VendorPrefix::empty()
36+
vendor_prefix: VendorPrefix::empty(),
37+
in_calc: false
3638
}
3739
}
3840

src/values/calc.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ impl<V: std::cmp::PartialOrd<f32>> std::cmp::PartialOrd<f32> for Calc<V> {
373373

374374
impl<V: ToCss + std::cmp::PartialOrd<f32> + std::ops::Mul<f32, Output = V> + Clone + std::fmt::Debug> ToCss for Calc<V> {
375375
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
376-
match self {
376+
let was_in_calc = dest.in_calc;
377+
dest.in_calc = true;
378+
379+
let res = match self {
377380
Calc::Value(v) => v.to_css(dest),
378381
Calc::Number(n) => serialize_number(*n, dest),
379382
Calc::Sum(a, b) => {
@@ -383,12 +386,11 @@ impl<V: ToCss + std::cmp::PartialOrd<f32> + std::ops::Mul<f32, Output = V> + Clo
383386
if *b < 0.0 {
384387
dest.write_str(" - ")?;
385388
let b = b.clone() * -1.0;
386-
b.to_css(dest)?;
389+
b.to_css(dest)
387390
} else {
388391
dest.write_str(" + ")?;
389-
b.to_css(dest)?;
392+
b.to_css(dest)
390393
}
391-
Ok(())
392394
}
393395
Calc::Product(num, calc) => {
394396
if num.abs() < 1.0 {
@@ -403,6 +405,9 @@ impl<V: ToCss + std::cmp::PartialOrd<f32> + std::ops::Mul<f32, Output = V> + Clo
403405
}
404406
}
405407
Calc::Function(f) => f.to_css(dest)
406-
}
408+
};
409+
410+
dest.in_calc = was_in_calc;
411+
res
407412
}
408413
}

src/values/length.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ impl Parse for LengthValue {
112112
impl ToCss for LengthValue {
113113
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
114114
let (value, unit) = self.to_unit_value();
115-
if value == 0.0 {
115+
116+
// The unit can be omitted if the value is zero, except inside calc()
117+
// expressions, where unitless numbers won't be parsed as dimensions.
118+
if !dest.in_calc && value == 0.0 {
116119
return dest.write_char('0')
117120
}
118121

0 commit comments

Comments
 (0)