Skip to content

Commit 7d94e23

Browse files
authored
(fix) more robust handling of inner script tags (#1433)
Remove scripts inside of `@html` tags #1427
1 parent b1b3810 commit 7d94e23

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ function processSvelteTemplate(
200200
handleStyleTag(node);
201201
break;
202202
case 'Element':
203-
scripts.handleScriptTag(node, parent);
203+
scripts.checkIfElementIsScriptTag(node, parent);
204+
break;
205+
case 'RawMustacheTag':
206+
scripts.checkIfContainsScriptTag(node);
204207
break;
205208
case 'BlockStatement':
206209
scopeStack.push();

packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ export class Scripts {
1313

1414
constructor(private htmlxAst: Node) {}
1515

16-
handleScriptTag = (node: Node, parent: Node) => {
16+
checkIfElementIsScriptTag(node: Node, parent: Node) {
1717
if (parent !== this.htmlxAst && node.name === 'script') {
1818
this.topLevelScripts = this.topLevelScripts.filter(
1919
(tag) => tag.start !== node.start || tag.end !== node.end
2020
);
2121
}
22-
};
22+
}
23+
24+
checkIfContainsScriptTag(node: Node) {
25+
this.topLevelScripts = this.topLevelScripts.filter(
26+
(tag) => !(node.start <= tag.start && node.end >= tag.end)
27+
);
28+
}
2329

2430
getTopLevelScriptTags(): { scriptTag: Node; moduleScriptTag: Node } {
2531
let scriptTag: Node = null;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference types="svelte" />
2+
<></>;function render() {
3+
4+
const schema = {
5+
key: "value"
6+
};
7+
;
8+
() => (<>
9+
10+
{ ``}</>);
11+
return { props: {}, slots: {}, getters: {}, events: {} }}
12+
13+
export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
///<reference types="svelte" />
2+
;function render() {
3+
4+
const schema = {
5+
key: "value"
6+
};
7+
;
8+
async () => {
9+
10+
``;};
11+
return { props: {}, slots: {}, getters: {}, events: {} }}
12+
13+
export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
const schema = {
3+
key: "value"
4+
};
5+
</script>
6+
7+
{@html `<script type="application/ld+json">${JSON.stringify(schema)}</script>`}

0 commit comments

Comments
 (0)