Skip to content

Commit 16092ba

Browse files
feat(css): shorten the numbers of a @supports condition
Drop leading and trailing zeros there as in a value, but keep a written fraction and sign, so the tested token stream reads the same.
1 parent d77b3d4 commit 16092ba

3 files changed

Lines changed: 39 additions & 20 deletions

File tree

‎lib/css/syntax-printer.js‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,9 @@ const _EXPONENT_LIKE_UNIT_RE = /^e[+-]?\d/i;
469469

470470
/**
471471
* Normalize a number / dimension / percentage token: normalize the numeric part,
472-
* then round it and reach for a shorter equal unit. Neither is done inside a
473-
* `@supports` prelude, where the declaration is being tested rather than applied
474-
* — an engine may read `px` and not `pc`. Angles keep every digit: `rotate()`
475-
* runs its argument through trig, which amplifies a truncated one.
472+
* then round it and reach for a shorter equal unit. A `@supports` test gets the
473+
* spelling alone and keeps a written fraction, since `z-index:1.0` is no integer.
474+
* Angles keep every digit: `rotate()` runs its argument through trig.
476475
* @param {string} text the token's source text
477476
* @returns {string} the normalized token
478477
*/
@@ -502,6 +501,11 @@ const _normalizeNumericToken = (text) => {
502501
// itself wherever nothing below changed a part of it.
503502
const numText = end === text.length ? text : text.slice(0, end);
504503
let num = _normalizeNumber(numText);
504+
// A tested `calc(1px+2px)` holds `+2px`: dropping its sign would part it into
505+
// two terms, a different token stream under test.
506+
if (_S._inSupportsPrelude && text.charCodeAt(0) === CC_PLUS_SIGN) {
507+
num = `+${num}`;
508+
}
505509
// `_normalizeNumber` hands an exponent back as written; a tested or
506510
// substituted one stays so, and elsewhere an unknown context may read `<integer>`.
507511
// A unit like `e3` would read as the exponent of a number written plainly.
@@ -523,14 +527,13 @@ const _normalizeNumericToken = (text) => {
523527
// (`grid-row:1.0` computes `auto`). Property read last, it is the costlier one.
524528
const dot = text.indexOf(".");
525529
if (
526-
unit === "" &&
527530
dot !== -1 &&
528531
dot < end &&
529532
!num.includes(".") &&
530533
!num.includes("e") &&
531-
_inIntegerProperty()
534+
(_S._inSupportsPrelude || (unit === "" && _inIntegerProperty()))
532535
) {
533-
return _keepFraction(num);
536+
return _keepFraction(num) + unit;
534537
}
535538
// WHY: `round()`, `mod()` and `rem()` are step functions of their arguments,
536539
// so a rewrite holding everywhere else does not hold inside one. `4.5cm` and
@@ -3988,9 +3991,12 @@ const printer = (path, writer) => {
39883991
case T_PERCENTAGE:
39893992
case T_DIMENSION: {
39903993
const raw = path.source();
3991-
// A declaration value, or a media feature — only ever a `<length>`,
3992-
// `<ratio>` or `<integer>`. Elsewhere a prelude number is An+B.
3993-
return minify && (path.inValue() || _S._inMediaConditionPrelude)
3994+
// A declaration value, a media feature or a `@supports` test — elsewhere a
3995+
// prelude number is An+B, as it is in a tested `selector()`.
3996+
return minify &&
3997+
(path.inValue() ||
3998+
_S._inMediaConditionPrelude ||
3999+
(_S._inSupportsPrelude && !_S._inSupportsSelector))
39944000
? _normalizeNumericToken(raw)
39954001
: raw;
39964002
}

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,23 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
40374037
expect(condition(input)).toBe(input);
40384038
});
40394039

4040+
it.each([
4041+
["(color:rgba(0,0,0,0.5))", "(color:rgba(0,0,0,.5))"],
4042+
["(width:0.50PX)", "(width:.5px)"],
4043+
["not (opacity:00.5)", "not (opacity:.5)"],
4044+
// a written fraction keeps the number from reading as an integer
4045+
["(z-index:1.0)", "(z-index:1.0)"],
4046+
["(width:-0.0px)", "(width:-.0px)"],
4047+
["(height:10.50%)", "(height:10.5%)"],
4048+
// the sign is a token boundary, and the rest is not rewritten
4049+
["(width:+0.5px)", "(width:+.5px)"],
4050+
["(width:5E-1px)", "(width:5E-1px)"],
4051+
["(width:16px)", "(width:16px)"],
4052+
["selector(:nth-child(+2n+01))", "selector(:nth-child(+2n+01))"]
4053+
])("spells the numbers of %s as %s", (input, expected) => {
4054+
expect(condition(input)).toBe(expected);
4055+
});
4056+
40404057
it("keeps the comment a keyword's condition was parted from it by", () => {
40414058
// WebKit reads `and`, `or` or `not` as invalid unless whitespace follows,
40424059
// as it reads the source here, so a space in its place would make it read.
@@ -4123,7 +4140,7 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
41234140
minify(
41244141
"@import url(A.css) layer(Foo) supports(opacity:0.50) ALL and (min-width:1px);"
41254142
)
4126-
).toBe("@import url(A.css) layer(Foo) supports(opacity:0.50) (width>=1px);");
4143+
).toBe("@import url(A.css) layer(Foo) supports(opacity:.5) (width>=1px);");
41274144
// A layer's name and the URL are the author's, whatever their case.
41284145
expect(minify("@import url(A.css) LAYER ALL;")).toBe(
41294146
"@import url(A.css) LAYER all;"
@@ -7203,12 +7220,13 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
72037220
expect(minify("@media (aspect-ratio:16/9){a{color:red}}")).toBe(
72047221
"@media (aspect-ratio:16/9){a{color:red}}"
72057222
);
7206-
// A style query compares the tokens as written, and `@supports` reads a
7207-
// declaration rather than a feature.
7223+
// A style query compares the tokens as written, and a `@supports` test
7224+
// keeps its fraction: `z-index:1.0` holds no integer.
72087225
const style = "@container style(--x: 0480.0px){a{color:red}}";
72097226
expect(minify(style)).toBe(style);
7210-
const supports = "@supports (width:0480.0px){a{color:red}}";
7211-
expect(minify(supports)).toBe(supports);
7227+
expect(minify("@supports (width:0480.0px){a{color:red}}")).toBe(
7228+
"@supports (width:480.0px){a{color:red}}"
7229+
);
72127230
// ...and a selector's own An+B keeps every sign it was written with.
72137231
expect(minify("li:nth-child(2n+3){color:red}")).toBe(
72147232
"li:nth-child(2n+3){color:red}"

‎tooling/compare-css-tools.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,11 +1276,6 @@ const EXPECTED = [
12761276
contains: "-> \\76 ",
12771277
source: "parsing/cases/declaration.css",
12781278
why: "`prop: {value}{value}` is no declaration: it reparses as a rule whose selector `prop:` is invalid, which the printer writes back as authored"
1279-
},
1280-
{
1281-
relation: "respelling leading-zero",
1282-
contains: "-> 0",
1283-
why: "an `@supports` prelude is kept as authored because it is a feature test rather than a declaration to print, so `(color:rgba(0,0,0,.5))` keeps every byte. A judgement to revisit rather than a defect; retire this entry if it changes"
12841279
}
12851280
];
12861281

0 commit comments

Comments
 (0)