Skip to content

Commit d5d73c9

Browse files
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.
1 parent 755c49e commit d5d73c9

5 files changed

Lines changed: 31 additions & 11 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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8906,7 +8906,18 @@ 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 float laid out in 1/60px,
8911+
// so the digits must reach that float: `33.3333%` drifts, `33.333333%` not.
8912+
const parsed = Math.fround(value);
8913+
for (
8914+
let more = digits;
8915+
more < _SCALED_LENGTH_DIGITS && Math.fround(Number(rounded)) !== parsed;
8916+
more++
8917+
) {
8918+
rounded = value.toPrecision(more + 1);
8919+
}
8920+
} else if (digits === _SCALED_LENGTH_DIGITS) {
89108921
// The fewest digits landing where the written length does, asked again of
89118922
// each answer until it holds, which is what a second pass would ask.
89128923
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/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ const FILED_CONFIG_HTML_DEFECTS = new Map([
210210

211211
const FILED_BENCHMARK_CSS_DEFECTS = new Map([
212212
["UIkit 3", "firefox only: not a printer defect — `rgba(255, 255, 255, 0.7)` prints as `#ffffffb3` (lightningcss writes the same), whose alpha is 179/255. Blink stores alpha in a byte either way, while Gecko keeps 0.702 and its `::placeholder` mix lands one byte from the source's 0.378"],
213-
["Radix Themes 3 (components)", "firefox only: `calc(100% / 3)` prints as `33.3333%`, six digits being calibrated on Blink's 1/64px layout; Gecko lays out in 1/60px, so a `translateX(100%)` over that width moves by 0.03px"],
214213
]);
215214

216215
const FILED_BENCHMARK_HTML_DEFECTS = new Map();

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,7 +4156,7 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
41564156

41574157
describe("rounding and unit conversion", () => {
41584158
it.each([
4159-
["33.33333333%", "33.3333%"],
4159+
["33.33333333%", "33.333333%"],
41604160
["1.0000001px", "1px"],
41614161
["400ms", ".4s"],
41624162
[".005s", "5ms"],
@@ -5013,9 +5013,9 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
50135013
expect(settled("a{width:.12499999em}")).toBe("a{width:.12499999em}");
50145014
// 13/252em at 63px: `.0515873em` is 3.25px only until parsed to a float.
50155015
expect(settled("a{width:.05158730159em}")).toBe("a{width:.051587302em}");
5016-
// A pixel, a percentage and a bare number round as before.
5016+
// A pixel and a bare number round as before, a percentage to its float.
50175017
expect(settled("a{width:1.0526315789px}")).toBe("a{width:1.05263px}");
5018-
expect(settled("a{width:33.33333333%}")).toBe("a{width:33.3333%}");
5018+
expect(settled("a{width:33.33333333%}")).toBe("a{width:33.333333%}");
50195019
expect(settled("a{opacity:.3333333333}")).toBe("a{opacity:.333333}");
50205020
});
50215021

@@ -5191,10 +5191,11 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
51915191
settled("a{color:r\\67 b(67.2549019608%, 66.862745098%, 91.9607843137%)}")
51925192
).toBe("a{color:rgb(67.2549019608%,66.862745098%,91.9607843137%)}");
51935193
// Only a channel reads the percentage as a byte: a length still rounds.
5194-
expect(settled("a{width:66.862745098%}")).toBe("a{width:66.8627%}");
5194+
expect(settled("a{width:66.862745098%}")).toBe("a{width:66.86275%}");
51955195
expect(settled(".a{flex:0 0 16.66666667%;opacity:16.66666667%}")).toBe(
5196-
".a{opacity:.166667;flex:0 0 16.6667%}"
5196+
".a{opacity:.166667;flex:0 0 16.6666667%}"
51975197
);
5198+
expect(settled("a{scale:16.66666667%}")).toBe("a{scale:.166667}");
51985199
// An exact half rounds up in every engine.
51995200
expect(settled("a{color:rgba(50%,50%,50%,.5)}")).toBe(
52005201
"a{color:#80808080}"
@@ -8578,7 +8579,8 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
85788579
// The fold prints a double back, so its result is rounded the way an
85798580
// authored number is — six significant digits.
85808581
["calc(3px/1.1)", "2.72727px"],
8581-
["calc(100%/3)", "33.3333%"],
8582+
// ...and a percentage up to the single-precision float Gecko parses.
8583+
["calc(100%/3)", "33.333333%"],
85828584
["calc(1/3*1px)", ".333333px"],
85838585
["calc((6/10 - .375)*1em)", ".225em"],
85848586
// ...and up to nine where the engine scales the length by a font size.

0 commit comments

Comments
 (0)