Skip to content

Commit

Permalink
Only reduce clamp if comparison between center and max is known
Browse files Browse the repository at this point in the history
Fixes #836
  • Loading branch information
devongovett committed Nov 3, 2024
1 parent 378955e commit ddc9ce8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7704,7 +7704,7 @@ mod tests {
);
minify_test(
".foo { border-width: clamp(1em, 2em, 4vh) }",
".foo{border-width:min(2em,4vh)}",
".foo{border-width:clamp(1em,2em,4vh)}",
);
minify_test(
".foo { border-width: clamp(1em, 2vh, 4vh) }",
Expand All @@ -7715,6 +7715,10 @@ mod tests {
".foo{border-width:clamp(1px,1px + 2em,4px)}",
);
minify_test(".foo { border-width: clamp(1px, 2pt, 1in) }", ".foo{border-width:2pt}");
minify_test(
".foo { width: clamp(-100px, 0px, 50% - 50vw); }",
".foo{width:clamp(-100px,0px,50% - 50vw)}",
);

minify_test(
".foo { top: calc(-1 * clamp(1.75rem, 8vw, 4rem)) }",
Expand Down
30 changes: 16 additions & 14 deletions src/values/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,24 @@ impl<
None => {}
}

let cmp = if let (Some(Calc::Value(min_val)), Calc::Value(center_val)) = (&min, &center) {
center_val.partial_cmp(&min_val)
} else {
None
};
if cmp.is_some() {
let cmp = if let (Some(Calc::Value(min_val)), Calc::Value(center_val)) = (&min, &center) {
center_val.partial_cmp(&min_val)
} else {
None
};

// If center is known to be less than the minimum, replace it with minimum and remove the min argument.
// Otherwise, if center is known to be greater than the minimum, remove the min argument.
match cmp {
Some(std::cmp::Ordering::Less) => {
center = std::mem::take(&mut min).unwrap();
}
Some(_) => {
min = None;
// If center is known to be less than the minimum, replace it with minimum and remove the min argument.
// Otherwise, if center is known to be greater than the minimum, remove the min argument.
match cmp {
Some(std::cmp::Ordering::Less) => {
center = std::mem::take(&mut min).unwrap();
}
Some(_) => {
min = None;
}
None => {}
}
None => {}
}

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

0 comments on commit ddc9ce8

Please sign in to comment.