Skip to content

Commit ddc9ce8

Browse files
committed
Only reduce clamp if comparison between center and max is known
Fixes #836
1 parent 378955e commit ddc9ce8

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7704,7 +7704,7 @@ mod tests {
77047704
);
77057705
minify_test(
77067706
".foo { border-width: clamp(1em, 2em, 4vh) }",
7707-
".foo{border-width:min(2em,4vh)}",
7707+
".foo{border-width:clamp(1em,2em,4vh)}",
77087708
);
77097709
minify_test(
77107710
".foo { border-width: clamp(1em, 2vh, 4vh) }",
@@ -7715,6 +7715,10 @@ mod tests {
77157715
".foo{border-width:clamp(1px,1px + 2em,4px)}",
77167716
);
77177717
minify_test(".foo { border-width: clamp(1px, 2pt, 1in) }", ".foo{border-width:2pt}");
7718+
minify_test(
7719+
".foo { width: clamp(-100px, 0px, 50% - 50vw); }",
7720+
".foo{width:clamp(-100px,0px,50% - 50vw)}",
7721+
);
77187722

77197723
minify_test(
77207724
".foo { top: calc(-1 * clamp(1.75rem, 8vw, 4rem)) }",

src/values/calc.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -400,22 +400,24 @@ impl<
400400
None => {}
401401
}
402402

403-
let cmp = if let (Some(Calc::Value(min_val)), Calc::Value(center_val)) = (&min, &center) {
404-
center_val.partial_cmp(&min_val)
405-
} else {
406-
None
407-
};
403+
if cmp.is_some() {
404+
let cmp = if let (Some(Calc::Value(min_val)), Calc::Value(center_val)) = (&min, &center) {
405+
center_val.partial_cmp(&min_val)
406+
} else {
407+
None
408+
};
408409

409-
// If center is known to be less than the minimum, replace it with minimum and remove the min argument.
410-
// Otherwise, if center is known to be greater than the minimum, remove the min argument.
411-
match cmp {
412-
Some(std::cmp::Ordering::Less) => {
413-
center = std::mem::take(&mut min).unwrap();
414-
}
415-
Some(_) => {
416-
min = None;
410+
// If center is known to be less than the minimum, replace it with minimum and remove the min argument.
411+
// Otherwise, if center is known to be greater than the minimum, remove the min argument.
412+
match cmp {
413+
Some(std::cmp::Ordering::Less) => {
414+
center = std::mem::take(&mut min).unwrap();
415+
}
416+
Some(_) => {
417+
min = None;
418+
}
419+
None => {}
417420
}
418-
None => {}
419421
}
420422

421423
// Generate clamp(), min(), max(), or value depending on which arguments are left.

0 commit comments

Comments
 (0)