Skip to content

Commit 1018345

Browse files
committed
fix: ignore style tag inside script tag
#2104
1 parent 5a8cfa5 commit 1018345

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

packages/svelte2tsx/src/utils/htmlxparser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,18 @@ function extractTag(htmlx: string, tag: 'script' | 'style') {
7373
}
7474

7575
function findVerbatimElements(htmlx: string) {
76-
return [...extractTag(htmlx, 'script'), ...extractTag(htmlx, 'style')];
76+
const styleTags = extractTag(htmlx, 'style');
77+
const tags = extractTag(htmlx, 'script');
78+
for (const styleTag of styleTags) {
79+
// Could happen if someone has a `<style>...</style>` string in their script tag
80+
const insideScript = tags.some(
81+
(tag) => tag.start < styleTag.start && tag.end > styleTag.end
82+
);
83+
if (!insideScript) {
84+
tags.push(styleTag);
85+
}
86+
}
87+
return tags;
7788
}
7889

7990
function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
///<reference types="svelte" />
2+
;function render() {
3+
4+
const config = { branding: { primaryColor: '#012345' } },
5+
branding = config?.branding;
6+
7+
const cssString = `<style>:root {--primary-color: ${
8+
branding?.primaryColor ?? '#ABCDEF'
9+
};}</style>`;
10+
;
11+
async () => {};
12+
return { props: /** @type {Record<string, never>} */ ({}), slots: {}, events: {} }}
13+
14+
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
const config = { branding: { primaryColor: '#012345' } },
3+
branding = config?.branding;
4+
5+
const cssString = `<style>:root {--primary-color: ${
6+
branding?.primaryColor ?? '#ABCDEF'
7+
};}</style>`;
8+
</script>

0 commit comments

Comments
 (0)