Skip to content

Commit ab83631

Browse files
fix(css,html): print input ending mid-construct and emptied inline CSS in the form a second pass keeps (#22290)
* fix(css,html): close what the input left open at its end The invariants sweep now also asks idempotence of each source cut short, since the end of the input is where the tokenizer and parser close what the source left open, and a printer writing after it has to close it too. It found: - CSS: a custom property value, or text kept as written, that ran into EOF inside a bracket, string, url, comment or after a `\` was echoed open, and the rule's `}` printed after it was read inside it: Chromium dropped the declaration, and each pass added another `}`. It is closed in a stylesheet, and left as written in a `style=""`, where nothing is printed after it and Chromium keeps it open. - CSS: a url cut off at EOF was closed instead of normalized, so a second pass lowercased or trimmed it; a `\` it ended on escaped its `)`. - HTML: a kept comment or `<?…` left open at EOF swallowed the `</body></html>` printed after it; it is closed from its data. - HTML: an end tag the input ended inside (`</p`, `</br`) was emitted, building the element a stray one inserts; §13.2.5.8 drops it, as the start tag case already did. Not in raw text, which reads it as text. - HTML: `sortAttributes` stopped short on an attribute the print drops, which had no rank, so a second pass finished the sort. The cuts add about two seconds to the CSS sweep: each keeps only the top-level construct it falls in, the first one small enough. Three HTML findings in the same family — `removeEmptyElements` and `mergeStyles` decide on the source before the renderer has emptied or closed a body — are recorded as owed expectations rather than fixed here. * fix(html): decide style emptiness and merging on the rendered CSS removeEmptyElements, removeEmptyAttributes and mergeStyles judged a <style> or style attribute on its source, so one the renderer minified to nothing, or closed, only changed on a second pass. The async path prints again with the answers in hand when one overturns such a choice. * fix(html): close a <!-- comment ending in > at end of input A <!-- comment reads > as data, so one the input ended inside after a > was left open and swallowed the end tags printed after it. Also reprint when a deferred sheet answer leaves open a sheet its source closed, cut the CSS sweep inside comments, and fold the changesets into one. * test: read a held value last in the syntax-equivalence probe A value left open swallowed the declaration written after it, so the raw side set nothing once the printer started closing what the input left open. * test: file WebKit's paint() read through the custom property probe The two values WebKit already has filed for the unimplemented paint() now reach the var() path too, since the printer closes what they leave open.
1 parent 8735b62 commit ab83631

40 files changed

Lines changed: 1299 additions & 356 deletions

‎.changeset/036-css-idempotent-output.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
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+
Make minified CSS and HTML stable on a second pass, even for truncated input.

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ This is the canonical repository map. **When you add, rename or remove a top-lev
119119
- `FIXTURE=`, `TOOL=`, `STAGE=` narrow the rows.
120120
- **Corpora** are declared in `tooling/comparison/<cache name>/`: a `package.json` pinning every package exactly and a `package-lock.json` freezing the resolution, transitive deps included, so a sweep runs what was committed rather than what the registry serves that day, and a finding names a reproducible corpus. `installPackages` runs `npm ci` into `node_modules/.cache/<cache name>/` (not webpack's deps) on first run, skipped while the lockfile it last installed from matches; expect ~a minute of install first, and 10+ minutes per full run. Bump a corpus by editing its manifest, never the script. Dependabot watches the three directories, so a bump arrives as its own PR — where a break is attributable, not on somebody else's — and there the `invariants` job — which installs and sweeps all three — reds on a broken printer invariant, an `npm ci`-refused manifest/lockfile, or a platform package the lockfile misses (`yarn benchmark:<language>-tools:setup` is the install alone). Each corpus keeps an `.npmrc` with `package-lock=true`: Dependabot reads npm config from parent directories (npm doesn't), so otherwise the root's `package-lock=false` (right for yarn's tree) leaves a bump's lockfile unregenerated — exactly what `npm ci` refuses.
121121
- **Invariants.** Each comparison first holds webpack's implementation to five relations over its own input — `test/**/*.css` / `*.html` / `*.js`, its own fixtures, whatever the cache holds — in seconds, before any install. `--invariants` runs only that and exits non-zero on any finding; the `invariants` job gates every PR on it (`yarn test:invariants`, or `:css` / `:html` / `:js`). A relation is cheap to hold, so a regression fails rather than reports. `FIXTURE=`, `RELATION=`, `PRESET=`, `SPELLING=` narrow a run.
122-
1. Printer, idempotence: minifying already-minified output changes nothing, under every option set the webpack rows use.
122+
1. Printer, idempotence: minifying already-minified output changes nothing, under every option set the webpack rows use. **Also asked of each source cut short** — inside and just after one token of each kind (CSS) or inside each construct (HTML): the end of input is where the tokenizer and parser close what the source left open, and a printer writing after it (a rule's `}`, a document's `</body>`) must close it too, or the next pass reads that inside it. A CSS cut keeps only the first small-enough top-level construct it falls in, so the cuts cost seconds. They are why a value written as it stands is closed where a `}` follows it and left open in a `style=""`, where nothing does and Chromium keeps it open.
123123
2. Printer, spelling: how a value was spelled doesn't decide what it minifies to, under the first option set (the question is the same under each; each asking is a full minify). **A respelling may only rewrite what the spec calls equivalent**: HTML — a delimiter, a character reference, name case; CSS — an escape (§4.3.7), a string's delimiter, a leading zero, unit and at-rule-name case. Identifiers are never case-respelled: which kind one is depends on position, and class, id, custom property, font family and counter name are case-sensitive. A custom property's value is skipped whole (`var()` substitutes it elsewhere and the spec keeps it as authored) — the one region where spelling decides output on purpose, as a tag another language writes into is skipped in HTML. **Outputs are compared by meaning, not bytes**, through a stream resolving each token's value (`8PX` ≡ `8px`, `"a"` ≡ `'a'`); a shorter-or-equal output saying the same isn't a finding, a longer one is. HTML respellings are lexical and checked against webpack's tokenizer first, dropping any that moved the document; whitespace inside a tag isn't one (a tag nothing beats is echoed as written), and a tag another language writes into is skipped whole (`=""` on `{% endif %}` restates nothing). Each finding is bisected to what carries it, re-run on the enclosing tag alone to name a repro, and grouped by it — one defect reaches hundreds of pages. (`method=GET` folding only where quoted, fixed in #22095, was found this way.)
124124
3. Parser, `spans`: every node's range against its source — nothing inverted or past the end, a node inside its holder, siblings not overlapping, every sub-range (CSS declaration name and block, HTML opening tag, each attribute) inside the node. **The exclusions are the specs, not slack**: CSS owes all of it. HTML owes neither containment nor sibling non-overlap — §13.2 ends an element at its start tag until an end tag is read (`<html>` omitting `</html>` ends before its `<body>`) and closes an open element at the next tag (`<p>a<p>b` shares bytes); asking reports 4031 and 22 findings, none a defect. JS owes containment but not sibling non-overlap (ESTree aliases a shorthand property's key and value over one range).
125125
4. Parser, `slices`: the bytes between a node's offsets, parsed alone, give the node back. **A slice is parsed in a context keeping its meaning, never at top level** (`yield x` is an identifier outside a generator; a lone `<td>x</td>` is dropped and its text foster-parented): CSS wraps a declaration in a rule and a value in a declaration; JS tries a ladder of function and class-method contexts under the source's own `ecmaVersion` (`function*(){}` can't open a statement; an ES6-pinned case can't parse `async function*`); HTML runs the fragment parsing algorithm on the enclosing tag, as browsers do for `innerHTML`. A node no context parses is counted, not reported. Left out (§13.2 again): an element the parser inserted or cloned (no tag to slice), and one whose end is still its start tag's (its range doesn't hold its children). **Two limits keep it to seconds**: one shape is one question (a bundle's millionth `Identifier[0,3)` is skipped — CSS went 30s → 4.6s), and each source may spend at most four times its bytes on reparsing, leaves first; both skip counts are reported, as unread coverage. The JS sweep also reads `fixtures/acorn-corpus.json` under each case's options, reaching productions no fixture writes; sources the parser refuses are counted, not reported (those cases assert the refusal).

‎lib/css/syntax-parser.js‎

Lines changed: 131 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10961,27 +10961,17 @@ const _customPropertyToken = (path, child, writer) => {
1096110961
if (_S._rewriteCustomProperties) return writer.get(child);
1096210962
const source = path.source(child);
1096310963
const type = path.type(child);
10964-
// The input ran out inside this token, so the engine closed it there and the
10965-
// printer has to as well — otherwise the `}` written after it is read as part
10966-
// of the token rather than as the end of the rule.
10967-
if (path.end(child) === _S._input.length) {
10968-
// A url token holds a value its text no longer spells only when an escape
10969-
// was truncated; `url(foo` the engine echoes open, and so does this.
10970-
if (
10971-
type === T_URL &&
10972-
source !== _S._input.slice(path.start(child), path.end(child))
10973-
) {
10974-
return `${source})`;
10975-
}
10976-
if (type === T_STRING && !_isClosedString(source)) {
10977-
return source + source[0];
10978-
}
10979-
}
10964+
// WHY: Chromium keeps a value the input ran out inside as written, open, but
10965+
// in a stylesheet the rule's `}` is printed after it, lands inside what is
10966+
// open and loses the whole declaration — so there it is closed. A `style=""`
10967+
// list prints nothing after it, and there it stays as the engine keeps it.
10968+
const closes =
10969+
_blockContentsNodes === null && path.end(child) === _S._input.length;
1098010970
if (
1098110971
(type !== T_FUNCTION && type !== T_SIMPLE_BLOCK) ||
1098210972
!CUSTOM_PROPERTY_REWRITABLE_RE.test(source)
1098310973
) {
10984-
return source;
10974+
return closes ? _closeAtEof(source) : source;
1098510975
}
1098610976
const start = path.start(child);
1098710977
const end = path.end(child);
@@ -10992,16 +10982,29 @@ const _customPropertyToken = (path, child, writer) => {
1099210982
opened = start + 1;
1099310983
closer = block === "[" ? "]" : block === "{" ? "}" : ")";
1099410984
}
10995-
// Closed at EOF: there is no closer to write back, and it ends the value.
10996-
const closed = _S._input[end - 1] === closer;
10985+
// Closed at EOF, the source has no closer to strip, and one is written only
10986+
// for the reason above. Read off the children, since a string running to EOF
10987+
// can end on the closer's character (`f(")`) without closing anything.
10988+
const i0 = _nodeIndex(child);
10989+
const count = _listLens[i0];
10990+
const closed =
10991+
(count === 0
10992+
? opened
10993+
: path.end(
10994+
/** @type {ComponentValue} */ (
10995+
_nodeRef(_flat[_listStarts[i0] + count - 1])
10996+
)
10997+
)) < end;
1099710998
const inner = _customPropertyValue(
1099810999
path,
1099911000
child,
1100011001
writer,
1100111002
opened,
1100211003
closed ? end - 1 : end
1100311004
);
11004-
return `${_S._input.slice(start, opened)}${inner}${closed ? closer : ""}`;
11005+
return `${_S._input.slice(start, opened)}${inner}${
11006+
closed || closes ? closer : ""
11007+
}`;
1100511008
};
1100611009

1100711010
/**
@@ -13221,6 +13224,111 @@ const _isClosedString = (text) => {
1322113224
return true;
1322213225
};
1322313226

13227+
/**
13228+
* Write a token's terminator back. A `\` left dangling at EOF has to be replaced
13229+
* rather than kept: kept, it would escape the terminator and leave the token open
13230+
* on exactly the input this repairs. What it stands for differs by token — nothing
13231+
* in a string (§4.3.5), U+FFFD in a url (§4.3.6 reads it as an escape, §4.3.7 ends
13232+
* one at EOF with the replacement character).
13233+
* @param {string} raw the token's source text, missing its terminator
13234+
* @param {string} terminator the character that closes it
13235+
* @param {string} dangling what a `\` left at EOF contributed to the token's value
13236+
* @returns {string} the terminated token
13237+
*/
13238+
const _terminate = (raw, terminator, dangling) => {
13239+
let backslashes = 0;
13240+
while (
13241+
raw.length - 1 - backslashes > 0 &&
13242+
raw.charCodeAt(raw.length - 1 - backslashes) === CC_REVERSE_SOLIDUS
13243+
) {
13244+
backslashes++;
13245+
}
13246+
const body = backslashes % 2 === 1 ? `${raw.slice(0, -1)}${dangling}` : raw;
13247+
return `${body}${terminator}`;
13248+
};
13249+
13250+
/**
13251+
* The `url()` counterpart of `_isClosedString`: §4.3.6 ends a url-token at EOF, so
13252+
* it has no closing `)` and the printer's next byte would land inside it.
13253+
* @param {string} raw the url-token's source text
13254+
* @returns {boolean} true when the `)` is absent or itself escaped
13255+
*/
13256+
const _isUnterminatedUrl = (raw) => {
13257+
const n = raw.length;
13258+
if (n < 2 || raw.charCodeAt(n - 1) !== CC_RIGHT_PARENTHESIS) return true;
13259+
let backslashes = 0;
13260+
for (let i = n - 2; i > 0 && raw.charCodeAt(i) === CC_REVERSE_SOLIDUS; i--) {
13261+
backslashes++;
13262+
}
13263+
return backslashes % 2 === 1;
13264+
};
13265+
13266+
/**
13267+
* Text the input ran out inside, closed where the tokenizer and parser closed it
13268+
* (§4.3, §5.4): its last token's terminator, then each block and function still
13269+
* open, innermost first. Printed as written instead, the next byte the printer
13270+
* writes — a `;` or the rule's `}` — would be read as part of what was left open.
13271+
* @param {string} text source text that runs to the end of the input
13272+
* @returns {string} the text with what it left open closed
13273+
*/
13274+
const _closeAtEof = (text) => {
13275+
const stream = new TokenStream(text);
13276+
/** @type {string[]} */
13277+
const open = [];
13278+
let lastType = TT_EOF;
13279+
let lastStart = 0;
13280+
let lastEnd = 0;
13281+
for (;;) {
13282+
const token = stream.consume();
13283+
const type = token.type;
13284+
if (type === TT_EOF) break;
13285+
lastType = type;
13286+
lastStart = token.start;
13287+
lastEnd = token.end;
13288+
if (type === TT_FUNCTION || type === TT_LEFT_PARENTHESIS) {
13289+
open.push(")");
13290+
} else if (type === TT_LEFT_SQUARE_BRACKET) {
13291+
open.push("]");
13292+
} else if (type === TT_LEFT_CURLY_BRACKET) {
13293+
open.push("}");
13294+
} else if (
13295+
// §5.4.8 ends a block only on its own closer; any other is a token in it.
13296+
open.length !== 0 &&
13297+
((type === TT_RIGHT_PARENTHESIS && open[open.length - 1] === ")") ||
13298+
(type === TT_RIGHT_SQUARE_BRACKET && open[open.length - 1] === "]") ||
13299+
(type === TT_RIGHT_CURLY_BRACKET && open[open.length - 1] === "}"))
13300+
) {
13301+
open.pop();
13302+
}
13303+
}
13304+
let out = text;
13305+
// Comments are consumed between tokens, so one left open is what follows the last.
13306+
let at = lastEnd;
13307+
for (;;) {
13308+
while (at < out.length && isWhitespace(out.charCodeAt(at))) at++;
13309+
if (!out.startsWith("/*", at)) break;
13310+
const end = out.indexOf("*/", at + 2);
13311+
if (end === -1) {
13312+
out += "*/";
13313+
break;
13314+
}
13315+
at = end + 2;
13316+
}
13317+
if (out === text && lastEnd === text.length) {
13318+
const last = text.slice(lastStart, lastEnd);
13319+
if (lastType === TT_STRING && !_isClosedString(last)) {
13320+
out = text.slice(0, lastStart) + _terminate(last, last[0], "");
13321+
} else if (lastType === TT_URL && _isUnterminatedUrl(last)) {
13322+
out = text.slice(0, lastStart) + _terminate(last, ")", "�");
13323+
} else {
13324+
// §4.3.7 reads a `\` at EOF as U+FFFD; kept, it would escape the closer.
13325+
out = _terminate(text, "", "�");
13326+
}
13327+
}
13328+
for (let i = open.length - 1; i >= 0; i--) out += open[i];
13329+
return out;
13330+
};
13331+
1322413332
/**
1322513333
* A selector list is a set: the same selectors in any order match the same
1322613334
* elements at the same specificity, so a repeat inside it says nothing and one
@@ -15118,6 +15226,7 @@ module.exports._anyUnordered = _anyUnordered;
1511815226
module.exports._appendChildTexts = _appendChildTexts;
1511915227
module.exports._atKeyword = _atKeyword;
1512015228
module.exports._blockSpans = _blockSpans;
15229+
module.exports._closeAtEof = _closeAtEof;
1512115230
module.exports._collapseBox = _collapseBox;
1512215231
module.exports._composeBlockBody = _composeBlockBody;
1512315232
module.exports._customMedia = _customMedia;
@@ -15137,6 +15246,7 @@ module.exports._isHexDigit = _isHexDigit;
1513715246
module.exports._isIdentCodePoint = _isIdentCodePoint;
1513815247
module.exports._isIdentLike = _isIdentLike;
1513915248
module.exports._isLetter = _isLetter;
15249+
module.exports._isUnterminatedUrl = _isUnterminatedUrl;
1514015250
module.exports._join = _join;
1514115251
module.exports._lowercaseConditionParts = _lowercaseConditionParts;
1514215252
module.exports._minifyColorFunction = _minifyColorFunction;
@@ -15161,6 +15271,7 @@ module.exports._splitTopLevelArguments = _splitTopLevelArguments;
1516115271
module.exports._splitTopLevelSpaces = _splitTopLevelSpaces;
1516215272
module.exports._stream = _stream;
1516315273
module.exports._takeUnusedRule = _takeUnusedRule;
15274+
module.exports._terminate = _terminate;
1516415275
module.exports._tokenizeCalc = _tokenizeCalc;
1516515276
module.exports._unreadableColorFunction = _unreadableColorFunction;
1516615277
module.exports._valueClasses = _valueClasses;

0 commit comments

Comments
 (0)