Skip to content

Commit 9359b28

Browse files
committed
fix(html): read a script type through the parser's own decoding
`type="text/javascript"` is a JavaScript type — Chrome runs it — but matching the raw attribute text read it as a data block, so the body was trimmed rather than offered to a JS renderer, and an entity-spelled `application/ld+json` was never minified as JSON.
1 parent c202e00 commit 9359b28

3 files changed

Lines changed: 24 additions & 1 deletion

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+
Trim the body of a `<script>` holding a data block when collapsing whitespace.

‎lib/html/syntax.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10430,7 +10430,9 @@ const _rewriteAttributeValue = (element, name, raw, viewport, enumeratedOn) => {
1043010430
const _scriptType = (path, element) => {
1043110431
for (const attribute of path.attributes(element)) {
1043210432
if (attribute.name === "type") {
10433-
return _asciiLowerCase(_asciiTrim(attribute.value));
10433+
// Decoded first: the value the spec matches is the one the parser read,
10434+
// so `text&#47;javascript` is a JavaScript type and not a data block.
10435+
return _asciiLowerCase(_asciiTrim(decodeEntities(attribute.value, true)));
1043410436
}
1043510437
}
1043610438
return "";

‎test/HtmlSyntax.unittest.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,6 +4189,22 @@ describe("SourceProcessor — JSON <script> bodies", () => {
41894189
expect(collapseBody("text/x-template", " ")).toBe("");
41904190
});
41914191

4192+
it.each([
4193+
"text&#47;javascript",
4194+
"text&#x2F;javascript",
4195+
"&#116;ext/javascript"
4196+
])("reads %s as executable, since the parser decodes it", (type) => {
4197+
// Chrome runs each of these; classifying one as a data block would trim a
4198+
// body that is really a script.
4199+
expect(collapseBody(type, " var a = 1 ")).toBe(" var a = 1 ");
4200+
});
4201+
4202+
it("reads an entity-encoded JSON type as JSON", () => {
4203+
expect(collapseBody("application&#47;ld&#43;json", ' { "a" : 1 } ')).toBe(
4204+
'{"a":1}'
4205+
);
4206+
});
4207+
41924208
it("keeps a NBSP, which is a character and not whitespace", () => {
41934209
// `@swc/html` trims it, which edits what the body renders.
41944210
expect(collapseBody("text/x-template", "\u00A0\u00A0x\u00A0\u00A0")).toBe(

0 commit comments

Comments
 (0)