Skip to content

Commit

Permalink
fix: escape / exclude scripts with unknown types (#1026)
Browse files Browse the repository at this point in the history
* fix: escape scripts with unknown types

* fix: order
  • Loading branch information
Princesseuh authored Jul 17, 2024
1 parent 5c9f30f commit a765f47
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-games-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Escape script tags with unknown types
11 changes: 8 additions & 3 deletions internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const (
Text
ScriptText
JsonScriptText
UnknownScriptText
StyleText
)

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

if attr != nil && ScriptJSONMimeTypes[strings.ToLower(attr.Val)] {
// There's no difference between JSON and unknown script types in the result JSX at this time
// however, we might want to add some special handling in the future, so we keep them separate
if ScriptJSONMimeTypes[strings.ToLower(attr.Val)] {
return JsonScriptText
}

return UnknownScriptText
}
if style := n.Closest(isStyle); style != nil {
return StyleText
Expand Down Expand Up @@ -395,9 +400,9 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
p.print("}}\n")
}
p.addSourceMapping(loc.Loc{Start: n.Loc[0].Start + len(n.Data)})
} else if textType == StyleText || textType == JsonScriptText || textType == RawText {
} else if textType == StyleText || textType == JsonScriptText || textType == RawText || textType == UnknownScriptText {
p.addNilSourceMapping()
if (textType == StyleText && o.IncludeStyles) || textType == JsonScriptText || textType == RawText {
if (textType == StyleText && o.IncludeStyles) || ((textType == JsonScriptText || textType == UnknownScriptText) && o.IncludeScripts) || textType == RawText {
p.print("{`")
p.printTextWithSourcemap(escapeText(n.Data), n.Loc[0])
p.addNilSourceMapping()
Expand Down
29 changes: 29 additions & 0 deletions packages/compiler/test/tsx/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,33 @@ export default function __AstroComponent_(_props: Record<string, any>): any {}\n
assert.snapshot(code, output, 'expected code to match snapshot');
});

test('escape unknown types', async () => {
const input = `<script type="text/somethigndf" is:inline>console.log("something");</script>`;
const output = `${TSXPrefix}<Fragment>
<script type="text/somethigndf" is:inline>{\`console.log("something");\`}</script>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external' });
assert.snapshot(code, output, 'expected code to match snapshot');
});

test("don't include scripts if disabled", async () => {
const input = `
<script>hello;</script>
<script type="module">hello;</script>
<script type="text/partytown">hello;</script>
<script type="application/ld+json">hello;</script>
<script type="text/somethigndf" is:inline>hello;</script>`;
const output = `${TSXPrefix}<Fragment>
<script></script>
<script type="module"></script>
<script type="text/partytown"></script>
<script type="application/ld+json"></script>
<script type="text/somethigndf" is:inline></script>
</Fragment>
export default function __AstroComponent_(_props: Record<string, any>): any {}\n`;
const { code } = await convertToTSX(input, { sourcemap: 'external', includeScripts: false });
assert.snapshot(code, output, 'expected code to match snapshot');
});

test.run();

0 comments on commit a765f47

Please sign in to comment.