Skip to content

Commit 3df4cfa

Browse files
committed
fix(html): read a script type through the parser's own decoding
`type="text&webpack#47;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 ef41ee8 commit 3df4cfa

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎lib/html/syntax.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10269,7 +10269,9 @@ const _rewriteAttributeValue = (element, name, raw, viewport, enumeratedOn) => {
1026910269
const _scriptType = (path, element) => {
1027010270
for (const attribute of path.attributes(element)) {
1027110271
if (attribute.name === "type") {
10272-
return _asciiLowerCase(_asciiTrim(attribute.value));
10272+
// Decoded first: the value the spec matches is the one the parser read,
10273+
// so `text/javascript` is a JavaScript type and not a data block.
10274+
return _asciiLowerCase(_asciiTrim(decodeEntities(attribute.value, true)));
1027310275
}
1027410276
}
1027510277
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)