Skip to content

Commit 1f66026

Browse files
fix(css,html): keep descriptors opaque under streaming, decline a trailing escape
A block that streams entered its rule outside the walk that set the `@property` / `@function` state, so `initial-value` and `result` were printed with ordinary value transforms once the block grew past the threshold. Carry the state per stream frame instead. A `style` attribute ending in an escape takes the wrapper rule's own `}` as an escaped brace, so it is declined rather than minified. Cover both, plus the escape and closed-pair arcs of the wrapper test, the non-minified text escapes and the two generator staleness guards.
1 parent 450d81d commit 1f66026

13 files changed

Lines changed: 114 additions & 26 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,10 @@ const _frameDecls = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
42524252
/** @type {(Rule[] | null)[]} */
42534253
const _frameRules = Array.from({ length: _STREAM_MAX_DEPTH }, () => null);
42544254
const _frameParentIndex = new Int32Array(_STREAM_MAX_DEPTH);
4255+
// The `@property` / `@function` state this frame was entered under. A streamed
4256+
// block outlives the call that set it, so it is restored when the frame closes.
4257+
const _framePrevProperty = new Uint8Array(_STREAM_MAX_DEPTH);
4258+
const _framePrevFunction = new Uint8Array(_STREAM_MAX_DEPTH);
42554259
// Printing a streamed block: where its first item landed in the output, how deep
42564260
// its opener sits in the writer's pending stack, and the direct declarations it
42574261
// has emitted so far — the last of a set of identical ones is the only one that
@@ -4495,9 +4499,10 @@ const _streamClose = (d, rule) => {
44954499
* @param {Rule} rule the open rule
44964500
* @param {Node | null} parent enclosing rule
44974501
* @param {number} index the rule's index among its siblings
4502+
* @param {number} d the block's frame depth
44984503
* @returns {boolean} whether `skipChildren()` declined its children
44994504
*/
4500-
const _streamEnterRule = (rule, parent, index) => {
4505+
const _streamEnterRule = (rule, parent, index, d) => {
45014506
const i0 = _nodeIndex(rule);
45024507
const ty = _types[i0];
45034508
const b = _visitors[ty];
@@ -4516,8 +4521,14 @@ const _streamEnterRule = (rule, parent, index) => {
45164521
const pe = ps + _listLens[i0];
45174522
const prevSupports = _inSupportsPrelude;
45184523
const prevMedia = _inMediaConditionPrelude;
4524+
// Body-scoped, unlike the two prelude flags: the block streams past this call,
4525+
// so `_streamConsumeBlock` puts them back once its `}` is reached.
4526+
_framePrevProperty[d] = _inPropertyRule ? 1 : 0;
4527+
_framePrevFunction[d] = _inFunctionRule ? 1 : 0;
45194528
if (ty === T_AT_RULE) {
45204529
const atName = _input.slice(_starts[i0] + 1, _aux0[i0]);
4530+
if (equalsLowerCase(atName, "property")) _inPropertyRule = true;
4531+
else if (equalsLowerCase(atName, "function")) _inFunctionRule = true;
45214532
if (equalsLowerCase(atName, "supports")) {
45224533
_inSupportsPrelude = true;
45234534
} else if (
@@ -4560,7 +4571,8 @@ const _streamActivate = (d) => {
45604571
const skipped = _streamEnterRule(
45614572
rule,
45624573
k === 0 ? null : _nodeRef(_frameRule[k - 1]),
4563-
parentIndex
4574+
parentIndex,
4575+
k
45644576
);
45654577
if (_streamWriter !== undefined) _streamOpen(k, rule);
45664578
const walk = !skipped && _recurseBlocks;
@@ -4706,6 +4718,8 @@ const _streamConsumeBlock = (ts, rule) => {
47064718
undefined
47074719
);
47084720
if (_streamWriter !== undefined) _streamClose(d, rule);
4721+
_inPropertyRule = _framePrevProperty[d] === 1;
4722+
_inFunctionRule = _framePrevFunction[d] === 1;
47094723
_streamWalked = rule;
47104724
};
47114725

@@ -11511,9 +11525,8 @@ let _placeShorthandAllowed = true;
1151111525

1151211526
// Set when the source names one at all, so a stylesheet without any pays a
1151311527
// single scan and keeps nothing.
11514-
// An at-keyword that could name `@namespace`: the spelling itself, or any
11515-
// at-keyword carrying an escape, which only decoding would tell apart. Over-wide
11516-
// on purpose — a false positive keeps an empty rule, which is the safe side.
11528+
// Could name `@namespace`: the spelling, or any escaped at-keyword only decoding
11529+
// tells apart. Over-wide on purpose — a false positive keeps an empty rule.
1151711530
const NAMESPACE_AT_RULE_RE = /@namespace|@[\w-]*\\/i;
1151811531

1151911532
// Whether a top-level `@namespace` could still be read here. Only a rule the

‎lib/html/syntax.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,11 +4804,8 @@ const FLAG_SELF_CLOSING = 4;
48044804
// A `<template>` element: `_nodeContentEnds` holds its content-fragment ref
48054805
// instead of a raw-text body-end offset (the two never coexist on one node).
48064806
const FLAG_HAS_TEMPLATE = 8;
4807-
// A `<template shadowrootmode>` the parser turned into its parent's shadow
4808-
// root, and the parent it hangs off. The engine takes such a template out of
4809-
// the tree entirely; webpack keeps it where it was written, so the two agree
4810-
// once it is written back — but it is no longer a child the tree construction
4811-
// may move away from its host.
4807+
// A `<template shadowrootmode>` kept where it was written, and its host: the
4808+
// engine takes it out of the tree, so nothing may move it away from that host.
48124809
const FLAG_SHADOW_ROOT = 16;
48134810
const FLAG_SHADOW_HOST = 32;
48144811

@@ -9228,8 +9225,8 @@ const _escapesStyleWrapper = (value) => {
92289225
const c = value.charCodeAt(i);
92299226
if (c === 0x5c) {
92309227
i++;
9231-
// The escape the value ran out of leaves nothing open.
9232-
if (i >= value.length) return depth !== 0;
9228+
// The escape the value ran out of takes the wrapper's own `}` next.
9229+
if (i >= value.length) return true;
92339230
} else if (c === 0x22 || c === 0x27) {
92349231
let closed = false;
92359232
for (i++; i < value.length; i++) {

‎test/CssSyntax.unittest.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,20 @@ describe("CssSyntax — block streaming", () => {
909909
expect(streamed[streamed.length - 1]).toBe("-AtRule|0|0");
910910
});
911911

912+
it("keeps a descriptor opaque in a streamed body", () => {
913+
// The block outlives the call that entered its rule, so `@property` /
914+
// `@function` state is restored per frame rather than around one walk.
915+
const value = "rgb(255,0,0)0.50px";
916+
expect(minify(`@function --f(){${BIG}result:${value}}`)).toContain(
917+
`result:${value}`
918+
);
919+
expect(
920+
minify(
921+
`@property --x{syntax:"*";inherits:false;${BIG}initial-value:${value}}`
922+
)
923+
).toContain(`initial-value:${value}`);
924+
});
925+
912926
it("visits a streamed block's children in source order", () => {
913927
// The collected walk emits every declaration and only then every child
914928
// rule; a streamed block emits each child as it finishes, so declarations

‎test/CssValueSyntax.unittest.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,5 +857,41 @@ describe("CssValueSyntax", () => {
857857
});
858858
expect(formatted).toBe(fs.readFileSync(DATA_TARGET, "utf8"));
859859
});
860+
861+
it.each([
862+
[
863+
"a supplemented initial the dataset now states itself",
864+
"text-align",
865+
(/** @type {EXPECTED_ANY} */ entry) => {
866+
entry.initial = "start";
867+
},
868+
/drop it from SUPPLEMENT\.initialValueKeywords/
869+
],
870+
[
871+
"a slot keyword the dataset no longer accepts",
872+
"flex-wrap",
873+
(/** @type {EXPECTED_ANY} */ entry) => {
874+
entry.syntax = "nowrap | [ wrap | wrap-reverse ]";
875+
},
876+
/unmergeableSlotKeywords: flex-wrap does not accept balance/
877+
]
878+
])("fails generation on %s", (_why, name, stale, message) => {
879+
// A `SUPPLEMENT` entry states what no dataset does; once one does, the
880+
// entry is stale and generation has to say so rather than emit it twice.
881+
const properties =
882+
/** @type {Record<string, { initial: string | string[], syntax: string }>} */ (
883+
/** @type {unknown} */ (require("mdn-data/css/properties.json"))
884+
);
885+
886+
const entry = properties[name];
887+
const before = { ...entry };
888+
stale(entry);
889+
try {
890+
expect(() => collectData()).toThrow(message);
891+
} finally {
892+
Object.assign(entry, before);
893+
}
894+
expect(() => collectData()).not.toThrow();
895+
});
860896
});
861897
});

‎test/HtmlSyntax.unittest.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6375,6 +6375,15 @@ describe("SourceProcessor — minify serialization edge cases", () => {
63756375
const minify = (source) =>
63766376
new SourceProcessor().process(source, { mode: "minify" }).code;
63776377

6378+
describe("style attributes the css minifier cannot read", () => {
6379+
it("keeps one the css parser runs out of stack on", () => {
6380+
// Balanced, so nothing is left open for the wrapper to lose — the parser
6381+
// still recurses per block, and the value has to come back as written.
6382+
const deep = `--x:${"(".repeat(20000)}${")".repeat(20000)}`;
6383+
expect(minify(`<p style="${deep}">x</p>`)).toContain(deep);
6384+
});
6385+
});
6386+
63786387
describe("cloned / reconstructed formatting elements", () => {
63796388
it("keeps the reconstructed <b> around text after an implied </p>", () => {
63806389
expect(minify("<p><b>a<p>b")).toBe("<p><b>a</b><p><b>b</b>");
@@ -6584,6 +6593,16 @@ describe("SourceProcessor — print modes", () => {
65846593
);
65856594
});
65866595

6596+
it("escapes text to the letter of \u00A713.3 outside minification", () => {
6597+
// A bare `>` is only ever a character, so minification keeps it; the CR is
6598+
// a reference either way, since a literal one would be read back as LF.
6599+
const source = "<p>a &amp; b &lt; c &gt; d &#13; e</p>";
6600+
expect(print(source, "beautify")).toBe(
6601+
"<p>a &amp; b &lt; c &gt; d &#13; e</p>"
6602+
);
6603+
expect(print(source, "minify")).toBe("<p>a & b &lt; c > d &#13; e");
6604+
});
6605+
65876606
it("beautifies to something that minifies back the same", () => {
65886607
for (const source of [
65896608
'<!DOCTYPE html><div id=a class="x"><!-- c --><p>hi</p></div>',

‎test/configCases/css/minimize-value-validity/style.css‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838
/*! a lone string in a later slot still unquotes */
3939
.o { font-family: serif, "My Font", monospace }
4040

41-
/* An `@property` descriptor is read against the `syntax` beside it, which
42-
webpack does not interpret — so its value is as opaque as a custom
43-
property's. `0` is not the `0px` a registered `<length>` was given. */
41+
/* An `@property` value is read against the `syntax` beside it, which webpack
42+
does not interpret: `0` is not the `0px` a registered `<length>` was given. */
4443
@property --len {
4544
syntax: "<length>";
4645
initial-value: 0px;
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!doctype html>
22
<title>declarative shadow root</title>
3-
<!-- The parser attaches this shadow root to the `div`, so the `</a>` below
4-
reconstructs its formatting element around what is left rather than
5-
around the template too. -->
3+
<!-- The shadow root attaches to the `div`, so the `</a>` reconstructs its
4+
formatting element around what is left rather than around the template. -->
65
<a href="#x"><div><template shadowrootmode=open><slot></slot></template></a>

‎test/configCases/html/declarative-shadow-root/webpack.config.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ module.exports = {
2121
},
2222
optimization: {
2323
minimize: true,
24-
// The default minimizer is replaced by the test harness, so wire the HTML
25-
// minify function into one minimizer plugin the same way the production
26-
// default does (lib/config/defaults.js): an array of minify functions, each
27-
// routed by its own `filter`, in a single instance / worker pool.
24+
// The harness replaces the default minimizer, so wire HTML minify up the way
25+
// `lib/config/defaults.js` does: filter-routed functions on one instance.
2826
minimizer: [
2927
{
3028
apply: (compiler) => {

‎test/configCases/html/minimize-transforms/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ exports[`ConfigCacheTestCases html minimize-transforms minimize-transforms shoul
1212
<div style=\\"top:anchor-size(--a1 width\\">declines: what the value leaves open would swallow the wrapper's own brace</div>
1313
<div style=box-shadow:var(--t,10px>declines: the same, unclosed in a fallback</div>
1414
<div style=\\"content:'a\\">declines: the same, unclosed in a string</div>
15+
<div style=width:3px>a closed pair leaves nothing open</div>
16+
<div style=\\"content:'\\\\'';color:red\\">an escape inside a string is not its end</div>
17+
<div style=\\"--escaped:\\\\7d \\">an escape outside one is not a brace either</div>
18+
<div style=--ran-out:\\\\>the escape the value ran out of</div>
1519
1620
<div style='content:\\"a{color:red}\\"'>wrapper-shaped string</div>
1721
<div style=--wrapper-shaped:a{color:red}>wrapper-shaped custom property</div>

‎test/configCases/html/minimize-transforms/__snapshots__/ConfigTest.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ exports[`ConfigTestCases html minimize-transforms minimize-transforms should com
1212
<div style=\\"top:anchor-size(--a1 width\\">declines: what the value leaves open would swallow the wrapper's own brace</div>
1313
<div style=box-shadow:var(--t,10px>declines: the same, unclosed in a fallback</div>
1414
<div style=\\"content:'a\\">declines: the same, unclosed in a string</div>
15+
<div style=width:3px>a closed pair leaves nothing open</div>
16+
<div style=\\"content:'\\\\'';color:red\\">an escape inside a string is not its end</div>
17+
<div style=\\"--escaped:\\\\7d \\">an escape outside one is not a brace either</div>
18+
<div style=--ran-out:\\\\>the escape the value ran out of</div>
1519
1620
<div style='content:\\"a{color:red}\\"'>wrapper-shaped string</div>
1721
<div style=--wrapper-shaped:a{color:red}>wrapper-shaped custom property</div>

0 commit comments

Comments
 (0)