Skip to content

Commit 99dd2c7

Browse files
feat(minify): safe HTML minify transforms, driven by the spec grammars (#21586)
* fix(minify): keep HTML text intact across CR line endings Two defects made CRLF-authored HTML lose characters when minified. `leadingWs` split a character token by counting its decoded data against raw source offsets, so a run whose decoding shortened it (CRLF, or a character reference) got a text node whose span named the wrong source. The `Text` printer took `raw.length === data.length` to mean nothing was decoded, but preprocessing maps a lone CR to LF without changing length, so a bare CR reached the output -- and a truncated span from the first defect passed the same test, printing `<head>\r\n\t` as `<head>\r\n`. Validated against the html5lib suite (13379 cases). * feat(minify): drop attribute quotes an HTML value does not need §13.1.2.3 admits an unquoted value that is non-empty and free of whitespace, quotes, backtick, `=`, `<` and `>`. Values are serialized from their raw source span, so the quotes come off without re-encoding, and a character reference decodes the same either way. The tag was previously copied byte-for-byte so the sentinels webpack's own late asset passes leave behind stayed matchable. Those passes run after minification, so the one that assumed quotes -- the `integrity` sentinel -- now matches the quote optionally and back-references it to keep the pair symmetric; it already re-quoted its replacement, which an SRI list needs for its spaces and `=` padding. A third-party post-processor that regexes emitted HTML assuming quotes would still not match, which is why `removeAttributeQuotes` is off by default in html-minifier. Checked over every HTML fixture in `test/` (236 documents): the parsed DOM is unchanged in all of them and 188 got smaller, 2.97% overall. * feat(minify): drop HTML whitespace nothing renders Whitespace directly under `<head>` or `<html>` sits outside any block formatting context, so no styling makes it visible -- the same reasoning that already drops inert comments. `<body>`'s whitespace does render and is left alone. The CRLF regression tests moved from `<head>` to `<colgroup>`: all three insertion modes that split a leading whitespace run through `leadingWs` insert into `<head>`, `<html>` or `<colgroup>`, and only the last one still keeps the run where the split is observable. Both re-targeted tests were checked to still fail against the pre-fix parser. Over every HTML fixture in `test/` (237 documents): 533 inert nodes dropped, no other DOM change, 0.96% smaller. * feat(minify): omit an HTML end tag the next element implies §13.1.2.4 lets `</li>`, `</p>`, `</td>`, `</tr>`, `</option>` and the rest go when a qualifying element follows or nothing else is left in the parent. The printer previously materialized every one of them. `<html>`, `<head>` and `<body>` are deliberately left alone: their tags are omissible too, but non-browser consumers routinely mis-parse a document without them. Two conditions are easy to miss and both are enforced. A trailing `</p>` stays inside `a`/`audio`/`del`/`ins`/`map`/`noscript`/`video` and any autonomous custom element, whose content model would otherwise absorb what follows. And the qualifying sibling has to actually print a start tag -- a parser-implied `<tbody>` / `<tr>` is emitted transparently, so dropping `</thead>` in front of one would continue the section instead of closing it. Without that second guard eight table shapes re-parsed differently. The decision reads the tree, not the emitted text, so an intervening comment keeps the tag even though minification drops it -- bigger than necessary, never wrong. Checked by re-parsing: 304 constructed cases (every element crossed with every kind of following sibling, plus each `</p>` parent carve-out) and every HTML fixture in `test/`, with no DOM change beyond one `<noscript>` scripting-flag artifact that predates this change. * feat(minify): fold HTML casing and tags the parser re-implies Three transforms whose result re-parses to the identical tree. HTML tag and attribute names are ASCII-lowercased, which is exactly what parsing does to them. Only ASCII: `path.tagName()` folds with Unicode `toLowerCase`, which rewrote `<aÀ-豈>` to `<aà-豈>` -- a name the parser leaves alone. Foreign content keeps its source bytes entirely, because SVG admits element names no adjustment table would case-correct on the way back in, and because the interned foreign attribute name is html5lib's `xlink href` spelling rather than the serialized `xlink:href`. A `/` before `>` is dropped on HTML elements, where the parser ignores it. It survives on foreign elements, where it self-closes, and where it is the last character of an unquoted value. A `<tbody>` start tag goes when the parser re-implies it: the first child is a `<tr>`, it carries no attributes, and no preceding table section dropped its own end tag -- otherwise these rows continue that section. Re-parsed and compared over 304 constructed cases and every HTML fixture in `test/`: no DOM change, 11 fixtures smaller. * feat(minify): reduce srcset whitespace to what the grammar needs The candidate list is re-serialized through webpack's own `parseSrcset`, so a comma inside a URL (a `data:` URI, a `?w=1,2` query) is not mistaken for a separator the way a regex would. Two traps, both caught by checking the parsed candidate list before and after. A descriptor-less candidate keeps a space after its comma: its URL runs to the next whitespace, so `a.png, b.png 2x` collapsed to `a.png,b.png 2x` parses as one candidate named `a.png,b.png`. And `parseSrcset` throws on a value the grammar rejects, which now hands the value back untouched instead of failing the build. A value carrying a character reference is left alone -- it decodes to something the grammar reads differently -- as is foreign content, where `srcset` is not this attribute. This changes the attribute's bytes, so `getAttribute("srcset")` differs while the candidate list does not. Checked over every HTML fixture in `test/`: no candidate list changed. * feat(minify): minify an inline `<style>` body An inline sheet now goes through webpack's own CSS minifier -- the one that runs on CSS assets -- so it is held to exactly the same rules, shorthand collapsing and all. `lib/html/syntax.js` already lazily links `../css/syntax` for `parseCssUrls`, so this adds no dependency. Minified only when the body is CSS: a `type` other than `text/css` means it is a template, not a sheet. An empty body and anything the CSS parser rejects are handed back untouched. Foreign content is included, since an SVG `<style>` is CSS too. An inline `<script>` is left alone. Minifying JS needs terser, which reaches webpack only through `minimizer-webpack-plugin` and exposes an async API, while this printer is synchronous -- so it needs a design, not a patch. Checked over every HTML fixture in `test/`: nothing outside a `<style>` body changed. * chore(minify): note the inline-script minification that is still open * feat(minify): rewrite style, viewport and doctype through their grammars A `style` attribute is a declaration list, so it goes through webpack's CSS minifier inside a throwaway rule; a value that does not come back as exactly one rule escaped the wrapper (a stray `}`, a custom property holding braces) and keeps its source spelling. A `<meta name=viewport>` content list ignores the whitespace around its `,` / `;` / `=` separators, so that whitespace goes. No other `content` is touched -- a description is prose. Parsing ASCII-lowercases the doctype keyword and name, so both are folded. The public and system identifiers behind them are case-sensitive strings and stay as written, and the space after the keyword stays too: omitting it is a `missing-whitespace-before-doctype-name` parse error. A rewritten value now picks its own quote, since a minified declaration list can carry either kind (`grid-template-areas`). Checked over every HTML fixture in `test/`: every value still parses to the same declarations, candidates and pairs. * feat(minify): collapse a class list and a boolean attribute's value `class` is an ASCII-whitespace-separated token list, so its separators go to one space and the ends are trimmed. The token set the DOM reads is unchanged. A boolean attribute's presence is its value, so the canonical spellings (`disabled`, `disabled=""`, `DISABLED="DISABLED"`) lose theirs. Collapsing is gated on the elements each attribute is boolean *on*: `checked` means nothing on a `<div>`, where it is an ordinary attribute whose value a script may read. A value the spec does not canonicalize (`disabled="true"`) is left as written. Both change the attribute string while keeping what the DOM parses, so they are noted alongside the other value rewrites as wanting an option. Checked over every HTML fixture in `test/`: same token sets, same boolean presence, nothing else moved. * test(tooling): compare an app shell, and a `<style>` body as CSS Neither installed fixture carries an inline `<style>`, a `srcset` or a boolean attribute, so the comparison could not see what any minifier does with them. The added shell is the third real shape: weight in critical CSS and form markup. Its `<style>` body also exposed the fingerprint treating CSS as bytes, which reported every minifier that rewrites a sheet as losing text -- four of the six on that fixture. It is canonicalized through webpack's CSS minifier now, so the column reports what a sheet means. A CSS-level mistake is left to webpack's CSS suites; this tool checks the HTML around it, and it still catches the inline JS that htmlnano and swc rewrite. * refactor(minify): move the HTML minify tables into `lib/html/data.js` The optional-end-tag conditions, the boolean-attribute inventory and the attribute-kind sets now sit beside the algorithm rather than inside it, the way `lib/css/data.js` does. That one is generated from `mdn-data`; these are transcribed from the spec, and the file says so along with what a generator would still need — no dataset states "is this attribute boolean on this element" or the optional-tag conditions yet. The tree-construction tables (`VOID`, `SPECIAL`, `FORMATTING`, the scope sets) stay where they are: they are the parsing algorithm's own steps, not data a source could supply. Every minify transform moves to `configCases/html/minimize-transforms`, one snapshot over a corpus carrying each transform and each case it declines, so the assertion is the output a real build emits. The 452 lines of unit tests they replace are gone. The CR cases stay a unit test and say why: `.gitattributes` checks every `test/` file out with LF, so a CR only reaches the parser from a string built in the test. * fix(minify): keep a body's leading whitespace when its tag is implied A parser-implied `<body>` prints transparently, which puts a leading whitespace run where re-parsing drops it: before `<body>` starts, the insertion modes ignore whitespace. `</html> foo` came back as `foo`, losing a text node the source had. The tag materializes now when the run would otherwise be eaten. Found by running the html5lib serializer corpus (228 cases) through minify and comparing the parsed trees -- it flagged four documents, all this one bug. The behaviour predates this branch; `origin/main` loses the run too. * test(html): assert minification keeps a document's tree over a corpus A snapshot records what the minifier emits; it cannot say whether the output still means the same thing. `configCases/html/minimize-round-trip` parses each corpus document before and after minification and compares the trees, so a transform that changes meaning fails whatever the bytes look like. The comparison canonicalizes exactly what the transforms are licensed to rewrite -- a token list, a declaration list, a srcset's candidates, a viewport's pairs, a boolean attribute's presence, a `<style>` body, and the whitespace nothing renders -- and nothing else. Sixteen documents, one per construct family a minifier classically breaks: table sections, nested lists, option groups, the adoption agency, paragraph siblings, SVG and MathML integration points, `<template>` content, attribute value edge cases, verbatim text, conditional comments, CSS, srcset, quoting, an implied `<body>`, and a body whose text starts with whitespace. The corpus is ours rather than ported: the other minifiers publish no tests in their packages, and this environment cannot reach their repositories. Checked to have teeth against the parent commit, where the leading-whitespace document fails with the text node it lost. * chore: split the minify changeset by bump level and subject * feat(minify): strip the whitespace in a JSON `<script>` body `application/json`, `application/ld+json` (any `+json` subtype), `importmap` and `speculationrules` bodies are JSON, and JSON's whitespace is only between tokens. Stripping it needs no async minifier, so this is the one inline `<script>` body that does not wait on the terser question. Every literal is copied byte for byte rather than re-serialized. `JSON.parse` + `JSON.stringify` would round `12345678901234567890` to `...567000`, rewrite `1.0` to `1`, drop the first of two duplicate keys and turn `é` into `é` -- all meaning changes this has no business making. A body the JSON parser rejects is left alone, as is any other `type`. * feat(minify): collapse every whitespace-separated token list, not just class `rel`, `ping`, `headers`, `sandbox`, `itemprop`, `itemref`, `itemtype`, `accesskey`, `blocking` and `part` are the same kind of value as `class` -- an ASCII-whitespace-separated token list whose set is what the DOM reads -- so they take the collapse `class` already had. `for` stays out: on `<output>` it is a token list, but on `<label>` it is one id, and an id may contain a space. * feat(minify): normalize URL, integer and comma-list attribute values Three more grammars whose whitespace carries nothing. A URL attribute's ends are trimmed: the URL parser strips leading and trailing C0 controls and spaces before resolving, so the result is the same URL. An integer attribute loses its surrounding whitespace and leading zeros -- the parse rules skip the first and read the rest as base ten. Both are gated on the elements the attribute is an integer on: `width` on `<img>` is one, but on `<td>` the legacy dimension grammar also admits a percentage, so that one is left alone. A comma-separated list (`accept`, `coords`, `sizes`) loses the whitespace around its commas. `sizes` keeps the whitespace inside an item, which its media condition needs. The fixtures show no gain from any of this -- generated markup already emits tidy values -- but hand-written markup does not. * fix(test): match an unquoted src, and minify in-process for coverage Two things CI caught that local runs could not. `process-assets/html-plugin`'s inline plugin matched `<script src="...">` with the quotes required. It runs at `OPTIMIZE_INLINE`, after minification at `OPTIMIZE_SIZE`, so it stopped matching once the quotes went and the script was never inlined. The matcher takes them optionally now -- the same fix `INTEGRITY_SENTINEL_REGEXP` needed, and the second quote-assuming consumer this change has turned up. The minify config cases now run the minimizer with `parallel: false`. Its worker pool is another process, so the coverage instrument saw none of the minify code the cases exercise -- patch coverage read 64% for lines the tests do run. In-process the four cases cover 58.69% of `lib/html/syntax.js` on their own. * fix(html): fold and trim only ASCII in the attribute grammars The attribute rewrites trimmed with String#trim, which also strips NBSP and the other Unicode spaces the HTML integer, token-list, URL, comma-list and viewport grammars read as ordinary characters, so a leading NBSP could turn an unparsable integer into a valid one or drop a class token. Trim ASCII whitespace instead, and reject a signed value on a non-negative integer attribute, which its parse rules do not accept. The same applies to case folding: the parser's unknown-name fallback and the printer both used a Unicode toLowerCase, which maps U+212A onto k and U+0130 onto two characters. Fold ASCII only. * feat(html): complete the optional-tag rules and stop escaping a bare > Adds the three §13.1.2.4 omissions that were still missing: a <caption> and a <colgroup> end tag when no whitespace or comment follows them, and a <colgroup> start tag when a <col> opens it and no preceding colgroup dropped its own end tag. <html>, <head> and <body> stay out on purpose. A > in text is only ever a character, so escaping it costs bytes and buys nothing. Minification emits it bare; the default serialization keeps §13.3. * test(html): find inlined scripts with the parser, refresh the cache snapshot The html-plugin case matched <script src=...></script> with a regex, which CodeQL flagged as a tag filter that misses forms like </script >. It now walks webpack's own HTML parser, which gives the element's source offsets and the decoded src directly, so no tag pattern is needed. ConfigCacheTest.snap was left behind when the transforms corpus grew. * fix(html): keep the whitespace inside a comma-list item "Split a string on commas" strips each token's edges and nothing else, but the rewrite also deleted the whitespace inside an item of accept and coords. That changes what the DOM keeps: `accept="image/ png"` is an invalid token the browser holds verbatim and became the valid `image/png`, and coords parses whitespace as a separator, so `1 2,3` went from three numbers to two. Trim the edges only, which is what sizes already did — the three attributes now share one rule, so the table is a Set.
1 parent 572fd32 commit 99dd2c7

53 files changed

Lines changed: 1929 additions & 151 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Minify HTML further where the parsed document is unchanged: drop redundant attribute quotes, whitespace nothing renders, escapes text does not need, tags the parser re-implies and casing it folds, and rewrite `style`, token lists, `srcset`, `sizes`, URL and integer attributes, viewport `content`, boolean attributes, inline `<style>` and a JSON `<script>` body through their own grammars.
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+
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.

‎.gitattributes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ test/configCases/html/legacy-and-obsolete-sources/** eol=lf
2222
test/configCases/html/webmanifest-icons/** eol=lf
2323
test/configCases/html/webmanifest-build-http/** eol=lf
2424
test/configCases/html/minimize-attributes/** eol=lf
25+
test/configCases/html/minimize-integrity/** eol=lf
26+
test/configCases/html/minimize-transforms/** eol=lf
27+
test/configCases/html/minimize-round-trip/** eol=lf
2528
examples/* eol=lf
2629
bin/* eol=lf
2730
*.svg eol=lf

‎cspell.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"externref",
119119
"fetchpriority",
120120
"fontobject",
121+
"formnovalidate",
121122
"filebase",
122123
"fileoverview",
123124
"filepath",
@@ -154,6 +155,9 @@
154155
"instanceof",
155156
"inversed",
156157
"itemprop",
158+
"itemref",
159+
"itemtype",
160+
"itemscope",
157161
"imagesrcset",
158162
"jhnns",
159163
"jrburke",
@@ -176,6 +180,7 @@
176180
"lcovonly",
177181
"Flexbox",
178182
"rrggbb",
183+
"rowspan",
179184
"xrrggbb",
180185
"lightningcss",
181186
"lintunit",
@@ -185,6 +190,7 @@
185190
"longtest",
186191
"lowlink",
187192
"mangleable",
193+
"maxlength",
188194
"MCEP",
189195
"mebibytes",
190196
"medikoo",
@@ -226,6 +232,7 @@
226232
"nonrecursive",
227233
"nosource",
228234
"nosources",
235+
"novalidate",
229236
"nwjs",
230237
"oklab",
231238
"oklch",
@@ -245,6 +252,7 @@
245252
"performant",
246253
"Phoscur",
247254
"parseargs",
255+
"playsinline",
248256
"pkgjs",
249257
"pmodule",
250258
"pnpm",
@@ -312,6 +320,7 @@
312320
"sourcemap",
313321
"sourcemapped",
314322
"spectest",
323+
"speculationrules",
315324
"splitted",
316325
"srcset",
317326
"starstar",
@@ -406,6 +415,7 @@
406415
"commithash",
407416
"contenturl",
408417
"formaters",
418+
"formaction",
409419
"akait",
410420
"evenstensberg",
411421
"Stensberg",

‎lib/html/HtmlGenerator.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,17 @@ const ASSET_URL_SENTINEL_REGEXP =
250250
const integritySentinelCompilations = new WeakSet();
251251

252252
// Matches the whole ` integrity="<sentinel>"` so the attribute can be dropped
253-
// entirely when the per-asset `integrity` function returns `false`.
253+
// entirely when the per-asset `integrity` function returns `false`. Minification
254+
// runs first and may have unquoted the value (§13.1.2.3), so the quote is
255+
// optional and back-referenced to keep the pair matched; the replacement always
256+
// re-quotes, since an SRI list carries spaces and `=` padding.
254257
const INTEGRITY_SENTINEL_REGEXP =
255-
/ integrity="__WEBPACK_HTML_INTEGRITY__([0-9a-f]+)__([a-z]+)__END__"/g;
258+
/ integrity=(["']?)__WEBPACK_HTML_INTEGRITY__([0-9a-f]+)__([a-z]+)__END__\1/g;
256259

257260
// Same attribute, but tolerant of the `\"` escaping the surrounding quotes get
258261
// when the HTML lives inside a JS string literal — used only to strip it there.
259262
const INTEGRITY_SENTINEL_STRIP_REGEXP =
260-
/ integrity=\\?"__WEBPACK_HTML_INTEGRITY__[0-9a-f]+__[a-z]+__END__\\?"/g;
263+
/ integrity=(\\?["']|)__WEBPACK_HTML_INTEGRITY__[0-9a-f]+__[a-z]+__END__\1/g;
261264

262265
// The trailing `(?:__<hash>)?` is the inlined chunk's content hash, embedded by
263266
// `embedInlineChunkHashes` so the host HTML's bytes (and its `[contenthash]`,
@@ -534,7 +537,7 @@ class HtmlGenerator extends Generator {
534537
const chunksById = getChunksById(compilation);
535538
return content.replace(
536539
INTEGRITY_SENTINEL_REGEXP,
537-
(_, hexId, contentHashType) => {
540+
(_, _quote, hexId, contentHashType) => {
538541
const chunkId = Buffer.from(hexId, "hex").toString("utf8");
539542
const chunk = chunksById.get(chunkId);
540543
if (!chunk) return "";

0 commit comments

Comments
 (0)