Skip to content

Commit 85d2390

Browse files
committed
fix(html): keep an end tag whose omission lets the adoption agency restructure the tree
§13.1.2.4 lets a p or li end tag go when nothing follows it in its parent, but inside a formatting element the element left open is exactly the furthest block the adoption agency looks for on the parent's end tag: <b><p>x</p></b> printed as <b><p>x</b> re-parses to a cloned <b> under the <p>. Keep the tag when the parent is a formatting element and the element is special; div and ul are unaffected.
1 parent d0a9320 commit 85d2390

8 files changed

Lines changed: 99 additions & 3 deletions

File tree

‎.changeset/157-html-minify-text-fixes.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-
Fix HTML minification losing a character after a CR line ending, dropping a body's leading whitespace when its tag is implied, and the parser case-folding the non-ASCII characters of an element or attribute name.
5+
Fix HTML minification losing a character after a CR line ending, dropping a body's leading whitespace when its tag is implied, omitting an end tag whose absence lets the parser's adoption agency restructure the tree, and the parser case-folding the non-ASCII characters of an element or attribute name.

‎lib/html/syntax.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7981,9 +7981,20 @@ const _canOmitEndTag = (path, name, node) => {
79817981
const next = path.nextSibling(node);
79827982
if (next === 0) {
79837983
if (!OPTIONAL_END_TAG_AT_END.has(name)) return false;
7984-
if (name !== "p") return true;
79857984
const parent = path.parentOf(node);
7986-
if (parent === 0 || path.namespace(parent) !== NS_HTML) return false;
7985+
const parentIsHtml = parent !== 0 && path.namespace(parent) === NS_HTML;
7986+
// Left open inside a formatting element, this is the furthest block the
7987+
// adoption agency needs on the parent's end tag — it would clone the
7988+
// parent and move this under it, so §13.1.2.4's permission does not hold.
7989+
if (
7990+
parentIsHtml &&
7991+
SPECIAL.has(name) &&
7992+
FORMATTING.has(path.tagName(parent))
7993+
) {
7994+
return false;
7995+
}
7996+
if (name !== "p") return true;
7997+
if (!parentIsHtml) return false;
79877998
const parentName = path.tagName(parent);
79887999
// An autonomous custom element is spelled with a hyphen.
79898000
return !P_KEEPS_END_TAG_IN.has(parentName) && !parentName.includes("-");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html minimize-end-tag-in-formatting minimize-end-tag-in-formatting should compile 1`] = `
4+
"<!doctype html><html><body>
5+
<b><p>paragraph</p></b>tail
6+
<em><li>item</li></em>rest
7+
<div><p>kept anyway</div>
8+
<b><span>not special</span></b>after
9+
10+
11+
</body></html>"
12+
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html minimize-end-tag-in-formatting minimize-end-tag-in-formatting should compile 1`] = `
4+
"<!doctype html><html><body>
5+
<b><p>paragraph</p></b>tail
6+
<em><li>item</li></em>rest
7+
<div><p>kept anyway</div>
8+
<b><span>not special</span></b>after
9+
10+
11+
</body></html>"
12+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "./page.html";
2+
3+
it("should keep an end tag whose omission would let the adoption agency run", () => {
4+
// The emitted file is the assertion — see the snapshot in test.config.js.
5+
expect(true).toBe(true);
6+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<b><p>paragraph</p></b>tail
5+
<em><li>item</li></em>rest
6+
<div><p>kept anyway</p></div>
7+
<b><span>not special</span></b>after
8+
</body>
9+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
findBundle(_i, options) {
8+
const files = fs.readdirSync(options.output.path);
9+
return files.includes("main.js") ? ["./main.js"] : undefined;
10+
},
11+
afterExecute(options) {
12+
expect(
13+
fs.readFileSync(path.join(options.output.path, "page.html"), "utf8")
14+
).toMatchSnapshot();
15+
}
16+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "production",
7+
output: {
8+
filename: "[name].js",
9+
pathinfo: false
10+
},
11+
module: {
12+
generator: {
13+
html: {
14+
extract: true
15+
}
16+
},
17+
parser: {
18+
html: {
19+
sources: false
20+
}
21+
}
22+
},
23+
optimization: {
24+
minimize: true,
25+
minimizer: ["..."]
26+
},
27+
experiments: {
28+
html: true
29+
}
30+
};

0 commit comments

Comments
 (0)