Skip to content

Commit 3cb5202

Browse files
build(tooling): align the compare sweeps across languages
- `Expected.source` takes a list of fixtures; the CSS `*zoom` entry names its four, so a narrowed run no longer reports it stale. - XML: invariants run the embedded CSS renderer presets the webpack rows use, `meaning` reads stylesheets in their canonical CSS form under the preset's target, sources are swept cut short (`cutsAt`, shared with HTML), and `PROFILE=1` prints. - JS: the printing table shows `2nd` and dropped legal notices, and purity digests the printed output. - CSS, HTML and XML dispatch through `runMode`, with tests. - Fix a blank SVG `<style>` the input ran out in inside a CDATA section being printed with the section still open, found by the XML cuts. Claude-Session: https://claude.ai/code/session_01Bqhj38umy4coKMvXQT28NV
1 parent 949a597 commit 3cb5202

15 files changed

Lines changed: 620 additions & 158 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+
Close a CDATA section the input ran out in inside a minified SVG `<style>`.

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
run: yarn test:invariants:css --require-corpus
158158

159159
# One language's finding must not hide another's: each names a repro of
160-
# its own and all three are read in the same sitting. `!cancelled()`
160+
# its own and all four are read in the same sitting. `!cancelled()`
161161
# rather than `always()`, so a cancelled run stops here instead of
162162
# sweeping again.
163163
- name: HTML printer invariants

‎lib/html/syntax-printer.js‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,17 @@ const _printXml = (path, writer, minify) => {
316316
case NodeType.Text: {
317317
const raw = path.source();
318318
const data = _dataOf(path.node);
319-
if (minify && _isXmlStylesheet(path)) {
320-
return data.trim() === ""
321-
? raw
322-
: _S._deferEmbeddedSource === undefined
323-
? _xmlCharacterData(_renderEmbedded(data, CSS_TYPE))
324-
: _deferEmbedded(data, CSS_TYPE, (rendered) =>
325-
_xmlCharacterData(
326-
typeof rendered === "string" ? rendered : data
327-
)
328-
);
319+
const stylesheet = minify && _isXmlStylesheet(path);
320+
if (stylesheet && data.trim() !== "") {
321+
return _S._deferEmbeddedSource === undefined
322+
? _xmlCharacterData(_renderEmbedded(data, CSS_TYPE))
323+
: _deferEmbedded(data, CSS_TYPE, (rendered) =>
324+
_xmlCharacterData(typeof rendered === "string" ? rendered : data)
325+
);
329326
}
330327
// A CDATA section the input ran out in would hold the end tags after it.
331-
if (!minify) {
328+
// A blank stylesheet is kept as written, as the renderer is never asked.
329+
if (!minify || stylesheet) {
332330
return raw.includes("<![CDATA[") && !raw.endsWith("]]>")
333331
? escapeXmlText(data)
334332
: raw;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,6 +4195,32 @@ b&#10;c">
41954195
"<r>]]&gt;&#13;</r>"
41964196
);
41974197
});
4198+
4199+
it("should close a blank stylesheet the input ran out in", () => {
4200+
const { builtinEmbeddedRenderer } = require("../../../lib/html/builtinEmbeddedRenderer");
4201+
/**
4202+
* @param {string} document an SVG
4203+
* @returns {string} the minified document
4204+
*/
4205+
const minify = (document) =>
4206+
/** @type {{ code: string }} */ (
4207+
new SourceProcessor().process(document, {
4208+
xml: true,
4209+
mode: "minify",
4210+
renderEmbeddedSource: builtinEmbeddedRenderer()
4211+
})
4212+
).code;
4213+
expect(
4214+
minify('<svg xmlns="http://www.w3.org/2000/svg"><style>\n<![CDATA[\n')
4215+
).toBe('<svg xmlns="http://www.w3.org/2000/svg"><style>\n\n</style></svg>');
4216+
expect(
4217+
minify(
4218+
'<svg xmlns="http://www.w3.org/2000/svg"><style><![CDATA[ ]]></style></svg>'
4219+
)
4220+
).toBe(
4221+
'<svg xmlns="http://www.w3.org/2000/svg"><style><![CDATA[ ]]></style></svg>'
4222+
);
4223+
});
41984224
});
41994225
});
42004226

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
"use strict";
22

3+
const CACHE_NAME = "css-tool-comparison";
4+
5+
const installed = jest.fn(() => Promise.resolve(CACHE_NAME));
6+
7+
// A real install would `npm ci` the corpus into a contributor's cache, and every
8+
// packed stylesheet and built fixture is taken as there, so `--setup` fetches
9+
// and builds nothing: what is under test is that it installs and stops.
10+
jest.mock("../../../tooling/compare-tools-harness", () => ({
11+
...jest.requireActual("../../../tooling/compare-tools-harness"),
12+
exists: () => Promise.resolve(true),
13+
installPackages: installed
14+
}));
15+
316
const { SourceProcessor } = require("../../../lib/css/syntax");
4-
const { COLD_BRANCHES } = require("../../../tooling/compare-css-tools");
17+
const {
18+
COLD_BRANCHES,
19+
runMode
20+
} = require("../../../tooling/compare-css-tools");
21+
22+
// Read before anything clears the mock: an unguarded script installs while
23+
// being required, and a `beforeEach` would erase exactly that evidence.
24+
const callsWhileRequiring = installed.mock.calls.length;
25+
26+
describe("the CSS tool comparison", () => {
27+
it("should start nothing when the script is required rather than run", () => {
28+
expect(callsWhileRequiring).toBe(0);
29+
});
30+
31+
it("should install the corpus and stop on `--setup`", async () => {
32+
installed.mockClear();
33+
34+
await runMode("--setup");
35+
36+
expect(installed).toHaveBeenCalledTimes(1);
37+
expect(installed).toHaveBeenCalledWith(CACHE_NAME);
38+
});
39+
});
540

641
// What each rewrite the fixture is there for writes. Every one was counted at
742
// zero over every stylesheet on disk before the fixture was written, so an edit

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { Readable } = require("stream");
88
const { pathToFileURL } = require("url");
99
const {
1010
compress,
11+
cutsAt,
1112
exists,
1213
filterFrom,
1314
findingGroups,
@@ -835,6 +836,39 @@ describe("compare-tools-harness", () => {
835836
expect(text).toContain("idempotence — bytes");
836837
expect(text).toContain("stale — 1 expectation(s) matched nothing");
837838
});
839+
840+
it("lets an entry naming several fixtures cover a group reaching them", () => {
841+
const groups = findingGroups([
842+
{
843+
relation: "idempotence",
844+
contains: "a ->",
845+
source: ["one", "two"],
846+
why: "those fixtures write it"
847+
}
848+
]);
849+
groups.add(finding("idempotence", " a -> b"), "default", "one.html");
850+
groups.add(finding("idempotence", " a -> b"), "default", "two.html");
851+
expect(written(groups).count).toBe(0);
852+
groups.add(finding("idempotence", " a -> b"), "default", "three.html");
853+
expect(written(groups).count).toBe(2);
854+
});
855+
});
856+
857+
describe("cutsAt", () => {
858+
it("cuts inside the first construct past the midpoint, else the first", () => {
859+
const source = "<a>1</a><b>2</b><c>3</c>";
860+
expect(cutsAt(source, [["in a start tag", /<[a-z]/g]])).toEqual([
861+
["in a start tag", "<a>1</a><b>2</b><c"]
862+
]);
863+
expect(cutsAt(source, [["in a comment", /<!--./g]])).toEqual([]);
864+
expect(cutsAt("<a>1</a>xyz", [["in a tag", /<[a-z]/g]])).toEqual([
865+
["in a tag", "<a"]
866+
]);
867+
});
868+
869+
it("leaves out a cut that would keep the whole source", () => {
870+
expect(cutsAt("ab<a", [["in a start tag", /<[a-z]/g]])).toEqual([]);
871+
});
838872
});
839873

840874
describe("narrowing a sweep", () => {
@@ -868,7 +902,13 @@ describe("compare-tools-harness", () => {
868902
{ relation: "idempotence", contains: "a", why: "…" },
869903
{ relation: "respelling quote-double", contains: "b", why: "…" },
870904
{ relation: "idempotence", contains: "c", source: "one", why: "…" },
871-
{ relation: "idempotence", contains: "d", presets: ["legacy"], why: "…" }
905+
{ relation: "idempotence", contains: "d", presets: ["legacy"], why: "…" },
906+
{
907+
relation: "idempotence",
908+
contains: "e",
909+
source: ["three", "one"],
910+
why: "…"
911+
}
872912
];
873913

874914
/**
@@ -886,7 +926,8 @@ describe("compare-tools-harness", () => {
886926
"a",
887927
"b",
888928
"c",
889-
"d"
929+
"d",
930+
"e"
890931
]);
891932
});
892933

@@ -896,7 +937,7 @@ describe("compare-tools-harness", () => {
896937

897938
it("drops an entry for a relation or respelling the run does not ask", () => {
898939
process.env.SPELLING = "unquote";
899-
expect(kept(["one.html"], ["legacy"])).toEqual(["a", "c", "d"]);
940+
expect(kept(["one.html"], ["legacy"])).toEqual(["a", "c", "d", "e"]);
900941
delete process.env.SPELLING;
901942
process.env.RELATION = "respelling quote-double";
902943
expect(kept(["one.html"], ["legacy"])).toEqual(["b"]);

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
"use strict";
22

3+
const CACHE_NAME = "html-tool-comparison";
4+
5+
const installed = jest.fn(() => Promise.resolve(CACHE_NAME));
6+
7+
// A real install would `npm ci` the corpus into a contributor's cache, and
8+
// nothing here reads it: the built documents are the ones under test.
9+
jest.mock("../../../tooling/compare-tools-harness", () => ({
10+
...jest.requireActual("../../../tooling/compare-tools-harness"),
11+
installPackages: installed
12+
}));
13+
314
const fs = require("fs");
415
const path = require("path");
516
const { SourceProcessor } = require("../../../lib/html/syntax");
617
const {
718
COLD_CONSTRUCTS,
819
EMBEDDED_LEGACY_OPTIONS,
9-
EMBEDDED_OPTIONS
20+
EMBEDDED_OPTIONS,
21+
runMode
1022
} = require("../../../tooling/compare-html-tools");
1123
const { collectFiles } = require("../../../tooling/compare-tools-harness");
1224

25+
// Read before anything clears the mock: an unguarded script installs while
26+
// being required, and a `beforeEach` would erase exactly that evidence.
27+
const callsWhileRequiring = installed.mock.calls.length;
28+
29+
describe("the HTML tool comparison", () => {
30+
it("should start nothing when the script is required rather than run", () => {
31+
expect(callsWhileRequiring).toBe(0);
32+
});
33+
34+
it("should install the corpus and stop on `--setup`", async () => {
35+
installed.mockClear();
36+
37+
await expect(runMode("--setup")).resolves.toBe(CACHE_NAME);
38+
39+
expect(installed).toHaveBeenCalledTimes(1);
40+
expect(installed).toHaveBeenCalledWith(CACHE_NAME);
41+
});
42+
});
43+
1344
const ROOT = path.join(__dirname, "..", "..", "..");
1445

1546
// cspell:ignore popovertarget popovertargetaction contenteditable exportparts

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ jest.mock("../../../tooling/compare-tools-harness", () => ({
1212
installPackages: installed
1313
}));
1414

15+
const acorn = require("acorn");
1516
const {
1617
behaviorDifference,
18+
roundTrip,
1719
runMode
1820
} = require("../../../tooling/compare-js-tools");
1921

@@ -36,6 +38,45 @@ describe("the JavaScript tool comparison", () => {
3638
});
3739
});
3840

41+
describe("the printing table's last column", () => {
42+
const source =
43+
'/*! a license */\n/** @license MIT */\nvar s = "/*! not one */"; /* plain */ a.b = 1;';
44+
45+
it("should find nothing where the output keeps every name and notice", () => {
46+
expect(
47+
roundTrip(
48+
acorn,
49+
new Set(["b"]),
50+
2,
51+
"/*! a license */\n/** @license MIT */\na.b=1",
52+
"script"
53+
)
54+
).toBe("-");
55+
});
56+
57+
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(
59+
"1 names! e.g. c, 2 legal notices!"
60+
);
61+
expect(roundTrip(acorn, new Set(), 2, "/*! a license */", "script")).toBe(
62+
"1 legal notice!"
63+
);
64+
});
65+
66+
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+
);
71+
});
72+
73+
it("should say when the output does not parse back", () => {
74+
expect(roundTrip(acorn, new Set(), 0, "a.=", "script")).toMatch(
75+
/^will not parse back: /
76+
);
77+
});
78+
});
79+
3980
describe("the behavior relation", () => {
4081
it("should report a printed script printing otherwise than its source", () => {
4182
const [report] = /** @type {import("../../../tooling/compare-tools-harness").Report[]} */ (

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,31 @@ const {
1717
ATOM_FEED,
1818
EDITOR_SVG,
1919
MAVEN_POM,
20+
PRESETS,
2021
XLIFF,
22+
XML_CUTS,
2123
canonical,
24+
printerFor,
25+
runMode,
2226
toolsFor
2327
} = require("../../../tooling/compare-xml-tools");
28+
const { cutsAt } = require("../../../tooling/compare-tools-harness");
2429
const { SourceProcessor } = require("../../../lib/html/syntax");
2530

2631
describe("the XML tool comparison", () => {
2732
it("should start nothing when the script is required rather than run", () => {
2833
expect(installed).toHaveBeenCalledTimes(0);
2934
});
3035

36+
it("should install the corpus and stop on `--setup`", async () => {
37+
installed.mockClear();
38+
39+
await expect(runMode("--setup")).resolves.toBe("xml-tool-comparison");
40+
41+
expect(installed).toHaveBeenCalledTimes(1);
42+
expect(installed).toHaveBeenCalledWith("xml-tool-comparison");
43+
});
44+
3145
it.each([
3246
["editor-exported SVG", EDITOR_SVG],
3347
["Atom feed", ATOM_FEED],
@@ -84,6 +98,57 @@ describe("the XML tool comparison", () => {
8498
it("should expand what the DTD declares, but not inside a CDATA section", () => {
8599
const read = canonical(EDITOR_SVG);
86100
expect(read).toContain('xmlns="http://www.w3.org/2000/svg"');
87-
expect(read).toContain(".st0{&amp;st0;}");
101+
expect(read).toContain(".st0{&amp;st0}");
102+
});
103+
104+
it("should read a stylesheet and a style attribute the same however the CSS minifier wrote them", () => {
105+
const source =
106+
'<svg><style>\n\t.dot {\n\t\tfill: #ff0000;\n\t}\n</style><g style="display: none;"/></svg>';
107+
const minified =
108+
'<svg><style>.dot{fill:red}</style><g style="display:none"/></svg>';
109+
expect(canonical(minified)).toBe(canonical(source));
110+
expect(canonical('<svg><g style="display:block"/></svg>')).not.toBe(
111+
canonical('<svg><g style="display:none"/></svg>')
112+
);
113+
});
114+
115+
it("should read CSS under the target it was lowered for", () => {
116+
const options = { environment: { browsers: ["safari 15.4"] } };
117+
const source = "<svg><style>.a{user-select:none}</style></svg>";
118+
const lowered =
119+
"<svg><style>.a{-webkit-user-select:none;user-select:none}</style></svg>";
120+
expect(canonical(lowered, options)).toBe(canonical(source, options));
121+
expect(canonical(lowered)).not.toBe(canonical(source));
122+
});
123+
124+
it("should cut the editor-exported SVG inside every construct it carries", () => {
125+
const where = cutsAt(EDITOR_SVG, XML_CUTS).map(([name]) => name);
126+
expect(where).toEqual(
127+
expect.arrayContaining([
128+
"in a processing instruction",
129+
"in a comment",
130+
"in an internal subset",
131+
"in a reference",
132+
"in a CDATA section",
133+
"in a <style> body",
134+
"in a style attribute"
135+
])
136+
);
137+
});
138+
139+
it("should print every cut of the built fixtures as its own output under every preset", () => {
140+
for (const xml of [EDITOR_SVG, ATOM_FEED, MAVEN_POM, XLIFF]) {
141+
for (const [, source] of cutsAt(xml, XML_CUTS)) {
142+
for (const [preset, options] of PRESETS) {
143+
const minify = printerFor(options);
144+
const once = minify(source);
145+
expect({ preset, source, again: minify(once) }).toEqual({
146+
preset,
147+
source,
148+
again: once
149+
});
150+
}
151+
}
152+
}
88153
});
89154
});

0 commit comments

Comments
 (0)