Skip to content

Commit a765f47

Browse files
authored
fix: escape / exclude scripts with unknown types (#1026)
* fix: escape scripts with unknown types * fix: order
1 parent 5c9f30f commit a765f47

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

.changeset/tame-games-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/compiler": patch
3+
---
4+
5+
Escape script tags with unknown types

internal/printer/print-to-tsx.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ const (
244244
Text
245245
ScriptText
246246
JsonScriptText
247+
UnknownScriptText
247248
StyleText
248249
)
249250

@@ -254,9 +255,13 @@ func getTextType(n *astro.Node) TextType {
254255
return ScriptText
255256
}
256257

257-
if attr != nil && ScriptJSONMimeTypes[strings.ToLower(attr.Val)] {
258+
// There's no difference between JSON and unknown script types in the result JSX at this time
259+
// however, we might want to add some special handling in the future, so we keep them separate
260+
if ScriptJSONMimeTypes[strings.ToLower(attr.Val)] {
258261
return JsonScriptText
259262
}
263+
264+
return UnknownScriptText
260265
}
261266
if style := n.Closest(isStyle); style != nil {
262267
return StyleText
@@ -395,9 +400,9 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
395400
p.print("}}\n")
396401
}
397402
p.addSourceMapping(loc.Loc{Start: n.Loc[0].Start + len(n.Data)})
398-
} else if textType == StyleText || textType == JsonScriptText || textType == RawText {
403+
} else if textType == StyleText || textType == JsonScriptText || textType == RawText || textType == UnknownScriptText {
399404
p.addNilSourceMapping()
400-
if (textType == StyleText && o.IncludeStyles) || textType == JsonScriptText || textType == RawText {
405+
if (textType == StyleText && o.IncludeStyles) || ((textType == JsonScriptText || textType == UnknownScriptText) && o.IncludeScripts) || textType == RawText {
401406
p.print("{`")
402407
p.printTextWithSourcemap(escapeText(n.Data), n.Loc[0])
403408
p.addNilSourceMapping()

packages/compiler/test/tsx/script.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,33 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
3737
assert.snapshot(code, output, 'expected code to match snapshot');
3838
});
3939

40+
test('escape unknown types', async () => {
41+
const input = `<script type="text/somethigndf" is:inline>console.log("something");</script>`;
42+
const output = `${TSXPrefix}<Fragment>
43+
<script type="text/somethigndf" is:inline>{\`console.log("something");\`}</script>
44+
</Fragment>
45+
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
46+
const { code } = await convertToTSX(input, { sourcemap: 'external' });
47+
assert.snapshot(code, output, 'expected code to match snapshot');
48+
});
49+
50+
test("don't include scripts if disabled", async () => {
51+
const input = `
52+
<script>hello;</script>
53+
<script type="module">hello;</script>
54+
<script type="text/partytown">hello;</script>
55+
<script type="application/ld+json">hello;</script>
56+
<script type="text/somethigndf" is:inline>hello;</script>`;
57+
const output = `${TSXPrefix}<Fragment>
58+
<script></script>
59+
<script type="module"></script>
60+
<script type="text/partytown"></script>
61+
<script type="application/ld+json"></script>
62+
<script type="text/somethigndf" is:inline></script>
63+
</Fragment>
64+
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
65+
const { code } = await convertToTSX(input, { sourcemap: 'external', includeScripts: false });
66+
assert.snapshot(code, output, 'expected code to match snapshot');
67+
});
68+
4069
test.run();

0 commit comments

Comments
 (0)