Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/css/values/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,15 +930,21 @@ impl<V: CalcValue> Calc<V> {
let calc = expression;
if num.abs() < 1.0 {
let div = 1.0 / num;
calc.to_css(dest)?;
dest.delim(b'/', true)?;
CSSNumberFns::to_css(div, dest)?;
if div.is_finite() {
calc.to_css(dest)?;
dest.delim(b'/', true)?;
return CSSNumberFns::to_css(div, dest);
Comment thread
robobun marked this conversation as resolved.
Resolved
}
// An infinite 1/num prints as 3.40282e38, so multiply (lightningcss divides).
if num < 0.0 {
dest.write_char(b'-')?; // https://github.com/oven-sh/bun/pull/38516
}
CSSNumberFns::to_css(num.abs(), dest)?;
Comment thread
robobun marked this conversation as resolved.
Resolved
} else {
CSSNumberFns::to_css(num, dest)?;
dest.delim(b'*', true)?;
calc.to_css(dest)?;
}
Ok(())
dest.delim(b'*', true)?;
calc.to_css(dest)
}
Calc::Function(f) => f.to_css(dest),
}
Expand Down
61 changes: 61 additions & 0 deletions test/js/bun/css/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,67 @@ describe("css tests", () => {
minify_test(`a { opacity: calc(NaN) }`, `a{opacity:0}`);
minify_test(`a { rotate: calc(NaN * 1deg) }`, `a{rotate:0deg}`);
minify_test(`a { transition-duration: calc(NaN * 1s) }`, `a{transition-duration:0s}`);

// A factor whose reciprocal is infinite (zero, or a subnormal up to 2^-128) prints as a multiplication.
const minify_twice_test = (source: string, expected: string) =>
test(source, () => {
const once = minify_test_with_options(source, "");
expect(once).toEqual(expected);
expect(minify_test_with_options(once, "")).toEqual(once);
});
for (const product of [
`min(1px,1em) * 0`,
`0 * min(1px,1em)`,
`min(1px,1em) * -0`,
`min(1px,1em) / 2 * 0`,
`min(1px,1em) * 2 * 0`,
`min(1px,1em) * (1 - 1)`,
`min(1px,1em) / infinity`,
`min(1px,1em) * 1e-50`,
`min(1px,1em) * 1e-30 * 1e-30`,
]) {
minify_twice_test(`a { width: calc(${product}) }`, `a{width:calc(0*min(1px,1em))}`);
}
minify_twice_test(`a { border-width: calc(min(1px,1em) * 0) }`, `a{border-width:calc(0*min(1px,1em))}`);
minify_twice_test(`a { width: calc(5px + min(1px,1em) * 0) }`, `a{width:calc(5px + 0*min(1px,1em))}`);
minify_twice_test(`a { width: calc(5px - min(1px,1em) * 0) }`, `a{width:calc(5px + 0*min(1px,1em))}`);
// `mod(1em, 0px)` is NaN in a browser, so this sum is 0px there. A product folded to zero makes it 5px.
minify_twice_test(`a { width: calc(5px + mod(1em, 0px) * 0) }`, `a{width:calc(5px + 0*mod(1em,0px))}`);
minify_twice_test(`a { width: min(1px, max(1px,1em) * 0) }`, `a{width:min(1px,0*max(1px,1em))}`);
minify_twice_test(`a { width: calc(clamp(1px,1em,2vw) * 0) }`, `a{width:calc(0*clamp(1px,1em,2vw))}`);
minify_twice_test(`a { width: calc(round(1rem, 10px) * 0) }`, `a{width:calc(0*round(1rem,10px))}`);
minify_twice_test(`a { width: calc(min(1px,10%) * 0) }`, `a{width:calc(0*min(1px,10%))}`);
minify_twice_test(
`a { background: conic-gradient(red calc(min(1deg,1%) * 0), blue) }`,
`a{background:conic-gradient(red calc(0*min(1deg,1%)),#00f)}`,
);
minify_twice_test(
`@media (width > calc(min(1px,1em) * 0)) { a { color: red } }`,
`@media (width>calc(0*min(1px,1em))){a{color:red}}`,
);
minify_twice_test(`a { width: calc(min(1px,1em) * 2.5e-39) }`, `a{width:calc(2.5e-39*min(1px,1em))}`);
minify_twice_test(`a { width: calc(min(1px,1em) * -2.5e-39) }`, `a{width:calc(-2.5e-39*min(1px,1em))}`);
cssTest(`.foo { width: calc(min(1px, 1em) * 0) }`, `.foo { width: calc(0 * min(1px, 1em)); }`);
// A factor with a finite reciprocal prints as a division below 1 and as a multiplication from 1.
minify_twice_test(`a { width: calc(min(1px,1em) * -.5) }`, `a{width:calc(min(1px,1em)/-2)}`);
minify_twice_test(`a { width: calc(min(1px,1em) * 2) }`, `a{width:calc(2*min(1px,1em))}`);
// The product cases lightningcss asserts in its calc tests.
for (const [source, expected] of [
[`calc(-1 * clamp(1.75rem, 8vw, 4rem))`, `calc(-1*clamp(1.75rem,8vw,4rem))`],
[`calc(-1 * min(1.75rem, 8vw, 4rem))`, `calc(-1*min(1.75rem,8vw))`],
[`calc(-1 * max(1.75rem, 8vw, 4rem))`, `calc(-1*max(4rem,8vw))`],
[`calc(clamp(1.75rem, 8vw, 4rem) * -1)`, `calc(-1*clamp(1.75rem,8vw,4rem))`],
[`calc(min(1.75rem, 8vw, 4rem) * -1)`, `calc(-1*min(1.75rem,8vw))`],
[`calc(max(1.75rem, 8vw, 4rem) * -1)`, `calc(-1*max(4rem,8vw))`],
[`calc(clamp(1.75rem, 8vw, 4rem) / 2)`, `calc(clamp(1.75rem,8vw,4rem)/2)`],
[`calc(min(1.75rem, 8vw, 4rem) / 2)`, `calc(min(1.75rem,8vw)/2)`],
[`calc(max(1.75rem, 8vw, 4rem) / 2)`, `calc(max(4rem,8vw)/2)`],
[`calc(0.5 * clamp(1.75rem, 8vw, 4rem))`, `calc(clamp(1.75rem,8vw,4rem)/2)`],
[`calc(1 * clamp(1.75rem, 8vw, 4rem))`, `calc(clamp(1.75rem,8vw,4rem))`],
[`calc(2 * clamp(1.75rem, 8vw, 4rem) / 2)`, `calc(clamp(1.75rem,8vw,4rem))`],
] as const) {
minify_twice_test(`.foo { top: ${source} }`, `.foo{top:${expected}}`);
}
});
describe("clamp() simplification", () => {
// The cases lightningcss asserts in its calc tests.
Expand Down
Loading