Skip to content

Commit 49fb573

Browse files
test: XML tokenizer and parser tests of its own, minifier entries covered by their printer tests (#22936)
* fix(html): keep every tag of a conditional comment's body A body was minified as a document of its own, so a wrapper one comment opens and another closes lost its end tag or had one added, an `<html class>` gained `</html>`, a table's row lost its cells and a `</p>` the page reads was dropped. Bodies now keep every tag, and one whose print would still add, drop or move one is left as written. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP * test: cover each minifier entry from its own printer test cssMinify, htmlMinify and jsMinify are now read whole by their own printer tests, as xmlMinify and jsonMinify were; xmlMinify joins the others outside coverage, as it is shipped to the workers as source too. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP * test(xml): give XML its own tokenizer and parser tests The XML mode's cases move out of the HTML tests, and the tree's parse errors from the HTML tokenizer test into its parser test. HTML and XML tests together still read every line they read before, and one branch only EOF reaches is folded. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP
1 parent 15ff887 commit 49fb573

22 files changed

Lines changed: 1603 additions & 1165 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 every tag of a conditional comment's body under `minifyConditionalComments`, and leave a body as written when minifying it would still add, drop or move one.

‎declarations/WebpackOptions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,7 @@ export interface OptimizationMinimizeHtml {
33723372
*/
33733373
mergeStyles?: boolean;
33743374
/**
3375-
* Minify the markup inside a downlevel-hidden conditional comment (`<!--[if IE]> … <![endif]-->`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.
3375+
* Minify the markup inside a downlevel-hidden conditional comment (`<!--[if IE]> … <![endif]-->`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.
33763376
* @since 5.110.0
33773377
*/
33783378
minifyConditionalComments?: boolean;

‎jest.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const config = {
6060
"\\.runtime\\.js$",
6161
// Serialized to the minimizer's worker pool as source (like runtime code):
6262
// coverage instrumentation would inject counters the worker can't resolve.
63-
"[\\\\/]lib[\\\\/](?:css[\\\\/]cssMinify|html[\\\\/]htmlMinify|javascript[\\\\/]jsMinify|json[\\\\/]jsonMinify)\\.js$",
63+
"[\\\\/]lib[\\\\/](?:css[\\\\/]cssMinify|html[\\\\/](?:htmlMinify|xmlMinify)|javascript[\\\\/]jsMinify|json[\\\\/]jsonMinify)\\.js$",
6464
"<rootDir>/test",
6565
"<rootDir>/schemas",
6666
"<rootDir>/examples",

‎lib/html/htmlMinify.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,44 @@ const htmlMinify = async (
219219

220220
new SourceProcessor().use(visitors).process(source, {});
221221

222+
/**
223+
* @param {string} markup a conditional comment's body
224+
* @returns {string} the start and end tags it spells, in order
225+
*/
226+
const tagsOf = (markup) => {
227+
/** @type {string[]} */
228+
const tags = [];
229+
parser.tokenize(markup, 0, {
230+
openTag: (text, _start, end, nameStart, nameEnd) => {
231+
tags.push(`<${text.slice(nameStart, nameEnd).toLowerCase()}`);
232+
return end;
233+
},
234+
closeTag: (text, _start, end, nameStart, nameEnd) => {
235+
tags.push(`</${text.slice(nameStart, nameEnd).toLowerCase()}`);
236+
return end;
237+
}
238+
});
239+
return tags.join(" ");
240+
};
241+
242+
// Every tag stays: which ones a body may leave out depends on what
243+
// surrounds the comment, which its own print cannot see.
244+
const bodyOptions = {
245+
...printOptions,
246+
transforms: { ...printOptions.transforms, removeOptionalTags: false },
247+
removeImpliedTags: /** @type {false} */ (false)
248+
};
249+
222250
// Back to front, so an earlier splice does not move a later range.
223251
for (let i = found.length - 1; i >= 0; i--) {
224252
const { start, end, value } = found[i];
225253
// Sequential: a later splice must not move an earlier range.
226-
const inner = await minifyDocument(value);
254+
const inner = await minifyDocument(value, bodyOptions);
255+
256+
// Read where the comment sits, not as a document: a print that adds,
257+
// drops or moves a tag (a wrapper another comment closes, a table's
258+
// row) decided as one would, so the body stays as written.
259+
if (tagsOf(inner) !== tagsOf(value)) continue;
227260

228261
source = `${source.slice(0, start)}${inner}${source.slice(end)}`;
229262
}

‎lib/html/syntax-parser.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8298,10 +8298,9 @@ modes.inBody = (t) => {
82988298
if (t.type === TOKEN_DOCTYPE) return treeError("unexpected-doctype", t);
82998299
if (t.type === TOKEN_START_TAG) return startTagInBody(t);
83008300
if (t.type === TOKEN_END_TAG) return endTagInBody(t);
8301-
if (t.type === TOKEN_EOF) {
8302-
if (templateModes.length) return modes.inTemplate(t);
8303-
if (hasUnclosedElement()) treeError("expected-closing-tag-but-got-eof", t);
8304-
}
8301+
// The end of the input, the one kind of token left.
8302+
if (templateModes.length) return modes.inTemplate(t);
8303+
if (hasUnclosedElement()) treeError("expected-closing-tag-but-got-eof", t);
83058304
};
83068305

83078306
/**

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4365,7 +4365,7 @@
43654365
"added": "5.110.0"
43664366
},
43674367
"minifyConditionalComments": {
4368-
"description": "Minify the markup inside a downlevel-hidden conditional comment (`<!--[if IE]> … <![endif]-->`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.",
4368+
"description": "Minify the markup inside a downlevel-hidden conditional comment (`<!--[if IE]> … <![endif]-->`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.",
43694369
"type": "boolean",
43704370
"added": "5.110.0"
43714371
},

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11930,13 +11930,13 @@ Object {
1193011930
"optimization-minimize-html-minify-conditional-comments": Object {
1193111931
"configs": Array [
1193211932
Object {
11933-
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.",
11933+
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.",
1193411934
"multiple": false,
1193511935
"path": "optimization.minimize.html.minifyConditionalComments",
1193611936
"type": "boolean",
1193711937
},
1193811938
],
11939-
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.",
11939+
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.",
1194011940
"multiple": false,
1194111941
"simpleType": "boolean",
1194211942
},
@@ -12727,13 +12727,13 @@ Object {
1272712727
"optimization-minimize-options-html-minify-conditional-comments": Object {
1272812728
"configs": Array [
1272912729
Object {
12730-
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.",
12730+
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.",
1273112731
"multiple": false,
1273212732
"path": "optimization.minimizeOptions.html.minifyConditionalComments",
1273312733
"type": "boolean",
1273412734
},
1273512735
],
12736-
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default: the body is minified on its own, so a context-sensitive decision inside it — which end tags are optional, where a table cell may sit — is taken as though it started a document rather than where the comment sits. Only browsers older than IE10 read these at all.",
12736+
"description": "Minify the markup inside a downlevel-hidden conditional comment (\`<!--[if IE]> … <![endif]-->\`). Off by default. The body is minified on its own with every tag it spells kept, and one whose print would still add, drop or move a tag — a wrapper another comment closes, a row of the table it sits in — is left as written, as it is read where the comment sits rather than as a document. Only browsers older than IE10 read these at all.",
1273712737
"multiple": false,
1273812738
"simpleType": "boolean",
1273912739
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigCacheTestCases html minimize-comments-and-whitespace minimize-comments-and-whitespace should compile 1`] = `"<!doctype html><head><title>Comments and whitespace</title></head><body><div>a b</div><div>c</div> <span> d </span><span> e </span> <!-- @license MIT: kept by pattern --> <!--[if IE 8]><p class=a>ie<![endif]--><pre> verbatim here </pre></body></html>"`;
3+
exports[`ConfigCacheTestCases html minimize-comments-and-whitespace minimize-comments-and-whitespace should compile 1`] = `"<!doctype html><head><title>Comments and whitespace</title></head><body><div>a b</div><div>c</div> <span> d </span><span> e </span> <!-- @license MIT: kept by pattern --> <!--[if IE 8]><p class=a>ie</p><![endif]--><pre> verbatim here </pre></body></html>"`;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigTestCases html minimize-comments-and-whitespace minimize-comments-and-whitespace should compile 1`] = `"<!doctype html><head><title>Comments and whitespace</title></head><body><div>a b</div><div>c</div> <span> d </span><span> e </span> <!-- @license MIT: kept by pattern --> <!--[if IE 8]><p class=a>ie<![endif]--><pre> verbatim here </pre></body></html>"`;
3+
exports[`ConfigTestCases html minimize-comments-and-whitespace minimize-comments-and-whitespace should compile 1`] = `"<!doctype html><head><title>Comments and whitespace</title></head><body><div>a b</div><div>c</div> <span> d </span><span> e </span> <!-- @license MIT: kept by pattern --> <!--[if IE 8]><p class=a>ie</p><![endif]--><pre> verbatim here </pre></body></html>"`;

‎test/configCases/html/minimize-comments-and-whitespace/test.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
expect(page).toContain("@license MIT");
2828
expect(page).not.toContain("ordinary chatter");
2929
// The markup inside the conditional comment was minified in place.
30-
expect(page).toContain("<!--[if IE 8]><p class=a>ie");
30+
expect(page).toContain("<!--[if IE 8]><p class=a>ie</p><![endif]-->");
3131
expect(page).toContain("<![endif]-->");
3232
}
3333
};

0 commit comments

Comments
 (0)