Skip to content

Commit a218797

Browse files
committed
Handle multiplication by math functions
1 parent b46226e commit a218797

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,19 @@ mod tests {
25192519
minify_test(".foo { border-width: clamp(1px, 1px + 2em, 4px) }", ".foo{border-width:clamp(1px,1px + 2em,4px)}");
25202520
minify_test(".foo { border-width: clamp(1px, 2pt, 1in) }", ".foo{border-width:2pt}");
25212521

2522+
minify_test(".foo { top: calc(-1 * clamp(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(-1*clamp(1.75rem,8vw,4rem))}");
2523+
minify_test(".foo { top: calc(-1 * min(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(-1*min(1.75rem,8vw))}");
2524+
minify_test(".foo { top: calc(-1 * max(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(-1*max(4rem,8vw))}");
2525+
minify_test(".foo { top: calc(clamp(1.75rem, 8vw, 4rem) * -1) }", ".foo{top:calc(-1*clamp(1.75rem,8vw,4rem))}");
2526+
minify_test(".foo { top: calc(min(1.75rem, 8vw, 4rem) * -1) }", ".foo{top:calc(-1*min(1.75rem,8vw))}");
2527+
minify_test(".foo { top: calc(max(1.75rem, 8vw, 4rem) * -1) }", ".foo{top:calc(-1*max(4rem,8vw))}");
2528+
minify_test(".foo { top: calc(clamp(1.75rem, 8vw, 4rem) / 2) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem)/2)}");
2529+
minify_test(".foo { top: calc(min(1.75rem, 8vw, 4rem) / 2) }", ".foo{top:calc(min(1.75rem,8vw)/2)}");
2530+
minify_test(".foo { top: calc(max(1.75rem, 8vw, 4rem) / 2) }", ".foo{top:calc(max(4rem,8vw)/2)}");
2531+
minify_test(".foo { top: calc(0.5 * clamp(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem)/2)}");
2532+
minify_test(".foo { top: calc(1 * clamp(1.75rem, 8vw, 4rem)) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem))}");
2533+
minify_test(".foo { top: calc(2 * clamp(1.75rem, 8vw, 4rem) / 2) }", ".foo{top:calc(clamp(1.75rem,8vw,4rem))}");
2534+
25222535
prefix_test(
25232536
".foo { border-width: clamp(1em, 2px, 4vh) }",
25242537
indoc! { r#"

src/values/calc.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub enum Calc<V> {
7979
Value(Box<V>),
8080
Number(f32),
8181
Sum(Box<Calc<V>>, Box<Calc<V>>),
82+
Product(f32, Box<Calc<V>>),
8283
Function(Box<MathFunction<V>>)
8384
}
8485

@@ -311,14 +312,25 @@ impl<V: std::ops::Mul<f32, Output = V>> std::ops::Mul<f32> for Calc<V> {
311312
type Output = Self;
312313

313314
fn mul(self, other: f32) -> Self {
315+
if other == 1.0 {
316+
return self
317+
}
318+
314319
match self {
315320
Calc::Value(v) => Calc::Value(Box::new(*v * other)),
316321
Calc::Number(n) => Calc::Number(n * other),
317322
Calc::Sum(a, b) => Calc::Sum(Box::new(*a * other), Box::new(*b * other)),
323+
Calc::Product(num, calc) => {
324+
let num = num * other;
325+
if num == 1.0 {
326+
return *calc
327+
}
328+
Calc::Product(num, calc)
329+
},
318330
Calc::Function(f) => {
319331
match *f {
320332
MathFunction::Calc(c) => Calc::Function(Box::new(MathFunction::Calc(c * other))),
321-
_ => todo!()
333+
_ => Calc::Product(other, Box::new(Calc::Function(f)))
322334
}
323335
}
324336
}
@@ -377,7 +389,19 @@ impl<V: ToCss + std::cmp::PartialOrd<f32> + std::ops::Mul<f32, Output = V> + Clo
377389
b.to_css(dest)?;
378390
}
379391
Ok(())
380-
},
392+
}
393+
Calc::Product(num, calc) => {
394+
if num.abs() < 1.0 {
395+
let div = 1.0 / num;
396+
calc.to_css(dest)?;
397+
dest.delim('/', true)?;
398+
div.to_css(dest)
399+
} else {
400+
num.to_css(dest)?;
401+
dest.delim('*', true)?;
402+
calc.to_css(dest)
403+
}
404+
}
381405
Calc::Function(f) => f.to_css(dest)
382406
}
383407
}

0 commit comments

Comments
 (0)