Skip to content

Commit 314ad4d

Browse files
authored
fix: strip doctype using AST instead of regex (#2798)
#2791
1 parent 14dc584 commit 314ad4d

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ A `.svelte` file would look something like this:
2525
let count = $state(1);
2626
2727
let doubled = $derived(count * 2);
28-
let quadrupled = $derived(doubled * 2);
28+
let quadrupled = $derived(doubled * 2);
2929
3030
function handleClick() {
3131
count += 1;
3232
}
3333
</script>
3434

35-
<button onclick={handleClick}>Count: {count}</button>
35+
<button onclick="{handleClick}">Count: {count}</button>
3636

3737
<p>{count} * 2 = {doubled}</p>
3838
<p>{doubled} * 2 = {quadrupled}</p>

packages/svelte2tsx/src/htmlxtojsx_v2/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ export interface TemplateProcessResult {
6464
isRunes: boolean;
6565
}
6666

67-
function stripDoctype(str: MagicString): void {
68-
const regex = /<!doctype(.+?)>(\n)?/i;
69-
const result = regex.exec(str.original);
70-
if (result) {
71-
str.remove(result.index, result.index + result[0].length);
72-
}
73-
}
74-
7567
/**
7668
* Walks the HTMLx part of the Svelte component
7769
* and converts it to JSX
@@ -92,8 +84,6 @@ export function convertHtmlxToJsx(
9284
options.typingsNamespace = options.typingsNamespace || 'svelteHTML';
9385
const preserveAttributeCase = options.namespace === 'foreign';
9486

95-
stripDoctype(str);
96-
9787
const rootSnippets: Array<[number, number, Map<string, any>, string]> = [];
9888
let element: Element | InlineComponent | undefined;
9989

@@ -325,7 +315,9 @@ export function convertHtmlxToJsx(
325315
slotHandler.handleSlot(node, templateScope);
326316
}
327317

328-
if (node.name !== '!DOCTYPE') {
318+
if (node.name === '!DOCTYPE') {
319+
str.remove(node.start, node.end);
320+
} else {
329321
if (element) {
330322
element.child = new Element(
331323
str,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///<reference types="svelte" />
2+
;function $$render() {
3+
4+
const regex = /<!DOCTYPE html>/i;
5+
;
6+
async () => {};
7+
return { props: /** @type {Record<string, never>} */ ({}), slots: {}, events: {} }}
8+
9+
export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event($$render()))) {
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
const regex = /<!DOCTYPE html>/i;
3+
</script>

0 commit comments

Comments
 (0)