Skip to content

Commit 5225423

Browse files
fix(html): keep an XML reference as written only when its name is a Name
- An `&` followed by a name XML's `Name` production refuses (`&foo?;`, `&1a;`) is escaped when minifying, as the decoder left it text. - The JS comparison matches legal notices one by one, so a changed or duplicated notice counts as dropped. - The XML idempotence checks read CSS under the preset's target, as `meaning` does. Claude-Session: https://claude.ai/code/session_01Bqhj38umy4coKMvXQT28NV
1 parent 4f75e6e commit 5225423

5 files changed

Lines changed: 60 additions & 20 deletions

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6392,10 +6392,10 @@ const isXmlChar = (code) =>
63926392
const XML_REFERENCE_REGEXP =
63936393
/&(?:#(?:x([\da-fA-F]+)|(\d+))|([^\s&;#<>"'=/%]+));/g;
63946394
const XML_CHARACTER_REFERENCE_REGEXP = /&#(?:x([\da-fA-F]+)|(\d+));/g;
6395-
// An `&` opening no reference `XML_REFERENCE_REGEXP` reads, which a strict
6396-
// parser refuses: the decoder kept it as text, so it is written escaped.
6395+
// An `&` opening no well-formed reference (§4.1, a `Name` per §2.3), which a
6396+
// strict parser refuses: the decoder kept it as text, so it is written escaped.
63976397
const XML_BARE_AMPERSAND_REGEXP =
6398-
/&(?!(?:#(?:x[\da-fA-F]+|\d+)|[^\s&;#<>"'=/%]+);)/;
6398+
/&(?!(?:#(?:x[\da-fA-F]+|\d+)|(?:[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u200C-\u200D]|[\uD800-\uDB7F][\uDC00-\uDFFF])(?:[-.0-9:A-Z_a-z\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u200C-\u200D]|[\uD800-\uDB7F][\uDC00-\uDFFF])*);)/;
63996399
// WHY: §3.3.3 normalizes each literal whitespace character of an attribute value
64006400
// to a space after §2.11 has folded CRLF, so a CRLF is one space; replacement
64016401
// text of an entity is already folded, so there each character counts alone.

‎test/unitCases/html/syntax.unittest.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4205,6 +4205,12 @@ b&#10;c">
42054205
expect(printXml("<r>&undeclared;</r>", "minify")).toBe(
42064206
"<r>&undeclared;</r>"
42074207
);
4208+
expect(printXml("<r>&foo?;&a(b;&.c;</r>", "minify")).toBe(
4209+
"<r>&amp;foo?;&amp;a(b;&amp;.c;</r>"
4210+
);
4211+
expect(printXml("<r>&é-1.x;&_:y;</r>", "minify")).toBe(
4212+
"<r>&é-1.x;&_:y;</r>"
4213+
);
42084214
});
42094215

42104216
it("should close a blank stylesheet the input ran out in", () => {

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,51 @@ describe("the printing table's last column", () => {
4747
roundTrip(
4848
acorn,
4949
new Set(["b"]),
50-
2,
50+
["! a license ", "* @license MIT "],
5151
"/*! a license */\n/** @license MIT */\na.b=1",
5252
"script"
5353
)
5454
).toBe("-");
5555
});
5656

5757
it("should name the legal notices and property names the output dropped", () => {
58-
expect(roundTrip(acorn, new Set(["b", "c"]), 2, "a.b=1", "script")).toBe(
58+
expect(
59+
roundTrip(acorn, new Set(["b", "c"]), ["! a ", "! b "], "a.b=1", "script")
60+
).toBe(
5961
"1 names! e.g. c, 2 legal notices!"
6062
);
61-
expect(roundTrip(acorn, new Set(), 2, "/*! a license */", "script")).toBe(
63+
expect(
64+
roundTrip(acorn, new Set(), ["! a license ", "! b "], "/*! a license */", "script")
65+
).toBe(
6266
"1 legal notice!"
6367
);
6468
});
6569

70+
it("should name a notice the output changed or replaced with another", () => {
71+
const notices = ["! a ", "! b "];
72+
expect(
73+
roundTrip(acorn, new Set(), notices, "/*! a *//*! a */", "script")
74+
).toBe("1 legal notice!");
75+
expect(
76+
roundTrip(acorn, new Set(), notices, "/*! b *//*! a */", "script")
77+
).toBe("-");
78+
});
79+
6680
it("should not count a `/*!` inside a string as a notice", () => {
67-
expect(roundTrip(acorn, new Set(), 0, source, "script")).toBe("-");
68-
expect(roundTrip(acorn, new Set(), 3, source, "script")).toBe(
69-
"1 legal notice!"
70-
);
81+
expect(roundTrip(acorn, new Set(), [], source, "script")).toBe("-");
82+
expect(
83+
roundTrip(
84+
acorn,
85+
new Set(),
86+
["! a license ", "* @license MIT ", "! not one "],
87+
source,
88+
"script"
89+
)
90+
).toBe("1 legal notice!");
7191
});
7292

7393
it("should say when the output does not parse back", () => {
74-
expect(roundTrip(acorn, new Set(), 0, "a.=", "script")).toMatch(
94+
expect(roundTrip(acorn, new Set(), [], "a.=", "script")).toMatch(
7595
/^will not parse back: /
7696
);
7797
});

‎tooling/compare-js-tools.js‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,12 +693,14 @@ const LEGAL_COMMENT = /^\**!|@license|@preserve/;
693693
* The legal notices among a program's comments, read off a real parse, so a
694694
* `/*!` inside a string, a template or a regular expression is not one.
695695
* @param {{ type: string, value: string }[]} comments what acorn read
696-
* @returns {number} how many are legal notices
696+
* @returns {string[]} the legal notices, each as its comment's text
697697
*/
698698
const legalNoticesOf = (comments) =>
699-
comments.filter(
700-
(comment) => comment.type === "Block" && LEGAL_COMMENT.test(comment.value)
701-
).length;
699+
comments
700+
.filter(
701+
(comment) => comment.type === "Block" && LEGAL_COMMENT.test(comment.value)
702+
)
703+
.map((comment) => comment.value);
702704

703705
const wantedFixture = filterFrom("FIXTURE");
704706
const wantedTool = filterFrom("TOOL");
@@ -2041,7 +2043,7 @@ const compareInWorker = (name, input) =>
20412043
* whether it still names every property and keeps every legal notice the input did.
20422044
* @param {EXPECTED_ANY} acorn the acorn export
20432045
* @param {Set<string>} before the input's property names
2044-
* @param {number} notices how many legal notices the input carries
2046+
* @param {string[]} notices the legal notices the input carries
20452047
* @param {string} printed what the tool wrote
20462048
* @param {"module" | "script"} goal what to parse it as
20472049
* @returns {string} the cell
@@ -2059,7 +2061,19 @@ const roundTrip = (acorn, before, notices, printed, goal) => {
20592061
}`;
20602062
}
20612063
const lost = [...before].filter((name) => !after.has(name));
2062-
const dropped = notices - legalNoticesOf(comments);
2064+
// Matched notice by notice: one changed, or another written twice in
2065+
// its place, is one dropped as surely as a missing one.
2066+
/** @type {Map<string, number>} */
2067+
const kept = new Map();
2068+
for (const notice of legalNoticesOf(comments)) {
2069+
kept.set(notice, (kept.get(notice) || 0) + 1);
2070+
}
2071+
let dropped = 0;
2072+
for (const notice of notices) {
2073+
const count = kept.get(notice) || 0;
2074+
if (count === 0) dropped++;
2075+
else kept.set(notice, count - 1);
2076+
}
20632077
/** @type {string[]} */
20642078
const parts = [];
20652079
if (lost.length > 0) {

‎tooling/compare-xml-tools.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ const invariants = (write) => {
13151315
? idempotence({
13161316
minify,
13171317
source: xml,
1318-
says: canonical,
1318+
says: (text) => canonical(text, cssOptions),
13191319
repro: (minified, at) => idempotenceRepro(minify, minified, at)
13201320
})
13211321
: { printed: minify(xml), reports: [] };
@@ -1350,12 +1350,12 @@ const invariants = (write) => {
13501350
for (const [label, xml] of corpus) {
13511351
for (const [where, source] of cutsAt(xml, XML_CUTS)) {
13521352
cut++;
1353-
for (const [preset, options] of presets) {
1353+
for (const [preset, options, cssOptions] of presets) {
13541354
const minify = printerFor(options);
13551355
const { reports } = idempotence({
13561356
minify,
13571357
source,
1358-
says: canonical,
1358+
says: (text) => canonical(text, cssOptions),
13591359
repro: (minified, at) => idempotenceRepro(minify, minified, at)
13601360
});
13611361
for (const report of reports) {

0 commit comments

Comments
 (0)