Skip to content

Commit ed7012d

Browse files
fix(css): keep the digits Firefox lays a percentage out with (#22718)
* fix(css): keep the digits Firefox lays a percentage out with Six significant digits are calibrated on Blink's 1/64px layout. Gecko parses a percentage as a single-precision float and lays it out in 1/60px, so `calc(100% / 3)` printed as `33.3333%` moved a `translateX(100%)` by 0.03px in the Radix Themes benchmark. A percentage now keeps the fewest digits, six or more, that parse to the same float (`33.333333%`). An alpha value or `scale` written as a number rounds again as one. * test(css): update the percentage outputs the config cases record * fix(css): keep a percentage as written where no nine digits reach its float * fix(css): compare a percentage as the fraction Gecko stores Gecko parses `14.285714%` as the single-precision fraction 0.14285713, one float step from the 0.14285715 `calc(100% / 7)` computes, which moved a seven-segment Radix indicator by 0.1px. The digits are now chosen to reach the fraction's float, not the percentage's.
1 parent 221caaa commit ed7012d

14 files changed

Lines changed: 89 additions & 63 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep the digits Firefox lays out a minified CSS percentage with.

‎lib/css/syntax-parser.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8906,7 +8906,20 @@ const _roundSignificant = (num, unit = "") => {
89068906
const value = Number(num);
89078907
if (!Number.isFinite(value) || Math.abs(value) >= _ROUNDING_LIMIT) return num;
89088908
let rounded = value.toPrecision(digits);
8909-
if (digits === _SCALED_LENGTH_DIGITS) {
8909+
if (unit === "%") {
8910+
// Gecko parses a percentage as a single-precision fraction laid out in 1/60px,
8911+
// so the digits must reach that float: `14.285714%` drifts, `14.2857143%` not.
8912+
const parsed = Math.fround(value / 100);
8913+
for (
8914+
let more = digits;
8915+
more < _SCALED_LENGTH_DIGITS &&
8916+
Math.fround(Number(rounded) / 100) !== parsed;
8917+
more++
8918+
) {
8919+
rounded = value.toPrecision(more + 1);
8920+
}
8921+
if (Math.fround(Number(rounded) / 100) !== parsed) return num;
8922+
} else if (digits === _SCALED_LENGTH_DIGITS) {
89108923
// The fewest digits landing where the written length does, asked again of
89118924
// each answer until it holds, which is what a second pass would ask.
89128925
let from = value;

‎lib/css/syntax-printer.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,8 +3190,11 @@ const _numberAlphaValue = (property, value) => {
31903190
const parts = _LONE_PERCENTAGE_RE.exec(value);
31913191
if (parts === null) return value;
31923192
const digits = `00${parts[2]}`;
3193-
const shifted = _normalizeNumber(
3194-
`${parts[1]}${digits.slice(0, -2)}.${digits.slice(-2)}${parts[3] || ""}`
3193+
// A number rounds to fewer digits than the percentage it was printed as.
3194+
const shifted = _roundSignificant(
3195+
_normalizeNumber(
3196+
`${parts[1]}${digits.slice(0, -2)}.${digits.slice(-2)}${parts[3] || ""}`
3197+
)
31953198
);
31963199
return shifted.length < value.length ? shifted : value;
31973200
};
@@ -4486,7 +4489,7 @@ const _shortenIndividualTransform = (property, components) => {
44864489
for (let i = 0; i < kept.length; i++) {
44874490
const percentage = _PERCENTAGE_RE.exec(kept[i]);
44884491
if (percentage === null) continue;
4489-
const number = _shiftDecimal(percentage[1], -2);
4492+
const number = _roundSignificant(_shiftDecimal(percentage[1], -2));
44904493
if (number.length <= kept[i].length) kept[i] = number;
44914494
}
44924495
if (kept.length === 3 && Number(kept[2]) === 1) kept.pop();

‎test/configCases/css/minimize-corpus/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Array [
5151
"/*! declined: division by zero is not a number */.d05{width:calc(1px/0)}",
5252
"/*! declined: past the range the rounding covers every digit carries, and all of
5353
them are longer than the expression */.d06{width:calc(123456px/1.1)}",
54-
"/*! the fold prints a double back, so its result is rounded to six digits */.f20{width:33.3333%}",
54+
"/*! the fold prints a double back, so its result is rounded to six digits, a percentage to the float Firefox parses */.f20{width:33.333333%}",
5555
"/*! an angle rounds to six digits too, as lightningcss rounds it */.f21{rotate:.333333turn}",
5656
"/*! declined: a math function other than calc() */.d07{width:3px}",
5757
"/*! two angle units add in degrees, as lightningcss adds them */.d08{rotate:90deg}",
@@ -179,14 +179,14 @@ Array [
179179
"/*! width: calc(3 * 2px) — same as lightningcss: 6px */.k004{width:6px}",
180180
"/*! width: calc(10px / 4) — same as lightningcss: 2.5px */.k005{width:2.5px}",
181181
"/*! width: calc(10px / 3) — same as lightningcss: 3.33333px */.k006{width:3.33333px}",
182-
"/*! width: calc(100% / 3) — same as lightningcss: 33.3333% */.k007{width:33.3333%}",
182+
"/*! width: calc(100% / 3) — lightningcss: 33.3333% — ours keeps the digits Firefox lays a percentage out with */.k007{width:33.333333%}",
183183
"/*! width: calc(100% / 4) — same as lightningcss: 25% */.k008{width:25%}",
184-
"/*! width: calc(100% / 6) — same as lightningcss: 16.6667% */.k009{width:16.6667%}",
185-
"/*! width: calc(100% / 7) — same as lightningcss: 14.2857% */.k010{width:14.2857%}",
184+
"/*! width: calc(100% / 6) — lightningcss: 16.6667% — ours keeps the digits Firefox lays a percentage out with */.k009{width:16.666667%}",
185+
"/*! width: calc(100% / 7) — lightningcss: 14.2857% — ours keeps the digits Firefox lays a percentage out with */.k010{width:14.2857143%}",
186186
"/*! width: calc(100% / 8) — same as lightningcss: 12.5% */.k011{width:12.5%}",
187-
"/*! width: calc(100% / 9) — same as lightningcss: 11.1111% */.k012{width:11.1111%}",
188-
"/*! width: calc(100% / 12) — same as lightningcss: 8.33333% */.k013{width:8.33333%}",
189-
"/*! width: calc(100% * 2 / 3) — same as lightningcss: 66.6667% */.k014{width:66.6667%}",
187+
"/*! width: calc(100% / 9) — lightningcss: 11.1111% — ours keeps the digits Firefox lays a percentage out with */.k012{width:11.111111%}",
188+
"/*! width: calc(100% / 12) — lightningcss: 8.33333% — ours keeps the digits Firefox lays a percentage out with */.k013{width:8.3333333%}",
189+
"/*! width: calc(100% * 2 / 3) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k014{width:66.66667%}",
190190
"/*! width: calc(200px / 3) — same as lightningcss: 66.6667px */.k015{width:66.6667px}",
191191
"/*! width: calc(1px / 3) — same as lightningcss: .333333px */.k016{width:.333333px}",
192192
"/*! width: calc(-1px / 3) — lightningcss: -.333333px — a bare negative is invalid here, where calc() clamps it to 0 */.k017{width:calc(-1px/3)}",
@@ -225,7 +225,7 @@ Array [
225225
"/*! width: calc(100% - (10px - 5px)) — same as lightningcss: calc(100% - 5px) */.k050{width:calc(100% - 5px)}",
226226
"/*! width: calc(-1 * (100% - 10px)) — same as lightningcss: calc(-100% + 10px) */.k051{width:calc(-100% + 10px)}",
227227
"/*! width: calc(100% / 3 - 10px) — lightningcss: calc(33.3333% - 10px) — ours is shorter */.k052{width:calc(100%/3 - 10px)}",
228-
"/*! width: calc(100% / 3 + 100% / 3) — same as lightningcss: 66.6667% */.k053{width:66.6667%}",
228+
"/*! width: calc(100% / 3 + 100% / 3) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k053{width:66.66667%}",
229229
"/*! opacity: calc(1 / 3) — same as lightningcss: .333333 */.k054{opacity:.333333}",
230230
"/*! opacity: calc(1 / 2) — same as lightningcss: .5 */.k055{opacity:.5}",
231231
"/*! opacity: calc(0.5 + 0.25) — same as lightningcss: .75 */.k056{opacity:.75}",
@@ -309,20 +309,20 @@ Array [
309309
"/*! width: -moz-calc(1px + 2px) — same as lightningcss: -moz-calc(1px + 2px) */.k134{width:-moz-calc(1px + 2px)}",
310310
"/*! margin: calc(1px + 1px) calc(2px * 2) — same as lightningcss: 2px 4px */.k135{margin:2px 4px}",
311311
"/*! margin: calc(10px / 3) 0 — same as lightningcss: 3.33333px 0 */.k136{margin:3.33333px 0}",
312-
"/*! transform: translateX(calc(100% / 3)) — same as lightningcss: translate(33.3333%) */.k137{transform:translate(33.3333%)}",
313-
"/*! transform: translate(calc(10px + 5px), calc(100% / 3)) — same as lightningcss: translate(15px,33.3333%) */.k138{transform:translate(15px,33.3333%)}",
312+
"/*! transform: translateX(calc(100% / 3)) — lightningcss: translate(33.3333%) — ours keeps the digits Firefox lays a percentage out with */.k137{transform:translate(33.333333%)}",
313+
"/*! transform: translate(calc(10px + 5px), calc(100% / 3)) — lightningcss: translate(15px,33.3333%) — ours keeps the digits Firefox lays a percentage out with */.k138{transform:translate(15px,33.333333%)}",
314314
"/*! transform: rotate(calc(45deg * 2)) — same as lightningcss: rotate(90deg) */.k139{transform:rotate(90deg)}",
315315
"/*! transform: scale(calc(1 / 3)) — same as lightningcss: scale(.333333) */.k140{transform:scale(.333333)}",
316-
"/*! background-position: calc(100% / 3) calc(50% + 10px) — lightningcss: 33.3333% calc(50% + 10px) — the same value, ours dropping a space the tokens do not need */.k141{background-position:33.3333%calc(50% + 10px)}",
317-
"/*! grid-template-columns: repeat(3, calc(100% / 3)) — same as lightningcss: repeat(3,33.3333%) */.k142{grid-template-columns:repeat(3,33.3333%)}",
316+
"/*! background-position: calc(100% / 3) calc(50% + 10px) — lightningcss: 33.3333% calc(50% + 10px) — ours keeps the digits Firefox lays a percentage out with */.k141{background-position:33.333333%calc(50% + 10px)}",
317+
"/*! grid-template-columns: repeat(3, calc(100% / 3)) — lightningcss: repeat(3,33.3333%) — ours keeps the digits Firefox lays a percentage out with */.k142{grid-template-columns:repeat(3,33.333333%)}",
318318
"/*! box-shadow: calc(1px + 1px) 0 calc(4px / 3) #000 — lightningcss: 2px 0 1.33333px #000 — the same value, ours dropping a space the tokens do not need */.k143{box-shadow:2px 0 1.33333px#000}",
319319
"/*! color: rgb(calc(255 / 3) 0 0) — same as lightningcss: #500 */.k144{color:#500}",
320320
"/*! color: rgb(calc(100 + 55) 0 0) — same as lightningcss: #9b0000 */.k145{color:#9b0000}",
321321
"/*! color: hsl(calc(360 / 7) 50% 50%) — same as lightningcss: #bfad40 */.k146{color:#bfad40}",
322322
"/*! color: rgb(0 0 0 / calc(1 / 3)) — same as lightningcss: #0005 */.k147{color:#0005}",
323323
"/*! font: calc(10px + 2px)/1.5 serif — same as lightningcss: 12px/1.5 serif */.k148{font:12px/1.5 serif}",
324-
"/*! border-radius: calc(100% / 3) — same as lightningcss: 33.3333% */.k149{border-radius:33.3333%}",
325-
"/*! inset: calc(100% / 3) 0 0 0 — lightningcss: 33.3333% 0 0 — the same value, ours dropping a space the tokens do not need */.k150{inset:33.3333%0 0}",
324+
"/*! border-radius: calc(100% / 3) — lightningcss: 33.3333% — ours keeps the digits Firefox lays a percentage out with */.k149{border-radius:33.333333%}",
325+
"/*! inset: calc(100% / 3) 0 0 0 — lightningcss: 33.3333% 0 0 — ours keeps the digits Firefox lays a percentage out with */.k150{inset:33.333333%0 0}",
326326
"/*! width: calc(100% / 3 * 3) — same as lightningcss: 100% */.k151{width:100%}",
327327
"/*! width: calc(1px * 3 / 3) — same as lightningcss: 1px */.k152{width:1px}",
328328
"/*! width: calc(1 / 3 * 3px) — same as lightningcss: 1px */.k153{width:1px}",
@@ -348,7 +348,7 @@ Array [
348348
"/*! rotate: calc(1turn - 90deg) — same as lightningcss: 270deg */.k173{rotate:270deg}",
349349
"/*! aspect-ratio: 16 / 9 — same as lightningcss: 16/9 */.k174{aspect-ratio:16/9}",
350350
"/*! aspect-ratio: auto 16 / 9 — same as lightningcss: auto 16/9 */.k175{aspect-ratio:auto 16/9}",
351-
"/*! background: linear-gradient(red calc(100% / 3), blue) — lightningcss: linear-gradient(red 33.3333%,#00f) — the same length either way */.k176{background:linear-gradient(red 33.3333%,blue)}",
351+
"/*! background: linear-gradient(red calc(100% / 3), blue) — lightningcss: linear-gradient(red 33.3333%,#00f) — ours keeps the digits Firefox lays a percentage out with */.k176{background:linear-gradient(red 33.333333%,blue)}",
352352
]
353353
`;
354354
@@ -1537,7 +1537,7 @@ Array [
15371537
"/*! webpack: \`@container\` carries media features too, name and all */@container card (width>=1px){.t69{color:red}}",
15381538
"/*! declined: a plain feature has no range spelling to reach for */@media (width:100px){.t70{color:red}}",
15391539
"/*! declined: a \`@supports\` condition is a declaration, not a media feature */@supports (min-width:1px){.t71{color:red}}",
1540-
"/*! webpack: 6 significant digits is below what the engine reports */.t72{width:33.3333%}",
1540+
"/*! webpack: a percentage keeps the digits of the float Firefox parses */.t72{width:33.333333%}",
15411541
"/*! webpack: ...and below the 1/64px it lays a length out in */.t73{width:1px}",
15421542
"/*! webpack: ...for a unitless number too */.t74{opacity:.123457}",
15431543
"/*! webpack: declined: an angle is amplified by the trig in rotate() */.t75{transform:rotate(33.33333333deg)}",

0 commit comments

Comments
 (0)