Skip to content

Commit d2429dd

Browse files
authored
(fix) ignore script/styles inside comments (#460)
#143
1 parent 925a0b2 commit d2429dd

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

packages/svelte2tsx/src/htmlxparser.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ function parseAttributes(str: string, start: number) {
3333
}
3434

3535
function extractTag(htmlx: string, tag: 'script' | 'style') {
36-
const exp = new RegExp(`(<${tag}([\\S\\s]*?)>)([\\S\\s]*?)<\\/${tag}>`, 'g');
36+
const exp = new RegExp(`(<!--[^]*?-->)|(<${tag}([\\S\\s]*?)>)([\\S\\s]*?)<\\/${tag}>`, 'g');
3737
const matches: Node[] = [];
3838

3939
let match: RegExpExecArray | null = null;
4040
while ((match = exp.exec(htmlx)) != null) {
41-
const content = match[3];
41+
if (match[0].startsWith('<!--')) {
42+
// Tag is inside comment
43+
continue;
44+
}
4245

46+
const content = match[4];
4347
if (!content) {
4448
// Self-closing/empty tags don't need replacement
4549
continue;
4650
}
4751

48-
const start = match.index + match[1].length;
52+
const start = match.index + match[2].length;
4953
const end = start + content.length;
5054
const containerStart = match.index;
5155
const containerEnd = match.index + match[0].length;
@@ -55,7 +59,7 @@ function extractTag(htmlx: string, tag: 'script' | 'style') {
5559
end: containerEnd,
5660
name: tag,
5761
type: tag === 'style' ? 'Style' : 'Script',
58-
attributes: parseAttributes(match[2], containerStart + `<${tag}`.length),
62+
attributes: parseAttributes(match[3], containerStart + `<${tag}`.length),
5963
content: {
6064
type: 'Text',
6165
start,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///<reference types="svelte" />
2+
<></>;function render() {
3+
4+
// } // <- error here
5+
;
6+
() => (<>
7+
8+
</>);
9+
return { props: {}, slots: {}, getters: {}, events: {} }}
10+
11+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(render)) {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
// } // <- error here
3+
</script>
4+
5+
<!-- <script src="/lib/jodit.es2018.min.js">
6+
</script> -->

0 commit comments

Comments
 (0)