Skip to content

Commit e9b6995

Browse files
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 e45e5b4 commit e9b6995

4 files changed

Lines changed: 21 additions & 18 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8907,17 +8907,18 @@ const _roundSignificant = (num, unit = "") => {
89078907
if (!Number.isFinite(value) || Math.abs(value) >= _ROUNDING_LIMIT) return num;
89088908
let rounded = value.toPrecision(digits);
89098909
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);
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);
89138913
for (
89148914
let more = digits;
8915-
more < _SCALED_LENGTH_DIGITS && Math.fround(Number(rounded)) !== parsed;
8915+
more < _SCALED_LENGTH_DIGITS &&
8916+
Math.fround(Number(rounded) / 100) !== parsed;
89168917
more++
89178918
) {
89188919
rounded = value.toPrecision(more + 1);
89198920
}
8920-
if (Math.fround(Number(rounded)) !== parsed) return num;
8921+
if (Math.fround(Number(rounded) / 100) !== parsed) return num;
89218922
} else if (digits === _SCALED_LENGTH_DIGITS) {
89228923
// The fewest digits landing where the written length does, asked again of
89238924
// each answer until it holds, which is what a second pass would ask.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ Array [
181181
"/*! width: calc(10px / 3) — same as lightningcss: 3.33333px */.k006{width:3.33333px}",
182182
"/*! 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) — lightningcss: 16.6667% — ours keeps the digits Firefox lays a percentage out with */.k009{width:16.6666667%}",
185-
"/*! width: calc(100% / 7) — lightningcss: 14.2857% — ours keeps the digits Firefox lays a percentage out with */.k010{width:14.285714%}",
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%}",
187187
"/*! 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.333333%}",
189-
"/*! width: calc(100% * 2 / 3) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k014{width:66.666667%}",
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) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k053{width:66.666667%}",
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}",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ Array [
181181
"/*! width: calc(10px / 3) — same as lightningcss: 3.33333px */.k006{width:3.33333px}",
182182
"/*! 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) — lightningcss: 16.6667% — ours keeps the digits Firefox lays a percentage out with */.k009{width:16.6666667%}",
185-
"/*! width: calc(100% / 7) — lightningcss: 14.2857% — ours keeps the digits Firefox lays a percentage out with */.k010{width:14.285714%}",
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%}",
187187
"/*! 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.333333%}",
189-
"/*! width: calc(100% * 2 / 3) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k014{width:66.666667%}",
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) — lightningcss: 66.6667% — ours keeps the digits Firefox lays a percentage out with */.k053{width:66.666667%}",
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}",

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,7 +4158,7 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
41584158
it.each([
41594159
["33.33333333%", "33.333333%"],
41604160
// No nine digits reach its float, so it stays as written.
4161-
["0.00032022017693075414%", ".00032022017693075414%"],
4161+
["10.1885002107%", "10.1885002107%"],
41624162
["1.0000001px", "1px"],
41634163
["400ms", ".4s"],
41644164
[".005s", "5ms"],
@@ -5193,9 +5193,9 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
51935193
settled("a{color:r\\67 b(67.2549019608%, 66.862745098%, 91.9607843137%)}")
51945194
).toBe("a{color:rgb(67.2549019608%,66.862745098%,91.9607843137%)}");
51955195
// Only a channel reads the percentage as a byte: a length still rounds.
5196-
expect(settled("a{width:66.862745098%}")).toBe("a{width:66.86275%}");
5196+
expect(settled("a{width:66.862745098%}")).toBe("a{width:66.862745%}");
51975197
expect(settled(".a{flex:0 0 16.66666667%;opacity:16.66666667%}")).toBe(
5198-
".a{opacity:.166667;flex:0 0 16.6666667%}"
5198+
".a{opacity:.166667;flex:0 0 16.666667%}"
51995199
);
52005200
expect(settled("a{scale:16.66666667%}")).toBe("a{scale:.166667}");
52015201
// An exact half rounds up in every engine.
@@ -8583,6 +8583,8 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
85838583
["calc(3px/1.1)", "2.72727px"],
85848584
// ...and a percentage up to the single-precision float Gecko parses.
85858585
["calc(100%/3)", "33.333333%"],
8586+
// Gecko keeps the fraction, which `14.285714%` misses by one float step.
8587+
["calc(100%/7)", "14.2857143%"],
85868588
["calc(1/3*1px)", ".333333px"],
85878589
["calc((6/10 - .375)*1em)", ".225em"],
85888590
// ...and up to nine where the engine scales the length by a font size.

0 commit comments

Comments
 (0)