Skip to content

Commit 59ac091

Browse files
fix(css): decode a trailing lone backslash as U+FFFD (#23038)
* fix(css): decode a trailing lone backslash as U+FFFD A `\` the input ends after escapes EOF and decodes to U+FFFD (CSS Syntax §4.3.7); `unescapeIdentifier` kept the backslash. The comparison tooling now also compares doctype public and system identifiers (HTML and XML) and decoded CSS rule and at-rule names, which is what surfaced it. parse5 8 sets limited-quirks mode for an empty system identifier, which §13.2.6.4.1 counts as missing; that is recorded as expected. Claude-Session: https://claude.ai/code/session_013FhutQSvaiUkh5Rrcksd9q * fix(css): read an escaped `:` as part of an unused custom property's name `_isUnusedCustomProperty` cut the printed declaration at its first `:`, so `--a\:b` was looked up as `--a\` and never dropped. Claude-Session: https://claude.ai/code/session_013FhutQSvaiUkh5Rrcksd9q * chore: keep the changeset within 80 characters Claude-Session: https://claude.ai/code/session_013FhutQSvaiUkh5Rrcksd9q
1 parent 88866a4 commit 59ac091

11 files changed

Lines changed: 182 additions & 28 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep an escaped `:` in a CSS custom property name when dropping unused ones.

‎lib/css/syntax-parser.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,9 +3936,8 @@ const _unescapeIdentifier = (str) => {
39363936
ret += "\\";
39373937
i += 1;
39383938
} else if (str.length === i + 1) {
3939-
// A trailing lone `\` is retained.
3940-
// https://github.com/postcss/postcss-selector-parser/commit/01a6b346e3612ce1ab20219acc26abdc259ccefb
3941-
ret += "\\";
3939+
// A trailing lone `\` escapes the end of the input (§4.3.7).
3940+
ret += "\uFFFD";
39423941
}
39433942
// Otherwise the lone `\` is dropped; the next char flushes with its run.
39443943
lastFlush = i + 1;
@@ -18614,8 +18613,14 @@ const _isUnusedCustomProperty = (path, item, text) => {
1861418613
if (_unusedSymbols === null || path.type(item) !== NodeType.Declaration) {
1861518614
return false;
1861618615
}
18617-
const colon = text.indexOf(":");
18618-
if (colon <= 0 || text.charCodeAt(1) !== CC_HYPHEN_MINUS) return false;
18616+
if (text.charCodeAt(1) !== CC_HYPHEN_MINUS) return false;
18617+
// An escaped `:` belongs to the name; the first bare one ends it.
18618+
let colon = 2;
18619+
for (; colon < text.length; colon++) {
18620+
const cc = text.charCodeAt(colon);
18621+
if (cc === CC_COLON) break;
18622+
if (cc === CC_REVERSE_SOLIDUS) colon++;
18623+
}
1861918624
return _unusedSymbols.has(unescapeIdentifier(text.slice(0, colon)));
1862018625
};
1862118626

‎test/unitCases/css/printer.unittest.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9698,6 +9698,13 @@ describe("CssSyntax minify — unusedSymbols", () => {
96989698
expect(withoutSymbols(":root{--gone:red}", ["gone"])).toBe(
96999699
":root{--gone:red}"
97009700
);
9701+
// An escaped `:` is part of the name, not the end of it.
9702+
expect(
9703+
withoutSymbols(":root{--a\\:b:red;--kept:blue}", ["--a:b"])
9704+
).toBe(":root{--kept:blue}");
9705+
expect(withoutSymbols(":root{--a\\:b:red}", ["--a�"])).toBe(
9706+
":root{--a\\:b:red}"
9707+
);
97019708
});
97029709

97039710
it("reaches a rule at any depth", () => {

‎test/unitCases/cssIdentifier.unittest.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ describe("css identifier utils", () => {
6969
["\\3A )", ":)"],
7070
// double backslash is preserved
7171
["foo\\\\bar", "foo\\bar"],
72-
// trailing single backslash retained
73-
["foo\\", "foo\\"],
72+
// a `\` the input ends after is an escape of EOF (§4.3.7)
73+
["foo\\", "foo\uFFFD"],
7474
// null code point -> replacement character
7575
["\\0 ", "�"],
7676
// surrogate -> replacement character

‎test/unitCases/tooling/compare-css.unittest.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const installed = jest.fn(() => Promise.resolve(CACHE_NAME));
99
// and builds nothing: what is under test is that it installs and stops.
1010
// css-tree is installed into that same cache, so its tree is written out by
1111
// hand; postcss arrives with the `css-loader` dev dependency, so it is real.
12-
const mockCssTree = { parse: jest.fn() };
12+
// Names decode as webpack decodes them: the stand-in answers parses, not escapes.
13+
const mockCssTree = {
14+
parse: jest.fn(),
15+
ident: { decode: require("../../../lib/css/syntax-parser").unescapeIdentifier }
16+
};
1317

1418
jest.mock("../../../tooling/compare-tools-harness", () => ({
1519
...jest.requireActual("../../../tooling/compare-tools-harness"),

‎test/unitCases/tooling/compare-html.unittest.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ describe("compare-html-tools — agreeing with parse5", () => {
264264
const parsed = (id) => ({
265265
nodeName: "#document",
266266
childNodes: [
267-
{ nodeName: "#documentType", name: "html" },
267+
{ nodeName: "#documentType", name: "html", publicId: "", systemId: "" },
268268
element("html", HTML_NAMESPACE, [
269269
element("head", HTML_NAMESPACE, []),
270270
element("body", HTML_NAMESPACE, [

‎test/unitCases/tooling/compare-xml.unittest.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ describe("the XML tool comparison", () => {
163163

164164
describe("the XML tool comparison — agreeing with @rgrove/parse-xml", () => {
165165
const xml =
166-
'<?xml version="1.0"?>\n<?style href="a"?>\n<r a="1" b="&amp;">t<![CDATA[c]]><!--m--><e/></r>\n';
166+
'<?xml version="1.0"?>\n<!DOCTYPE r>\n<?style href="a"?>\n<r a="1" b="&amp;">t<![CDATA[c]]><!--m--><e/></r>\n';
167167

168168
// What `@rgrove/parse-xml` builds for it, the declaration left out.
169169
const parsed = {
170170
children: [
171+
{ type: "doctype", name: "r", publicId: null, systemId: null },
171172
{ type: "pi", name: "style", content: 'href="a"' },
172173
{ type: "text", text: "\n" },
173174
{
@@ -198,6 +199,7 @@ describe("the XML tool comparison — agreeing with @rgrove/parse-xml", () => {
198199
// What `@xmldom/xmldom` builds for it, the declaration as an instruction.
199200
const document = dom([
200201
{ nodeType: 7, target: "xml", data: 'version="1.0"' },
202+
{ nodeType: 10, name: "r", publicId: "", systemId: "" },
201203
{ nodeType: 7, target: "style", data: 'href="a"' },
202204
{
203205
nodeType: 1,
@@ -302,8 +304,8 @@ describe("the XML tool comparison — the entities a DTD declares", () => {
302304
// A stand-in for `@rgrove/parse-xml`, which reads no DTD: it rejects every
303305
// entity unless resolved, and the internal subset is what `[…]` holds.
304306
const standIn = `exports.parseXml = (xml, options) => {
305-
if (xml === "broken") throw new Error(options.preserveDocumentType ? "second" : "first");
306-
if (options.preserveDocumentType) {
307+
if (xml === "broken") throw new Error(options.ignoreUndefinedEntities ? "second" : "first");
308+
if (options.ignoreUndefinedEntities) {
307309
const start = xml.indexOf("[");
308310
return { children: start === -1 ? [] : [{ type: "doctype", internalSubset: xml.slice(start + 1, xml.lastIndexOf("]")) }] };
309311
}

‎tooling/COMPARE_TOOLS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Printing tables add output size raw and gzip/brotli/zstd (`test:size` settings), whether the output lost classes / changed the DOM / stopped naming a property / dropped a legal notice (CSS and JS: a `/*!` comment, and in JS one naming `@license` or `@preserve`) ("rejects it" = the tool errored), and `2nd`: what printing the output again moved — the idempotence question asked of every tool (`-` is a printer done printing). A round-trip printer that reformats nothing (postcss for CSS, parse5 for HTML) sits in `beautify` as the floor.
88
- XML: every output is read back with `@rgrove/parse-xml`, a strict parser sharing nothing with webpack's, so `differs` also says when an output stopped being well-formed. webpack's `minify` row hands each SVG or XHTML stylesheet to `builtinEmbeddedRenderer`, as a build does.
99
- JS: webpack's parse row is its own parser; its `webpack` printing row is `lib/javascript/jsMinify.js` (the build's minify entry, one `SourceProcessor` pass that compresses and mangles), while `webpack printer` is the same work as one `SourceProcessor` run without that function around it, so the two are read against each other in the same table. The parse table's last column compares each ESTree parser to acorn's tree and names the first differing node; with the `spans` relation this catches a divergence in `lib/javascript/syntax-parser.js` (one reads the tree, the other the offsets — a range can be wrong while every node agrees). A parser with a dialect of its own reads `own dialect`, not a disagreement.
10-
- CSS, HTML and XML: the parse table's last column holds each parser to a reference — css-tree, parse5 (the §13.2 tree, with scripting off and again on, as the printer reads), `@rgrove/parse-xml` — and names the first differing node by its path (`/html[1]/body[1]/p[2]`), then how many more differences follow. The trees are aligned first (Myers' diff), so a node one side lacks is one difference rather than one per node after it. Both trees are flattened the same way: elements with their namespace and attributes in order, text with runs folded, comments, doctype nodes and processing instructions (HTML reads `<?…>` as the bogus comment §13.2 makes of it); CSS rules, at-rules and declarations, each prelude and value as its source wrote it, comments dropped and whitespace collapsed. Held: webpack everywhere, postcss (CSS), `@xmldom/xmldom` and htmlparser2 (XML); a parser building no tree, or a tree of its own, reads `own dialect`. Where a reader can see a reference's gap, it closes it, so a disagreement names something real: css-tree's reading of a style rule's block, which it gives up on once declarations and rules mix and reads an at-rule's block in as rules alone, is read again declarations first, and again from any declaration a rule read swallowed into its selector; a declaration whose name opens with no identifier (`*zoom`) is dropped, as the spec drops it, and postcss's `_prop` hack is read back as written; webpack's block contents are compared in source order, though its walk visits declarations before nested rules; and a `<?…` the input runs out in is dropped from parse5's tree where webpack's processing instruction rules drop it. parse-xml reads no DTD, so a source it rejects is read again with the general entities the internal subset declares, where each value is plain text once its character references are decoded. Left as disagreements: parse5 has no declarative shadow root (`<template shadowrootmode>`) or customizable `<select>`, and css-tree recovers from some invalid CSS other than the spec does. Both sides of a cell are cut from just before the first character they differ at. For webpack the column is also an invariant (`agreement`, below).
10+
- CSS, HTML and XML: the parse table's last column holds each parser to a reference — css-tree, parse5 (the §13.2 tree, with scripting off and again on, as the printer reads), `@rgrove/parse-xml` — and names the first differing node by its path (`/html[1]/body[1]/p[2]`), then how many more differences follow. The trees are aligned first (Myers' diff), so a node one side lacks is one difference rather than one per node after it. Both trees are flattened the same way: elements with their namespace and attributes in order, text with runs folded, comments, doctype nodes with their public and system identifiers, and processing instructions (HTML reads `<?…>` as the bogus comment §13.2 makes of it); CSS rules, at-rules and declarations, names decoded and each prelude and value as its source wrote it, comments dropped and whitespace collapsed. Held: webpack everywhere, postcss (CSS), `@xmldom/xmldom` and htmlparser2 (XML); a parser building no tree, or a tree of its own, reads `own dialect`. Where a reader can see a reference's gap, it closes it, so a disagreement names something real: css-tree's reading of a style rule's block, which it gives up on once declarations and rules mix and reads an at-rule's block in as rules alone, is read again declarations first, and again from any declaration a rule read swallowed into its selector; a declaration whose name opens with no identifier (`*zoom`) is dropped, as the spec drops it, and postcss's `_prop` hack is read back as written; webpack's block contents are compared in source order, though its walk visits declarations before nested rules; and a `<?…` the input runs out in is dropped from parse5's tree where webpack's processing instruction rules drop it. parse-xml reads no DTD, so a source it rejects is read again with the general entities the internal subset declares, where each value is plain text once its character references are decoded. Left as disagreements: parse5 has no declarative shadow root (`<template shadowrootmode>`) or customizable `<select>`, and css-tree recovers from some invalid CSS other than the spec does. Both sides of a cell are cut from just before the first character they differ at. For webpack the column is also an invariant (`agreement`, below).
1111
- `FIXTURE=`, `TOOL=`, `STAGE=` narrow the rows. `PROFILE=1` samples each row's timed runs and lists under it where the CPU went, by file (terser's and webpack's by their path in `lib`, `(generated)` for `new Function` code) — the attribution a phase is chosen and judged by.
1212
- **Corpora** are declared in `tooling/comparison/<cache name>/`: a `package.json` pinning every package exactly and a `package-lock.json` freezing the resolution, transitive deps included, so a sweep runs what was committed rather than what the registry serves that day, and a finding names a reproducible corpus. `installPackages` runs `npm ci` into `node_modules/.cache/<cache name>/` (not webpack's deps) on first run, skipped while the lockfile it last installed from matches; expect ~a minute of install first, and 10+ minutes per full run. Bump a corpus by editing its manifest, never the script. Dependabot watches the four directories, so a bump arrives as its own PR — where a break is attributable, not on somebody else's — and there the `invariants` job — which installs and sweeps all four — reds on a broken printer invariant, an `npm ci`-refused manifest/lockfile, or a platform package the lockfile misses (`yarn benchmark:<language>-tools:setup` is the install alone). Each corpus keeps an `.npmrc` with `package-lock=true`: Dependabot reads npm config from parent directories (npm doesn't), so otherwise the root's `package-lock=false` (right for yarn's tree) leaves a bump's lockfile unregenerated — exactly what `npm ci` refuses.
1313
- **Single stylesheets** the CSS corpus reads out of packages whose dependency trees dwarf them (component libraries, design systems) are listed in `tooling/comparison/css-tool-comparison/stylesheets.json` instead: each is fetched with `npm pack` at its pinned version, checked against its pinned integrity, and only that one file is kept, under `stylesheets/` in the cache. Dependabot does not see these, so a bump edits the version and integrity by hand (`npm view <package>@<version> dist.integrity`).

‎tooling/compare-css-tools.js‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -734,23 +734,27 @@ const startsIdentifier = (name) => {
734734
* @param {string} property a declaration's name
735735
* @param {string} value its value
736736
* @param {boolean} important whether it carries `!important`
737+
* @param {(name: string) => string} decode the reader's own escape decoder, read the name with
737738
*/
738-
const declaration = (events, property, value, important) => {
739+
const declaration = (events, property, value, important, decode) => {
739740
if (!startsIdentifier(property)) return;
740741
events.leaf(
741-
`${property}: ${comparedText(value)}${important ? " !important" : ""}`
742+
`${decode(property)}: ${comparedText(value)}${
743+
important ? " !important" : ""
744+
}`
742745
);
743746
};
744747

745748
/**
746749
* @param {string} name an at-rule's name
747750
* @param {string} prelude its prelude
748751
* @param {boolean} block whether it has a block
752+
* @param {(name: string) => string} decode the reader's own escape decoder, read the name with
749753
* @returns {string} what its start says, as every reader writes it
750754
*/
751-
const atRuleOf = (name, prelude, block) => {
755+
const atRuleOf = (name, prelude, block, decode) => {
752756
const compared = comparedText(prelude);
753-
return `@${name}${compared === "" ? "" : ` ${compared}`}${
757+
return `@${decode(name)}${compared === "" ? "" : ` ${compared}`}${
754758
block ? " {" : ";"
755759
}`;
756760
};
@@ -838,7 +842,12 @@ const cssTreeTree = (css) => {
838842
const { block } = node;
839843
events.open(
840844
`@${node.name}`,
841-
atRuleOf(node.name, source(node.prelude), Boolean(block))
845+
atRuleOf(
846+
node.name,
847+
source(node.prelude),
848+
Boolean(block),
849+
cssTree.ident.decode
850+
)
842851
);
843852
if (block && nested) {
844853
const { start, end } = block.loc;
@@ -853,7 +862,13 @@ const cssTreeTree = (css) => {
853862
}
854863
events.close(block ? "}" : "");
855864
} else if (node.type === "Declaration") {
856-
declaration(events, node.property, source(node.value), node.important);
865+
declaration(
866+
events,
867+
node.property,
868+
source(node.value),
869+
node.important,
870+
cssTree.ident.decode
871+
);
857872
} else if (node.type === "Raw") {
858873
// A stray `;` is no statement: the spec discards it (§5.4.5).
859874
if (/^[\s;]*$/.test(source(node))) continue;
@@ -937,7 +952,8 @@ const webpackTree = (css) => {
937952
atRuleOf(
938953
written,
939954
block ? prelude : prelude.replace(/;\s*$/, ""),
940-
block !== null
955+
block !== null,
956+
unescapeIdentifier
941957
),
942958
block ? "}" : ""
943959
);
@@ -957,7 +973,8 @@ const webpackTree = (css) => {
957973
held,
958974
nodePath.source(name[0], name[1]),
959975
value ? nodePath.source(value[0], value[1]) : "",
960-
nodePath.flag(Flag.important)
976+
nodePath.flag(Flag.important),
977+
unescapeIdentifier
961978
);
962979
for (const [, , said] of held.list) {
963980
hold(nodePath.start(), "", said, undefined);
@@ -996,6 +1013,8 @@ const webpackTree = (css) => {
9961013
*/
9971014
const postcssTree = (css) => {
9981015
const postcss = load("postcss");
1016+
// postcss keeps names as written, so they are read with the reference's decoder.
1017+
const { decode } = load("css-tree").ident;
9991018
const events = new TreeEvents();
10001019
/**
10011020
* @param {EXPECTED_ANY} parent a postcss container
@@ -1012,7 +1031,7 @@ const postcssTree = (css) => {
10121031
} else if (node.type === "atrule") {
10131032
events.open(
10141033
`@${node.name}`,
1015-
atRuleOf(node.name, node.params, node.nodes !== undefined)
1034+
atRuleOf(node.name, node.params, node.nodes !== undefined, decode)
10161035
);
10171036
if (node.nodes) walk(node);
10181037
events.close(node.nodes ? "}" : "");
@@ -1023,7 +1042,8 @@ const postcssTree = (css) => {
10231042
/[^\s;]*$/.exec(node.raws.before)
10241043
)[0] + node.prop,
10251044
node.raws.value ? node.raws.value.raw : node.value,
1026-
Boolean(node.important)
1045+
Boolean(node.important),
1046+
decode
10271047
);
10281048
}
10291049
}
@@ -1815,9 +1835,9 @@ const EXPECTED = [
18151835
},
18161836
{
18171837
relation: "agreement",
1818-
contains: "css-tree @\\30 media \\;, tool @\\30 media\\;",
1838+
contains: "css-tree @0media \\;, tool @0media\uFFFD;",
18191839
source: "css/escape-at-end-of-input.css",
1820-
why: "a `\\` the input ends after is a valid escape (§4.3.8), so it belongs to the at-rule's name; css-tree reads it as its prelude"
1840+
why: "a `\\` the input ends after is a valid escape (§4.3.8) of EOF, U+FFFD in the at-rule's name (§4.3.7); css-tree reads it as its prelude"
18211841
}
18221842
];
18231843

0 commit comments

Comments
 (0)