Skip to content

Commit 7a63ecc

Browse files
fix: HTML style and <noscript> minifying, inline ;;, and mergeDistantRules cascade (#22370)
* fix(html): keep a style attribute's lowered CSS however its source spelled it The CSS minifier's answer is written whenever it differs from what the source decodes to, at its shortest encoding, instead of only where it beat the source's raw bytes. Run the html5lib CDATA tokenizer cases through the tree builder and retire two HTML invariant exceptions. * fix(css): quote a style attribute's strings so the list holds one quote kind A string holding an apostrophe is written '-wrapped with \' escapes in a block-contents print, so the attribute escapes nothing. * fix(html): keep one `;` where a minified inline chunk ends its statement * fix(html): print `<noscript>` as a scripting-on browser reads it A head `<noscript>` read with scripting off lost its content to the body. The wpt script-on tree cases now run under the flag. * fix(css): merge distant rules past rules matching other element types A rule whose subjects all name other element types never reaches an element the moved selectors match. A block setting `all` no longer moves past what declares anything since it was held. * fix(css): keep a distant merge's grown list in the look-back join's record The neighbor join read a list the merge grew by its old length and cut it there, folding half a selector into the rule before it. * test(css): hold mergeDistantRules to the cascade every element computes Each stylesheet is printed with and without the merge, and every element its selectors reach is read under both, over a seeded corpus, configCases and the benchmark stylesheets. * fix(html): keep the head whitespace a scripting-off reader reads as body text Past a head `<noscript>` holding what the head cannot, such a reader has left the head, so the whitespace after it is text in the body. * test(css): adopt each cascade sheet rather than parse it from a <style> Gecko leaves a <style> element's sheet null a while after an @import. * fix(html): close a noscript comment where the tokenizer does An abrupt `<!-->` or `<!--->` ends at once and `--!>` closes one too, so text between them leaves the head for a reader with scripting off. * test(css): name where an element differing in the cascade tier stands * test(css): build no second html or body and compare only rendered elements WebKit styles a nested <html>/<body> and a box-less element apart from the tree around them, which read as cascade differences no page could show. * test(javascript): spell the parse suite's import apart from loadSources V8 hands back the function first compiled from the same text, bound to the Jest environment that compiled it, so the suite failed whenever it ran after syntax-printer.unittest.js in one worker. * test(css): restyle the cascade probe's tree under each sheet WebKit kept the Materialize probe root's inherited font and custom properties from before its sheet applied, so the unmerged read differed from the merged one on rules both prints hold alike. Reinserting the root before each read resolves its subtree afresh against the sheet then adopted. * test(css): insert the cascade probe's root once both sheets are adopted Reinserting the root before each read reloaded replaced elements between the reads, so Firefox laid the document out at different heights for one sheet. Building the tree detached and inserting it after the adoptions styles the root afresh, which is what WebKit needed, and leaves the reads one layout.
1 parent 26f5356 commit 7a63ecc

25 files changed

Lines changed: 1463 additions & 113 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+
Fix minified HTML for `style` spellings, CSS string quotes and `<noscript>`.
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+
Stop a minified inlined chunk leaving a doubled `;` in its `<script>`.
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+
Stop `mergeDistantRules` mangling rules or reordering `all`, and merge more.

‎declarations/WebpackOptions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,7 @@ export interface OptimizationMinimizeCss {
31533153
*/
31543154
lowerUnsupported?: boolean;
31553155
/**
3156-
* Give a rule the selectors of a later one printing the same block, and an at-rule the block of a later one stating the same condition, past the rules standing between them. Off by default because it reorders the cascade, so it holds only where nothing between the two declares a property the block being moved does — a condition between counts for what its own rules declare, while `@layer` and anything else whose declarations cannot be read stands in the way whatever it says. A block repeated at a distance already compresses on its own, so a selector join is taken only where the copy of the block it drops is worth more than twice the selector it writes instead; an at-rule join writes no selector and drops the repeated prelude outright. A named `@layer` is not joined this way — its place in the cascade is where its name is first written, not what its prelude states. `mergeRules` is the safe half of this, joining only what nothing stands between.
3156+
* Give a rule the selectors of a later one printing the same block, and an at-rule the block of a later one stating the same condition, past the rules standing between them. Off by default because it reorders the cascade, so it holds only where nothing between the two declares a property the block being moved does for an element its selectors can match (a rule whose selectors all name other element types, `h3` between two rules for `h1` and `h2`, never can) — a condition between counts for what its own rules declare, while `@layer` and anything else whose declarations cannot be read stands in the way whatever it says. A block repeated at a distance already compresses on its own, so a selector join is taken only where the copy of the block it drops is worth more than twice the selector it writes instead; an at-rule join writes no selector and drops the repeated prelude outright. A named `@layer` is not joined this way — its place in the cascade is where its name is first written, not what its prelude states. `mergeRules` is the safe half of this, joining only what nothing stands between.
31573157
* @since 5.111.0
31583158
*/
31593159
mergeDistantRules?: boolean;

‎lib/css/syntax-parser.js‎

Lines changed: 185 additions & 46 deletions
Large diffs are not rendered by default.

‎lib/css/syntax-printer.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,29 @@ const _minifyIdentEscapes = (text) => {
17851785
const _urlValue = (path, unterminated) =>
17861786
unterminated ? _terminate(path.value(), "", "\uFFFD") : path.value();
17871787

1788+
/**
1789+
* Write a string holding a `'` as `'`-wrapped, with each `'` inside escaped and
1790+
* each `\"` unescaped: a `"` costs a `style=""` six bytes as `&quot;`, a `\'` one.
1791+
* @param {string} raw the string token's source text, quotes included, which
1792+
* the printer has already closed where the input ran out inside it
1793+
* @returns {string} the string token
1794+
*/
1795+
const _minifyAttributeString = (raw) => {
1796+
const n = raw.length;
1797+
let out = "'";
1798+
for (let i = 1; i < n - 1; i++) {
1799+
const c = raw.charCodeAt(i);
1800+
if (c === CC_REVERSE_SOLIDUS) {
1801+
const next = raw.charCodeAt(i + 1);
1802+
out += next === CC_QUOTATION_MARK ? '"' : raw.slice(i, i + 2);
1803+
i++;
1804+
} else {
1805+
out += c === CC_APOSTROPHE ? "\\'" : raw[i];
1806+
}
1807+
}
1808+
return `${out}'`;
1809+
};
1810+
17881811
/**
17891812
* Normalize a string token's quotes, following cssnano's `postcss-normalize-string`:
17901813
* prefer `"`, switch to whichever quote needs fewer escapes, and unescape a quote
@@ -1804,6 +1827,12 @@ const _minifyString = (raw) => {
18041827
// A string the tokenizer closed at EOF has no matching final quote; rewriting
18051828
// one would move where it ends.
18061829
if (n < 2 || raw.charCodeAt(n - 1) !== quote) return raw;
1830+
if (_S._inStyleAttribute) {
1831+
const apostrophe = raw.indexOf("'", 1);
1832+
if (apostrophe !== -1 && apostrophe < n - 1) {
1833+
return _minifyAttributeString(raw);
1834+
}
1835+
}
18071836
// The overwhelmingly common string — already `"`-wrapped, no escape and no
18081837
// literal `'` — is unchanged; two native scans beat the counting loop below.
18091838
if (

‎lib/html/HtmlGenerator.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ const INTEGRITY_SENTINEL_STRIP_REGEXP =
256256
// The resolver ignores it.
257257
const INLINE_SENTINEL_REGEXP =
258258
/__WEBPACK_HTML_INLINE__([0-9a-f]+)__([a-z]+)(?:__[0-9a-f]+)?__END__/g;
259+
// The same, with the `;` a minifier ends the sentinel's statement with, which a
260+
// chunk ending in its own `;` would double.
261+
const INLINE_SENTINEL_STATEMENT_REGEXP =
262+
/__WEBPACK_HTML_INLINE__([0-9a-f]+)__([a-z]+)(?:__[0-9a-f]+)?__END__(;?)/g;
259263

260264
/**
261265
* Rewrite relative `url(...)` references in a CSS chunk so they still resolve
@@ -653,11 +657,11 @@ class HtmlGenerator extends Generator {
653657
const chunksById = getChunksById(compilation);
654658
const outputPath = /** @type {string} */ (compilation.outputOptions.path);
655659
return content.replace(
656-
INLINE_SENTINEL_REGEXP,
657-
(_, hexId, contentHashType) => {
660+
INLINE_SENTINEL_STATEMENT_REGEXP,
661+
(_, hexId, contentHashType, terminator) => {
658662
const chunkId = Buffer.from(hexId, "hex").toString("utf8");
659663
const chunk = chunksById.get(chunkId);
660-
if (!chunk) return "";
664+
if (!chunk) return terminator;
661665
const isCss = contentHashType === "css";
662666
let out = "";
663667
for (const file of chunk.files) {
@@ -683,7 +687,7 @@ class HtmlGenerator extends Generator {
683687
out += raw;
684688
if (inlinedFiles) inlinedFiles.add(file);
685689
}
686-
return out;
690+
return out.endsWith(";") ? out : out + terminator;
687691
}
688692
);
689693
}

‎lib/html/data.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ const QUIRKY_EXACT = new Set([
15881588
]);
15891589

15901590
/**
1591-
* Elements whose text children serialize literally, no escaping. `textarea` / `title` are escapable, so they are not here.
1591+
* Elements whose text children serialize literally, no escaping. `textarea` / `title` are escapable, so they are not here. `noscript` holds text only as a printed document is parsed, with scripting on.
15921592
* @type {Set<string>}
15931593
*/
15941594
const LITERAL_TEXT_PARENTS = new Set([
@@ -1598,6 +1598,7 @@ const LITERAL_TEXT_PARENTS = new Set([
15981598
"iframe",
15991599
"noembed",
16001600
"noframes",
1601+
"noscript",
16011602
"plaintext"
16021603
]);
16031604

0 commit comments

Comments
 (0)