Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape / exclude scripts with unknown types #1026

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();