Skip to content

Commit 2a65a95

Browse files
fix(html): drop the inline-script trim, keep the decoded type
Trimming an embedded body's edges changes the text of a `<script>` node, so `syntax-equivalence` reports every wpt page holding one as a DOM the printer did not preserve. A JavaScript minifier removes those edges anyway, so the trim bought nothing it was worth filing 488 differences for. What is left is the bug: a `<script type>` spelled with a character reference is now read the way the parser reads it, so `text&#47;javascript` runs and `application&#47;ld&#43;json` is stripped, instead of both shipping verbatim as data blocks.
1 parent d00a9ae commit 2a65a95

22 files changed

Lines changed: 87 additions & 335 deletions

File tree

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+
Read a `<script type>` decoded, so a reference-spelled type is minified.

‎.changeset/015-minify-embedded-script-bodies.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ export interface OptimizationMinimizeCss {
25152515
vendorPrefixes?: boolean;
25162516
}
25172517
/**
2518-
* What the HTML minimizer does. Every transform that keeps the document's DOM is on by default and may be turned off on its own, so a page a rewrite breaks can be minimized without it while the rest still applies; the ones that change what a script or a selector reads back are off until asked for. A body the document embeds in a language this can name is minified with it: a JSON `<script>` loses the whitespace between its tokens — every literal copied byte for byte, so no number is rounded and no escape rewritten — and an inline `<script>` holding JavaScript or JSON loses the whitespace at its edges, which is the host's rather than part of what the element holds. A data block's body is left whole, its own whitespace being part of its syntax. A caller's own renderer is asked before any of this, so what it answers for is its to decide.
2518+
* What the HTML minimizer does. Every transform that keeps the document's DOM is on by default and may be turned off on its own, so a page a rewrite breaks can be minimized without it while the rest still applies; the ones that change what a script or a selector reads back are off until asked for.
25192519
* @since 5.110.0
25202520
*/
25212521
export interface OptimizationMinimizeHtml {

‎lib/html/syntax.js‎

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9598,41 +9598,21 @@ const _deferEmbedded = (source, type, build, as) => {
95989598
return deferredWrite(id);
95999599
};
96009600

9601-
/**
9602-
* The text of an embedded body: a document in a language of its own, so the
9603-
* host's whitespace around it is not part of it and goes with every other
9604-
* thing minifying drops. A body that is only whitespace keeps it — emptying
9605-
* the element hands it to `removeEmptyElements`, and the DOM would lose what
9606-
* the source wrote.
9607-
* @param {string} source the nested body
9608-
* @returns {string} the body to embed
9609-
*/
9610-
const _embeddedBody = (source) => {
9611-
if (!_minifying) return source;
9612-
const trimmed = _asciiTrim(source);
9613-
return trimmed === "" ? source : trimmed;
9614-
};
9615-
96169601
/**
96179602
* `_renderEmbeddedOrNull` for a body webpack has no minifier of its own for, so
9618-
* a renderer that declines leaves it as embedding found it.
9603+
* a renderer that declines leaves it exactly as written.
96199604
* @param {string} source the nested body
96209605
* @param {string} type its language, e.g. `"css"` / `"javascript"` / `"svg"`
96219606
* @returns {string} the rendered body, or the original
96229607
*/
96239608
const _renderEmbedded = (source, type) => {
9624-
const body = _embeddedBody(source);
9625-
// A body that is only whitespace is no source to render, and what a
9626-
// renderer made of it would empty the element.
9627-
if (_asciiTrim(body) === "") return body;
96289609
if (_deferEmbeddedSource !== undefined) {
9629-
return _deferEmbedded(body, type, (rendered) =>
9630-
typeof rendered === "string" ? rendered : body
9610+
return _deferEmbedded(source, type, (rendered) =>
9611+
typeof rendered === "string" ? rendered : source
96319612
);
96329613
}
9633-
if (_renderEmbeddedSource === undefined) return body;
9634-
const rendered = _renderEmbeddedOrNull(body, type);
9635-
return rendered === null ? body : rendered;
9614+
const rendered = _renderEmbeddedOrNull(source, type);
9615+
return rendered === null ? source : rendered;
96369616
};
96379617

96389618
/**
@@ -10473,7 +10453,6 @@ const _scriptType = (path, element) => {
1047310453
const raw = attribute.value;
1047410454
// The value the spec matches is the one the parser read, so a reference
1047510455
// is decoded first: `text&#47;javascript` runs, it is not a data block.
10476-
// Guarded on the reference, so the decode stays off the hot path.
1047710456
return _asciiLowerCase(
1047810457
_asciiTrim(raw.includes("&") ? decodeEntities(raw, true) : raw)
1047910458
);
@@ -10510,18 +10489,17 @@ const _JSON_SUBTYPE_REGEXP =
1051010489
* @returns {string} the stripped body
1051110490
*/
1051210491
const _minifyInlineJson = (json) => {
10513-
const body = _embeddedBody(json);
10514-
if (_asciiTrim(body) === "") return body;
10492+
if (json.trim() === "") return json;
1051510493
if (_deferEmbeddedSource !== undefined) {
10516-
return _deferEmbedded(body, JSON_TYPE, (rendered) =>
10517-
typeof rendered === "string" ? rendered : _builtinInlineJson(body)
10494+
return _deferEmbedded(json, JSON_TYPE, (rendered) =>
10495+
typeof rendered === "string" ? rendered : _builtinInlineJson(json)
1051810496
);
1051910497
}
1052010498
if (_renderEmbeddedSource !== undefined) {
10521-
const rendered = _renderEmbeddedOrNull(body, JSON_TYPE);
10499+
const rendered = _renderEmbeddedOrNull(json, JSON_TYPE);
1052210500
if (rendered !== null) return rendered;
1052310501
}
10524-
return _builtinInlineJson(body);
10502+
return _builtinInlineJson(json);
1052510503
};
1052610504

1052710505
/**
@@ -12123,12 +12101,16 @@ const printer = (path, writer) => {
1212312101
if (JSON_SCRIPT_TYPES.has(type) || _JSON_SUBTYPE_REGEXP.test(type)) {
1212412102
return _minifyInlineJson(data);
1212512103
}
12126-
// A data block — a template, a payload in a language nothing here
12127-
// names. Its whitespace can be its own syntax, so it is left alone.
12128-
if (!JAVASCRIPT_SCRIPT_TYPES.has(type)) return data;
12129-
// Program text, embedded like any other: webpack ships no JS
12130-
// minifier, so what comes back is a caller's renderer or the edges.
12131-
return _renderEmbedded(data, JAVASCRIPT_TYPE);
12104+
// No built-in: webpack ships no JS minifier, so an inline script is
12105+
// touched only by a caller's renderer.
12106+
if (
12107+
(_renderEmbeddedSource !== undefined ||
12108+
_deferEmbeddedSource !== undefined) &&
12109+
JAVASCRIPT_SCRIPT_TYPES.has(type) &&
12110+
data.trim() !== ""
12111+
) {
12112+
return _renderEmbedded(data, JAVASCRIPT_TYPE);
12113+
}
1213212114
}
1213312115
return data;
1213412116
}

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3853,7 +3853,7 @@
38533853
"added": "5.110.0"
38543854
},
38553855
"OptimizationMinimizeHtml": {
3856-
"description": "What the HTML minimizer does. Every transform that keeps the document's DOM is on by default and may be turned off on its own, so a page a rewrite breaks can be minimized without it while the rest still applies; the ones that change what a script or a selector reads back are off until asked for. A body the document embeds in a language this can name is minified with it: a JSON `<script>` loses the whitespace between its tokens — every literal copied byte for byte, so no number is rounded and no escape rewritten — and an inline `<script>` holding JavaScript or JSON loses the whitespace at its edges, which is the host's rather than part of what the element holds. A data block's body is left whole, its own whitespace being part of its syntax. A caller's own renderer is asked before any of this, so what it answers for is its to decide.",
3856+
"description": "What the HTML minimizer does. Every transform that keeps the document's DOM is on by default and may be turned off on its own, so a page a rewrite breaks can be minimized without it while the rest still applies; the ones that change what a script or a selector reads back are off until asked for.",
38573857
"type": "object",
38583858
"additionalProperties": false,
38593859
"properties": {

‎test/HtmlSyntax.unittest.js‎

Lines changed: 36 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,186 +4193,85 @@ describe("SourceProcessor — JSON <script> bodies", () => {
41934193
});
41944194
});
41954195

4196-
describe("SourceProcessor — inline <script> whitespace", () => {
4196+
describe("SourceProcessor — inline <script> type", () => {
41974197
const { SourceProcessor } = require("../lib/html/syntax");
41984198

41994199
/**
42004200
* @param {string} type the `<script type>` value
42014201
* @param {string} body the raw body
4202-
* @param {"conservative" | "smart" | "all"=} collapseWhitespace the tier, where one is asked for
42034202
* @returns {string} the body, minified
42044203
*/
4205-
const minifyBody = (type, body, collapseWhitespace) => {
4204+
const minifyBody = (type, body) => {
42064205
const out = new SourceProcessor().process(
42074206
`<script type="${type}">${body}</script>`,
4208-
{ mode: "minify", collapseWhitespace }
4207+
{ mode: "minify" }
42094208
).code;
42104209
return out.slice(out.indexOf(">") + 1, out.lastIndexOf("</script>"));
42114210
};
42124211

4213-
it.each(["", "module", "text/javascript", "application/javascript"])(
4214-
"trims the edges of the program text (%s)",
4212+
it.each([
4213+
"application&#47;ld&#43;json",
4214+
"application&#x2F;json",
4215+
"application/json&#32;"
4216+
])("reads %s as JSON, since the parser decodes it", (type) => {
4217+
expect(minifyBody(type, ' { "a" : 1 } ')).toBe('{"a":1}');
4218+
});
4219+
4220+
it.each(["text&#47;x-template", "text&#47;yaml", "unknown&#47;thing"])(
4221+
"leaves the body of the data block %s alone",
42154222
(type) => {
4216-
expect(minifyBody(type, " var a = 1 ")).toBe("var a = 1");
4223+
// Its own syntax is not ours to read, and its whitespace can be part of it.
4224+
expect(minifyBody(type, " <div> x </div> ")).toBe(" <div> x </div> ");
42174225
}
42184226
);
42194227

4220-
it.each(
4221-
/** @type {(undefined | "conservative" | "smart" | "all")[]} */ ([
4222-
undefined,
4223-
"conservative",
4224-
"smart",
4225-
"all"
4226-
])
4227-
)("trims whatever `collapseWhitespace` says (%s)", (tier) => {
4228-
// An embedded body is not the host's text, so its edges are not a run
4229-
// for that option to have an opinion about.
4230-
expect(minifyBody("", " var a = 1 ", tier)).toBe("var a = 1");
4231-
});
4232-
4233-
it("takes every character HTML counts as whitespace", () => {
4234-
expect(minifyBody("", "\t\n\f\r var a = 1 \r\f\n\t")).toBe("var a = 1");
4235-
});
4236-
4237-
it("keeps a body that is only whitespace", () => {
4238-
// Emptying the element hands it to `removeEmptyElements`, and a `<script>`
4239-
// the source wrote would go missing from the DOM.
4240-
expect(minifyBody("", " ")).toBe(" ");
4241-
});
4242-
4243-
it("keeps a NBSP, which is a character and not whitespace", () => {
4244-
// It sits next to a JavaScript identifier, it does not separate one.
4245-
expect(minifyBody("", "\u00A0var a = 1\u00A0")).toBe(
4246-
"\u00A0var a = 1\u00A0"
4247-
);
4248-
});
4249-
4250-
it.each([
4251-
"text/x-template",
4252-
"text/template",
4253-
"text/ng-template",
4254-
"text/html",
4255-
"text/plain",
4256-
"unknown/thing"
4257-
])("leaves the body of the data block %s alone", (type) => {
4258-
// Its own syntax is not ours to read, and its whitespace can be part of it.
4259-
expect(minifyBody(type, " <div> x </div> ")).toBe(" <div> x </div> ");
4260-
});
4261-
42624228
it("keeps the indentation a payload's own language may count", () => {
4263-
// Trimming the edges alone would leave the first line of this at column 0
4264-
// and every other one indented — which is no longer the same document.
42654229
const yaml = "\n a: 1\n b: 2\n ";
42664230
expect(minifyBody("text/yaml", yaml)).toBe(yaml);
42674231
});
42684232

4269-
it.each([
4270-
"text&#47;javascript",
4271-
"text&#x2F;javascript",
4272-
"&#116;ext/javascript"
4273-
])("reads %s as executable, since the parser decodes it", (type) => {
4274-
// Chrome runs each of these, so each is program text and not a data block.
4275-
expect(minifyBody(type, " var a = 1 ")).toBe("var a = 1");
4276-
});
4277-
4278-
it("keeps the trim where a renderer declines the body", () => {
4279-
// The edges go before the offer, so declining costs them nothing.
4280-
const out = new SourceProcessor().process(
4281-
"<script> var a = 1 </script>",
4282-
{
4283-
mode: /** @type {"minify"} */ ("minify"),
4284-
renderEmbeddedSource: () => undefined
4285-
}
4286-
).code;
4287-
expect(out).toBe("<script>var a = 1</script>");
4288-
});
4289-
4290-
it("trims a JSON body the strip declines", () => {
4291-
// Embedded all the same: the edges are outside whatever it holds.
4292-
expect(minifyBody("application/ld+json", " {{ t }} ")).toBe("{{ t }}");
4293-
});
4294-
4295-
it("strips a JSON body with no switch to ask", () => {
4296-
// Minifying is what decides an embedded body; a caller that wants its own
4297-
// answer gives a renderer, which is asked first.
4298-
const out = new SourceProcessor().process(
4299-
'<script type="application/json"> { "a" : 1 } </script>',
4300-
{ mode: /** @type {"minify"} */ ("minify") }
4301-
).code;
4302-
expect(out).toBe('<script type=application/json>{"a":1}</script>');
4233+
it("reads the type case- and whitespace-insensitively through a reference", () => {
4234+
expect(minifyBody(" APPLICATION&#47;JSON ", ' { "a" : 1 } ')).toBe(
4235+
'{"a":1}'
4236+
);
4237+
expect(minifyBody(" TEXT&#47;X-TEMPLATE ", " x ")).toBe(" x ");
43034238
});
43044239

43054240
/**
4306-
* Print with a renderer that answers asynchronously, as a caller reaching a
4307-
* worker pool does.
4241+
* Minify with a renderer, recording the bodies it was offered.
43084242
* @param {string} html input markup
4309-
* @param {(source: string) => string | undefined} answer what the renderer makes of one body
4310-
* @returns {Promise<{ code: string, offered: string[] }>} the output and each body offered
4243+
* @returns {{ code: string, offered: string[] }} the output and each body offered
43114244
*/
4312-
const deferredBody = async (html, answer) => {
4245+
const withRenderer = (html) => {
43134246
/** @type {string[]} */
43144247
const offered = [];
4315-
const { code } = await new SourceProcessor().processAsync(html, {
4248+
const { code } = new SourceProcessor().process(html, {
43164249
mode: /** @type {"minify"} */ ("minify"),
43174250
renderEmbeddedSource: (/** @type {string} */ source) => {
43184251
offered.push(source);
4319-
return Promise.resolve(answer(source));
4252+
return "rendered";
43204253
}
43214254
});
43224255
return { code, offered };
43234256
};
43244257

4325-
it("offers a deferred JSON body trimmed", async () => {
4326-
const { code, offered } = await deferredBody(
4327-
'<script type="application/ld+json"> { "a" : 1 } </script>',
4328-
() => '{"answered":1}'
4329-
);
4330-
4331-
expect(offered).toEqual(['{ "a" : 1 }']);
4332-
expect(code).toBe(
4333-
'<script type=application/ld+json>{"answered":1}</script>'
4334-
);
4335-
});
4336-
4337-
it("strips a deferred JSON body the renderer declines", async () => {
4338-
// The built-in stands in, and it reads the same trimmed body.
4339-
const { code } = await deferredBody(
4340-
'<script type="application/ld+json"> { "a" : 1 } </script>',
4341-
() => undefined
4258+
it("offers the body of a script whose type is spelled with a reference", () => {
4259+
// Chrome runs this one, so it is program text and not a data block.
4260+
const { code, offered } = withRenderer(
4261+
'<script type="text&#47;javascript">var a = 1</script>'
43424262
);
43434263

4344-
expect(code).toBe('<script type=application/ld+json>{"a":1}</script>');
4345-
});
4346-
4347-
it("keeps a JSON body that is only whitespace", () => {
4348-
expect(minifyBody("application/json", " ")).toBe(" ");
4264+
expect(offered).toEqual(["var a = 1"]);
4265+
expect(code).toBe("<script type=text/javascript>rendered</script>");
43494266
});
43504267

4351-
it("reads an entity-encoded JSON type as JSON", () => {
4352-
expect(minifyBody("application&#47;ld&#43;json", ' { "a" : 1 } ')).toBe(
4353-
'{"a":1}'
4268+
it("offers no data block whose type is spelled with a reference", () => {
4269+
const { code, offered } = withRenderer(
4270+
'<script type="text&#47;x-template"> x </script>'
43544271
);
4355-
});
4356-
4357-
it("reads the type case- and whitespace-insensitively", () => {
4358-
expect(minifyBody(" TEXT/JAVASCRIPT ", " var a = 1 ")).toBe("var a = 1");
4359-
expect(minifyBody(" TEXT/X-TEMPLATE ", " x ")).toBe(" x ");
4360-
});
43614272

4362-
it("leaves every body alone where nothing is minified", () => {
4363-
const out = new SourceProcessor().process(
4364-
"<script> var a = 1 </script>",
4365-
{ mode: "beautify" }
4366-
).code;
4367-
expect(out).toBe("<script> var a = 1 </script>");
4368-
});
4369-
4370-
it("trims an inline script in foreign content too", () => {
4371-
const out = new SourceProcessor().process(
4372-
"<svg><script> var a = 1 </script></svg>",
4373-
{ mode: "minify" }
4374-
).code;
4375-
expect(out).toBe("<svg><script>var a = 1</script></svg>");
4273+
expect(offered).toEqual([]);
4274+
expect(code).toBe("<script type=text/x-template> x </script>");
43764275
});
43774276

43784277
it("drops a redundant type spelled with a reference", () => {

‎test/__snapshots__/HtmlSyntax.unittest.js.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ exports[`SourceProcessor — minifying what the round-trip guard checks minifies
4141
exports[`SourceProcessor — minifying what the round-trip guard checks minifies an inline sheet under a nested list 1`] = `"<ul><li>a<ul><li><style>a{color:red}</style></ul></ul>"`;
4242
4343
exports[`htmlMinify — one document reaching every embedded site minifies with webpack's own minifiers when no renderer is given 1`] = `
44-
"<!doctype html><html lang=en><head><title> every embedded body </title><style>.a{color:red;margin:10px}.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.css{background:url(\\"data:text/css,.i { color : #ff0000 }\\")}.html{background:url(\\"data:text/html,<div> <!-- x --> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{ \\"a\\" : 1 }')}.js{background:url(\\"data:text/javascript,var a = 1 ;\\")}.b64{background:url(data:text/css;base64,LmlubmVyIHsgIGNvbG9yIDogI2ZmMDAwMCAgfQ==)}.png{background:url(data:image/png;base64,AAAA)}</style><script type=application/json>{\\"a\\":1,\\"b\\":[1,2]}</script><script>var a = 1 ; function f ( ) { return a }</script></head><body>
44+
"<!doctype html><html lang=en><head><title> every embedded body </title><style>.a{color:red;margin:10px}.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.css{background:url(\\"data:text/css,.i { color : #ff0000 }\\")}.html{background:url(\\"data:text/html,<div> <!-- x --> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{ \\"a\\" : 1 }')}.js{background:url(\\"data:text/javascript,var a = 1 ;\\")}.b64{background:url(data:text/css;base64,LmlubmVyIHsgIGNvbG9yIDogI2ZmMDAwMCAgfQ==)}.png{background:url(data:image/png;base64,AAAA)}</style><script type=application/json>{\\"a\\":1,\\"b\\":[1,2]}</script><script> var a = 1 ; function f ( ) { return a } </script></head><body>
4545
<p style=color:red;margin:10px>styled</p>
4646
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
4747
<iframe srcdoc=\\"<style>.s{color:red}</style><p> deep \\"></iframe>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html minimize-inline-script-entity-type minimize-inline-script-entity-type should compile 1`] = `
4+
"<!doctype html><html lang=en><head><title>Inline script type</title><script type=application/ld+json>{\\"a\\":1}</script><script type=application/ld+json>{\\"b\\":2}</script></head><body> <script>var a=1;</script> <script type=text/javascript>var b=2;</script> <script type=text/x-template> <p class=\\"a\\"> {{ t }} </p> </script> <script type=text/yaml>
5+
a: 1
6+
b: 2
7+
</script> <script type=unknown/thing> raw payload </script> </body></html>"
8+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html minimize-inline-script-entity-type minimize-inline-script-entity-type should compile 1`] = `
4+
"<!doctype html><html lang=en><head><title>Inline script type</title><script type=application/ld+json>{\\"a\\":1}</script><script type=application/ld+json>{\\"b\\":2}</script></head><body> <script>var a=1;</script> <script type=text/javascript>var b=2;</script> <script type=text/x-template> <p class=\\"a\\"> {{ t }} </p> </script> <script type=text/yaml>
5+
a: 1
6+
b: 2
7+
</script> <script type=unknown/thing> raw payload </script> </body></html>"
8+
`;

test/configCases/html/minimize-inline-script-whitespace/index.js renamed to test/configCases/html/minimize-inline-script-entity-type/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "./page.html";
22

3-
it("should trim an inline script but leave a data block alone", () => {
3+
it("should read a script type spelled with a character reference", () => {
44
// The emitted page is the assertion — see `test.config.js`.
55
expect(true).toBe(true);
66
});

0 commit comments

Comments
 (0)