Skip to content

Commit 9e08180

Browse files
fix(css): read only CSS whitespace in a joined selector and An+B
U+00A0 is an identifier's character, not CSS whitespace, but `\s` took it for one: `[c]<U+00A0>b` joined a valid rule into a list no engine parses, and `:nth-child(n<U+00A0>+<U+00A0>2)` was rewritten into a valid `n+2`.
1 parent 5b3e3c3 commit 9e08180

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14849,19 +14849,21 @@ const _STRING = String.raw`"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'`;
1484914849
// The matchers Selectors 4 §6 defines, and only `i` after one: an engine that
1485014850
// cannot read the `s` modifier drops the selector, and with it the list.
1485114851
const _ATTRIBUTE = String.raw`\[(?:${_IDENT}\|)?${_IDENT}(?:[~|^$*]?=(?:${_IDENT}|${_STRING})(?: [iI])?)?\]`;
14852+
// CSS whitespace: U+00A0 is an identifier's character, not a separator.
14853+
const _SPACE = "[ \\t\\n\\f\\r]";
1485214854
const _LEGACY_ELEMENT_NAMES = [...LEGACY_PSEUDO_ELEMENTS].join("|");
1485314855
// A pseudo-class with no selector inside it: argument-less, or the `An+B` of an
1485414856
// `:nth-*()`, whose grammar is closed — the one argument a shape may carry.
1485514857
const _PSEUDO_CLASS = String.raw`:(?!(?:${_LEGACY_ELEMENT_NAMES})(?![-\w]))(?:(?:${[
1485614858
...NTH_PSEUDO_FUNCTIONS
1485714859
].join(
1485814860
"|"
14859-
)})\(\s*(?:odd|even|[-+]?(?:\d*n(?:\s*[-+]\s*\d+)?|\d+))\s*\)|[-\w]+(?![-\w(]))`;
14861+
)})\(${_SPACE}*(?:odd|even|[-+]?(?:\d*n(?:${_SPACE}*[-+]${_SPACE}*\d+)?|\d+))${_SPACE}*\)|[-\w]+(?![-\w(]))`;
1486014862
// A pseudo-element ends the selector it is in: anything after it is no shape
1486114863
// an engine parses.
1486214864
const _PSEUDO_ELEMENT = String.raw`(?:::[-\w]+|:(?:${_LEGACY_ELEMENT_NAMES}))(?![-\w(])`;
1486314865
const _COMPOUND = `(?:\\*|${_IDENT}|[.#]${_IDENT}|${_ATTRIBUTE})(?:[.#]${_IDENT}|${_ATTRIBUTE}|${_PSEUDO_CLASS})*`;
14864-
const _COMBINATOR = "(?:\\s*[>+~]\\s*|\\s+)";
14866+
const _COMBINATOR = `(?:${_SPACE}*[>+~]${_SPACE}*|${_SPACE}+)`;
1486514867
const _JOINABLE_SELECTOR_RE = new RegExp(
1486614868
`^(?:${_COMPOUND}${_COMBINATOR})*${_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`,
1486714869
"i"

‎lib/css/syntax-printer.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,17 +2534,19 @@ const _takeCustomSelector = (prelude) => {
25342534
};
25352535

25362536
// The `An+B` microsyntax (CSS Syntax 3 §6), whose whitespace and `+` are its
2537-
// own: `2n + 1`, `+3` and `2N+1` all say what a shorter spelling does.
2537+
// own: `2n + 1`, `+3` and `2N+1` all say what a shorter spelling does. Its
2538+
// whitespace is CSS's, so U+00A0 keeps an argument no engine reads as written.
25382539
const _NTH_RE =
2539-
/^\s*(?:([+-]?)\s*(\d*)[nN]\s*(?:([+-])\s*(\d+))?|([+-]?)\s*(\d+))\s*$/;
2540+
/^[ \t\n\f\r]*(?:([+-]?)[ \t\n\f\r]*(\d*)[nN][ \t\n\f\r]*(?:([+-])[ \t\n\f\r]*(\d+))?|([+-]?)[ \t\n\f\r]*(\d+))[ \t\n\f\r]*$/;
2541+
const _NTH_EDGE_SPACE_RE = /^[ \t\n\f\r]+|[ \t\n\f\r]+$/g;
25402542

25412543
/**
25422544
* Write one `An+B` in its shortest equal spelling.
25432545
* @param {string} text the argument between the parentheses
25442546
* @returns {string | null} the shortest spelling, or null when it is not `An+B`
25452547
*/
25462548
const _minifyNth = (text) => {
2547-
const lower = text.trim().toLowerCase();
2549+
const lower = text.replace(_NTH_EDGE_SPACE_RE, "").toLowerCase();
25482550
let a;
25492551
let b;
25502552
if (lower === "even") {

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6073,6 +6073,10 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
60736073
["...or no `n` to stand for", "a{top:0}b:nth-child(n2){top:0}"],
60746074
["...or two signs", "a{top:0}b:nth-child(+-n){top:0}"],
60756075
["...or two before `B`", "a{top:0}b:nth-child(2n++1){top:0}"],
6076+
// U+00A0 is no CSS whitespace but an identifier's character.
6077+
["...or U+00A0 for a space", "a{top:0}b:nth-child(n + 2){top:0}"],
6078+
["...or around a keyword", "a{top:0}b:nth-child( odd){top:0}"],
6079+
["U+00A0 stands for a combinator", "a{top:0}[c] b{top:0}"],
60766080
["...as does a pseudo-element", "a{top:0}b::part{top:0}"],
60776081
["a pseudo-element is followed in its compound", "a{top:0}b:before.c{top:0}"],
60786082
["...or by another compound", "a{top:0}b:before c{top:0}"],

0 commit comments

Comments
 (0)