Skip to content

Commit ba16551

Browse files
fix(css): read the value-time property guards by the same name
The declaration printer reads the unescaped name; the guards that decide what a value may be rewritten to still read the source's spelling, so an escaped name misses every table they look it up in. `z-index` is the one that changes what a page renders rather than only what it costs: `1.0` is not an `<integer>`, so `z-index:1.0` is a declaration the engine drops — and `\7a -index:1.0` was normalized to `1`, which turns it into one the engine applies. The others are the same missing lookup with smaller stakes: a family list, a colour-only property, the `-webkit-tap-highlight-color` special case. Reading the unescaped name costs an `indexOf` on a short string; the guard this is asked of most already memoizes its answer on the declaration. Reported by CodeRabbit on this PR, and reproduced before fixing.
1 parent caf4d77 commit ba16551

4 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎lib/css/syntax-printer.js‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,15 @@ const _convertUnit = (num, unit, written) => {
320320
* grammar, so a number in it may be one.
321321
* @returns {boolean} true inside such a declaration
322322
*/
323+
// WHY: the unescaped name, like the declaration printer's own lookups — these
324+
// decide what a value may be rewritten to, so reading `\7a -index` as written
325+
// misses `z-index` and normalizes `1.0` to `1`, which turns a declaration the
326+
// engine drops into one it applies.
323327
const _inIntegerProperty = () =>
324328
_S._valueDeclaration !== null &&
325-
INTEGER_PROPERTIES.has(toLowerCaseIfNeeded(A.name(_S._valueDeclaration)));
329+
INTEGER_PROPERTIES.has(
330+
toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration))
331+
);
326332

327333
/**
328334
* Whether the declaration being printed is one an engine takes no `calc()` in,
@@ -332,7 +338,7 @@ const _inIntegerProperty = () =>
332338
const _inCalcRejectingProperty = () =>
333339
_S._valueDeclaration !== null &&
334340
CALC_REJECTING_PROPERTIES.has(
335-
toLowerCaseIfNeeded(A.name(_S._valueDeclaration))
341+
toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration))
336342
);
337343

338344
/**
@@ -344,7 +350,7 @@ const _inCalcRejectingProperty = () =>
344350
const _inNumberOnlyOutsideCalcProperty = () =>
345351
_S._valueDeclaration !== null &&
346352
NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES.has(
347-
toLowerCaseIfNeeded(A.name(_S._valueDeclaration))
353+
toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration))
348354
);
349355

350356
// A folded term, as a number and its unit.
@@ -377,7 +383,7 @@ const _losesClamp = (property, number, unit) => {
377383
*/
378384
const _foldLosesClamp = (term) => {
379385
if (_S._valueDeclaration === null) return false;
380-
const property = toLowerCaseIfNeeded(A.name(_S._valueDeclaration));
386+
const property = toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration));
381387
if (!CLAMPED_VALUE_RANGES.has(property)) return false;
382388
const match = _TERM_VALUE_RE.exec(term);
383389
return match === null || _losesClamp(property, match[1], match[2]);
@@ -1299,7 +1305,8 @@ const _namesUnreadableColor = (value) => {
12991305
*/
13001306
const _inTapHighlightColor = () =>
13011307
_S._valueDeclaration !== null &&
1302-
A.name(_S._valueDeclaration).toLowerCase() === "-webkit-tap-highlight-color";
1308+
A.unescapedName(_S._valueDeclaration).toLowerCase() ===
1309+
"-webkit-tap-highlight-color";
13031310

13041311
/**
13051312
* Write one `:nth-*()` in its shortest equal spelling: the An+B microsyntax
@@ -1327,7 +1334,9 @@ const _minifyAnPlusB = (name, inner) => {
13271334
*/
13281335
const _inFontFamily = () =>
13291336
_S._valueDeclaration !== null &&
1330-
FAMILY_LIST_PROPERTIES.has(asciiLowerCaseName(A.name(_S._valueDeclaration)));
1337+
FAMILY_LIST_PROPERTIES.has(
1338+
asciiLowerCaseName(A.unescapedName(_S._valueDeclaration))
1339+
);
13311340

13321341
/**
13331342
* Whether the string is the whole entry in its comma-separated slot:
@@ -1399,7 +1408,9 @@ const _unquoteFontFamily = (raw) => {
13991408
*/
14001409
const _inColorOnlyProperty = () =>
14011410
_S._valueDeclaration !== null &&
1402-
COLOR_ONLY_PROPERTIES.has(toLowerCaseIfNeeded(A.name(_S._valueDeclaration)));
1411+
COLOR_ONLY_PROPERTIES.has(
1412+
toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration))
1413+
);
14031414

14041415
/**
14051416
* Whether the declaration being printed spells no name of the author's: its
@@ -1414,7 +1425,7 @@ const _inKeywordOnlyValue = () => {
14141425
if (_S._keywordOnlyFor !== _S._valueDeclaration) {
14151426
_S._keywordOnlyFor = _S._valueDeclaration;
14161427
const property = _standardSpelling(
1417-
toLowerCaseIfNeeded(A.name(_S._valueDeclaration))
1428+
toLowerCaseIfNeeded(A.unescapedName(_S._valueDeclaration))
14181429
);
14191430
_S._keywordOnly =
14201431
KEYWORD_ONLY_PROPERTIES.has(property) ||
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css escaped-property-names escaped-property-names should compile 1`] = `
4+
Array [
5+
"/*! the value rules a property name selects */.t01{font-weight:700}",
6+
"/*! the same property, spelled with an escape */.t02{\\\\66 ont-weight:700}",
7+
"/*! a shorthand whose value repeats what the notation implies */.t03{margin:0}",
8+
"/*! the same shorthand, spelled with an escape */.t04{\\\\6d argin:0}",
9+
"/*! a custom property's value is kept as authored */.t05{--kept:0px 0px 0px 0px}",
10+
"/*! the same custom property, its leading dashes escaped */.t06{\\\\2d -kept:0px 0px 0px 0px}",
11+
"/*! a prefixed spelling reads through to the standard property */.t07{-webkit-box-flex:1}",
12+
"/*! the same prefixed spelling, with an escape */.t08{-\\\\77 ebkit-box-flex:1}",
13+
"/*! an integer property keeps a value that is not an integer, so the engine drops it */.t09{z-index:1.0}",
14+
"/*! the same, spelled with an escape: still not an integer, still dropped */.t10{\\\\7a -index:1.0}",
15+
"/*! a family list is read as one, not folded into a keyword */.t11{font-family:A B}",
16+
"/*! the same family list, spelled with an escape */.t12{\\\\66 ont-family:A B}",
17+
]
18+
`;

‎test/configCases/css/escaped-property-names/__snapshots__/ConfigTest.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ Array [
1010
"/*! the same custom property, its leading dashes escaped */.t06{\\\\2d -kept:0px 0px 0px 0px}",
1111
"/*! a prefixed spelling reads through to the standard property */.t07{-webkit-box-flex:1}",
1212
"/*! the same prefixed spelling, with an escape */.t08{-\\\\77 ebkit-box-flex:1}",
13+
"/*! an integer property keeps a value that is not an integer, so the engine drops it */.t09{z-index:1.0}",
14+
"/*! the same, spelled with an escape: still not an integer, still dropped */.t10{\\\\7a -index:1.0}",
15+
"/*! a family list is read as one, not folded into a keyword */.t11{font-family:A B}",
16+
"/*! the same family list, spelled with an escape */.t12{\\\\66 ont-family:A B}",
1317
]
1418
`;

‎test/configCases/css/escaped-property-names/style.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@
1919
.t07 { -webkit-box-flex: 1 }
2020
/*! the same prefixed spelling, with an escape */
2121
.t08 { -\77 ebkit-box-flex: 1 }
22+
/*! an integer property keeps a value that is not an integer, so the engine drops it */
23+
.t09 { z-index: 1.0 }
24+
/*! the same, spelled with an escape: still not an integer, still dropped */
25+
.t10 { \7a -index: 1.0 }
26+
/*! a family list is read as one, not folded into a keyword */
27+
.t11 { font-family: A B }
28+
/*! the same family list, spelled with an escape */
29+
.t12 { \66 ont-family: A B }

0 commit comments

Comments
 (0)