Skip to content

Commit b4c3707

Browse files
fix(css): resolve an escape the input ran out of (#22257)
* fix(css): resolve an escape the input ran out of A `\` no character follows is an escape CSS Syntax 3 resolves: to the replacement character anywhere but inside a string (§4.3.7), and to nothing inside one (§4.3.5). `A.source` already read it that way, but the two accessors beside it — `name` and `unescaped`, in both the path and the materialized-node implementations — handed back the `\` as written. Printing it is what made this visible. `_rulePrelude` reads an at-rule's name through `name`, and a blockless at-rule is closed with `;`, so `@\30 media\` printed as `@\30 media\;` — where the `\` escapes the `;` rather than ending the statement. A second pass then read one statement where the first wrote one and added another `;`, so the output grew a byte per pass instead of standing still. The fixture added here is what `yarn test:invariants:css` now holds that to; it reports `idempotence — differs, +1 B` under all three presets without this change. A string's `\` carries one more meaning the identifier unescape has no context for: before a newline it is a line continuation, naming nothing. It has to be read in the same pass as the escapes around it rather than stripped ahead of them — removing `\`+newline from `"\41\<newline>b"` first leaves `\41b`, which then spells U+041B instead of `A` and `b`. `rule_list` was the corpus's own answer rather than webpack's: it states "parse a list of rules" with the top-level flag unset, which is `parseABlocksContents`, while the suite ran it through `parseAStylesheetsContents` — the flag set, where CDO/CDC are discarded between rules. webpack implemented both correctly; only the mapping was wrong. All 11 of that file's cases now agree. The five entries this retires from `KNOWN_DIVERGENCES` were asserted to *still* diverge, so the suite failed on them before the list was updated. * chore(changeset): keep the escape entry within the length limit The unioned entry ran to 85 characters, over the 80 the guide sets. It names the same two things in fewer words rather than dropping the at-rule-name half the entry already carried. * test(css): cover the escape meanings a stylesheet cannot reach Four of the branches the fix added were reached by nothing: a string whose escape the input ran out of, a closing quote an odd run of backslashes escaped, a `\\` inside a string, and a CR or CRLF line continuation. Each needs the source to stop mid-escape or to spell a newline the corpus does not, so a `configCases/` stylesheet cannot drive them — a build appends a newline after a module's content, which makes the last `\` a delim token rather than an escape at end of input. Counted rather than assumed: instrumenting each branch and replaying the committed corpus cases plus the invariants fixture reached six of ten, and the same count reaches all ten with these. Both halves are checked by mutation — removing the CR arm fails one case, and resolving a string's trailing escape to the replacement character fails three.
1 parent bb50c7e commit b4c3707

5 files changed

Lines changed: 169 additions & 44 deletions

File tree

‎.changeset/036-css-escaped-at-rule-names.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Read an at-rule whose name is written with CSS escapes as the name it spells.
5+
Read a CSS escape as what it spells, an at-rule name and end of input included.

‎lib/css/syntax-parser.js‎

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,9 +2454,11 @@ const NODE_READER_PROTO = /** @type {NodeReader} */ ({
24542454
const store = this._store;
24552455
const i = this._i;
24562456
const v = _storeValueOf(store, i);
2457-
return store.types[i] === T_STRING
2458-
? unescapeIdentifier(v.slice(1, -1))
2459-
: unescapeIdentifier(v);
2457+
if (store.types[i] === T_STRING) {
2458+
return _unescapeStringValue(_stringContent(v));
2459+
}
2460+
const ranOut = store.ends[i] === store.input.length;
2461+
return unescapeIdentifier(ranOut ? _resolveTrailingEscape(v, false) : v);
24602462
},
24612463
get numericValue() {
24622464
const store = this._store;
@@ -2505,10 +2507,14 @@ const NODE_READER_PROTO = /** @type {NodeReader} */ ({
25052507
get name() {
25062508
const store = this._store;
25072509
const i = this._i;
2510+
const end = store.aux0[i];
25082511
// An at-rule's name skips its `@`; others start at the node.
2509-
return store.types[i] === T_AT_RULE
2510-
? store.input.slice(store.starts[i] + 1, store.aux0[i])
2511-
: store.input.slice(store.starts[i], store.aux0[i]);
2512+
const text = store.input.slice(
2513+
store.types[i] === T_AT_RULE ? store.starts[i] + 1 : store.starts[i],
2514+
end
2515+
);
2516+
if (end !== store.input.length) return text;
2517+
return _resolveTrailingEscape(text, false);
25122518
},
25132519
get nameStart() {
25142520
return this._store.starts[this._i];
@@ -3930,6 +3936,66 @@ const _unescapeIdentifier = (str) => {
39303936
const escapeIdentifier = makeCacheable(_escapeIdentifier);
39313937
const unescapeIdentifier = makeCacheable(_unescapeIdentifier);
39323938

3939+
/**
3940+
* A string token's text with its quotes taken off. §4.3.5 returns the token at
3941+
* end of input too, and that one has no closing quote to take off — nor does one
3942+
* an odd run of backslashes escaped.
3943+
* @param {string} v the string token's raw text
3944+
* @returns {string} the text the quotes hold
3945+
*/
3946+
const _stringContent = (v) => {
3947+
let end = v.length;
3948+
if (end > 1 && v.charCodeAt(end - 1) === v.charCodeAt(0)) {
3949+
let slashes = 0;
3950+
while (
3951+
end - 2 - slashes >= 1 &&
3952+
v.charCodeAt(end - 2 - slashes) === CC_REVERSE_SOLIDUS
3953+
) {
3954+
slashes++;
3955+
}
3956+
if (slashes % 2 === 0) end--;
3957+
}
3958+
return v.slice(1, end);
3959+
};
3960+
3961+
/**
3962+
* A string token's value — `_unescapeIdentifier` with the two meanings §4.3.5
3963+
* gives a `\` that §4.3.7 does not: before a newline it is a line continuation
3964+
* and at end of input it names nothing, so both spell nothing rather than a `\`.
3965+
* @param {string} str the string's text, its quotes removed
3966+
* @returns {string} the value the string holds
3967+
*/
3968+
const _unescapeStringValue = (str) => {
3969+
const first = str.indexOf("\\");
3970+
if (first === -1) return str;
3971+
let ret = "";
3972+
let lastFlush = 0;
3973+
for (let i = first; i < str.length; i++) {
3974+
if (str.charCodeAt(i) !== CC_REVERSE_SOLIDUS) continue;
3975+
ret += str.slice(lastFlush, i);
3976+
// Read before the newline test, so the `\` closing a hex escape is the one
3977+
// that opens a continuation rather than the digits' own terminator.
3978+
const gobbled = gobbleHex(str, i + 1);
3979+
const next = str.charCodeAt(i + 1);
3980+
if (gobbled !== undefined) {
3981+
ret += gobbled[0];
3982+
i += gobbled[1];
3983+
} else if (next === CC_REVERSE_SOLIDUS) {
3984+
ret += "\\";
3985+
i += 1;
3986+
} else if (next === CC_CARRIAGE_RETURN) {
3987+
// A CRLF is the one newline the pair spells.
3988+
i += str.charCodeAt(i + 2) === CC_LINE_FEED ? 2 : 1;
3989+
} else if (next === CC_LINE_FEED || next === CC_FORM_FEED) {
3990+
i += 1;
3991+
}
3992+
// Otherwise the `\` is dropped — an escaped character flushes with the run
3993+
// after it, and one the input ran out of leaves nothing to flush.
3994+
lastFlush = i + 1;
3995+
}
3996+
return ret + str.slice(lastFlush);
3997+
};
3998+
39333999
/**
39344000
* Fold an at-keyword's source spelling to the name the tokenizer read it as, so
39354001
* a name written with escapes is looked up as the one it spells. Unescaping
@@ -13465,6 +13531,20 @@ const _endsInLoneEscape = (text) => {
1346513531
return (text.length - at) % 2 === 1;
1346613532
};
1346713533

13534+
/**
13535+
* Text whose trailing escape the input ran out of, resolved to what it names:
13536+
* nothing inside a string (§4.3.5) and the replacement character anywhere else
13537+
* (§4.3.7). Text ending in anything else is handed back as written.
13538+
* @param {string} text raw source text ending at end of input
13539+
* @param {boolean} inString whether the text is a string token's
13540+
* @returns {string} the text the token holds
13541+
*/
13542+
const _resolveTrailingEscape = (text, inString) => {
13543+
if (!_endsInLoneEscape(text)) return text;
13544+
const kept = text.slice(0, -1);
13545+
return inString ? kept : `${kept}\uFFFD`;
13546+
};
13547+
1346813548
/**
1346913549
* The declarations a `text-decoration` naming more than its line says, for a
1347013550
* target that reads the shorthand as CSS 2.1's line alone. The line stays in the
@@ -14551,13 +14631,8 @@ const A = {
1455114631
const i = _nodeIndex(n);
1455214632
const end = _ends[i];
1455314633
const text = _S._input.slice(_starts[i], end);
14554-
if (end !== _S._input.length || !_endsInLoneEscape(text)) return text;
14555-
// The escape the input ran out of names nothing inside a string (§4.3.5)
14556-
// and the replacement character anywhere else (§4.3.7) — either way not
14557-
// the `\` the source shows, which would escape what the printer adds next.
14558-
return _types[i] === T_STRING
14559-
? text.slice(0, -1)
14560-
: `${text.slice(0, -1)}\uFFFD`;
14634+
if (end !== _S._input.length) return text;
14635+
return _resolveTrailingEscape(text, _types[i] === T_STRING);
1456114636
},
1456214637
/**
1456314638
* @param {Node=} n node
@@ -14568,23 +14643,18 @@ const A = {
1456814643
},
1456914644
/**
1457014645
* @param {Node=} n node
14571-
* @returns {string} unescaped token value
14646+
* @returns {string} unescaped token value — an escape the input ran out
14647+
* of names the replacement character, or nothing inside a string, where a
14648+
* `\` before a newline is a line continuation naming nothing either
1457214649
*/
1457314650
unescaped(n = _S._currentNode) {
1457414651
const i = _nodeIndex(n);
1457514652
const v = _valueOf(i);
14576-
if (_types[i] !== T_STRING) return unescapeIdentifier(v);
14577-
// §4.3.5 returns the token at end of input too, and that one has no closing
14578-
// quote to take off — nor does one an odd run of backslashes escaped.
14579-
let end = v.length;
14580-
if (end > 1 && v.charCodeAt(end - 1) === v.charCodeAt(0)) {
14581-
let slashes = 0;
14582-
while (end - 2 - slashes >= 1 && v.charCodeAt(end - 2 - slashes) === 92) {
14583-
slashes++;
14584-
}
14585-
if (slashes % 2 === 0) end--;
14653+
if (_types[i] !== T_STRING) {
14654+
const ranOut = _ends[i] === _S._input.length;
14655+
return unescapeIdentifier(ranOut ? _resolveTrailingEscape(v, false) : v);
1458614656
}
14587-
return unescapeIdentifier(v.slice(1, end));
14657+
return _unescapeStringValue(_stringContent(v));
1458814658
},
1458914659
/**
1459014660
* @param {Node=} n node
@@ -14626,13 +14696,19 @@ const A = {
1462614696
},
1462714697
/**
1462814698
* @param {Node=} n node
14629-
* @returns {string} rule / declaration / function name
14699+
* @returns {string} rule / declaration / function name — as written, but
14700+
* for an escape the input ran out of, which is the replacement character it
14701+
* names rather than the `\` that spells it (§4.3.7)
1463014702
*/
1463114703
name(n = _S._currentNode) {
1463214704
const i = _nodeIndex(n);
14633-
return _types[i] === T_AT_RULE
14634-
? _S._input.slice(_starts[i] + 1, _aux0[i])
14635-
: _S._input.slice(_starts[i], _aux0[i]);
14705+
const end = _aux0[i];
14706+
const text = _S._input.slice(
14707+
_types[i] === T_AT_RULE ? _starts[i] + 1 : _starts[i],
14708+
end
14709+
);
14710+
if (end !== _S._input.length) return text;
14711+
return _resolveTrailingEscape(text, false);
1463614712
},
1463714713
/**
1463814714
* @param {Node=} n node
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* A stylesheet the invariants sweep reads: its last token is an escape the
2+
input ran out of, which names the replacement character rather than the `\`
3+
that spells it. Printing the `\` would escape the `;` the statement is
4+
closed with, and each pass would then add another. */
5+
@\30 media\

‎test/specCases/cssParsing-webpack.spectest.js‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ const {
2121
parseADeclaration,
2222
parseAListOfComponentValues,
2323
parseARule,
24-
parseAStylesheet,
25-
parseAStylesheetsContents
24+
parseAStylesheet
2625
} = require("../../lib/css/syntax-parser");
2726
const expectNoDeprecations = require("../helpers/expectNoDeprecations");
2827
const {
@@ -303,10 +302,13 @@ describe("css-parsing-tests trees", () => {
303302
],
304303
["one_declaration", (source) => serializeDeclaration(parseADeclaration(source))],
305304
["one_rule", (source) => serializeRule(parseARule(source), source)],
305+
// `rule_list` is the spec's "parse a list of rules" with the top-level flag
306+
// unset, which is what a block's contents are read as — a stylesheet's are
307+
// the flag set, and the two differ over whether CDO/CDC are discarded.
306308
[
307309
"rule_list",
308310
(source) =>
309-
parseAStylesheetsContents(source).map((rule) =>
311+
parseABlocksContents(source).rules.map((rule) =>
310312
serializeRule(rule, source)
311313
)
312314
],
@@ -334,17 +336,6 @@ describe("css-parsing-tests trees", () => {
334336
["component_value_list #46", "unicode-range token"],
335337
["component_value_list #47", "match token"],
336338
["component_value_list #48", "match token"],
337-
// A `\` with nothing after it is a parse error the spec resolves to
338-
// U+FFFD; webpack keeps the character it was written as.
339-
["component_value_list #7", "backslash at end of input"],
340-
["component_value_list #10", "backslash at end of input"],
341-
["component_value_list #11", "backslash at end of input"],
342-
// A `\` before a newline inside a string is a line continuation the spec
343-
// removes, and webpack keeps.
344-
["component_value_list #12", "escaped newline in a string"],
345-
// CDO and CDC are preserved tokens in a prelude the spec reads at the top
346-
// level; webpack drops them.
347-
["rule_list #10", "CDO/CDC in a prelude"],
348339
// A declaration's value is the run webpack will print, so the trailing
349340
// whitespace and `;` the spec keeps as component values are not in it.
350341
["one_declaration #11", "trailing token in a value"],

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,59 @@ describe("CssSyntax regressions", () => {
192192
});
193193
});
194194

195+
// A `\` carries three meanings a stylesheet a build reads never reaches, since
196+
// each needs the source to stop mid-escape or to spell a newline the corpus
197+
// does not: §4.3.5 inside a string and §4.3.7 everywhere else.
198+
describe("CssSyntax — an escape the input ran out of", () => {
199+
const CR = String.fromCharCode(0x0d);
200+
const FF = String.fromCharCode(0x0c);
201+
202+
/**
203+
* @param {string} src css source whose first component value is a leaf token
204+
* @returns {string} that token's unescaped text
205+
*/
206+
const unescapedOf = (src) =>
207+
/** @type {import("../../lib/css/syntax-parser").Token} */ (
208+
parseAListOfComponentValues(src)[0]
209+
).unescaped;
210+
211+
it("removes a line continuation however the newline is spelled", () => {
212+
expect(unescapedOf('"a\\\nb"')).toBe("ab");
213+
expect(unescapedOf(`"a\\${CR}b"`)).toBe("ab");
214+
expect(unescapedOf(`"a\\${CR}\nb"`)).toBe("ab");
215+
expect(unescapedOf(`"a\\${FF}b"`)).toBe("ab");
216+
});
217+
218+
it("reads an escape before a continuation as the escape it is", () => {
219+
// The `\` closing `\41` is what opens the continuation, so reading the
220+
// two together would spell U+041B rather than `A` and `b`.
221+
expect(unescapedOf('"\\41\\\nb"')).toBe("Ab");
222+
expect(unescapedOf('"a\\\\b"')).toBe("a\\b");
223+
expect(unescapedOf('"a\\\\\\\nb"')).toBe("a\\b");
224+
});
225+
226+
it("names nothing at end of input inside a string", () => {
227+
expect(unescapedOf('"ab\\')).toBe("ab");
228+
// An odd run of backslashes escaped the quote, so it closes nothing and
229+
// the token runs to the end of the input.
230+
expect(unescapedOf('"ab\\"')).toBe('ab"');
231+
});
232+
233+
it("names the replacement character at end of input anywhere else", () => {
234+
expect(unescapedOf("ab\\")).toBe("ab�");
235+
expect(unescapedOf("#ab\\")).toBe("ab�");
236+
expect(unescapedOf("@ab\\")).toBe("ab�");
237+
});
238+
239+
it("prints what the escape names rather than the backslash", () => {
240+
// Printing the `\` would escape the `}` or `;` written after it, so a
241+
// second pass would read one construct where the first wrote one.
242+
expect(minifyFor("a{color:re\\")).toBe("a{color:re�}");
243+
expect(minifyFor("@\\30 media\\")).toBe("@\\30 media�;");
244+
expect(minifyFor('a{content:"x\\')).toBe('a{content:"x"}');
245+
});
246+
});
247+
195248
/**
196249
* @param {string} src css source
197250
* @returns {number[]} component value types

0 commit comments

Comments
 (0)