Skip to content

Commit e284968

Browse files
fix: correctly handle unclosed tags with same type tag inside (#2682)
1 parent af88417 commit e284968

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Element.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,19 @@ export class Element {
225225
...this.startEndTransformation
226226
]);
227227

228+
const closingTag = this.str.original.substring(
229+
this.str.original.lastIndexOf('</', this.node.end - 1) + 2,
230+
this.node.end - 1
231+
);
232+
228233
const tagEndIdx = this.str.original
229234
.substring(this.node.start, this.node.end)
230235
.lastIndexOf(`</${this.node.name}`);
231236
// tagEndIdx === -1 happens in situations of unclosed tags like `<p>fooo <p>anothertag</p>`
232-
const endStart = tagEndIdx === -1 ? this.node.end : tagEndIdx + this.node.start;
237+
const endStart =
238+
tagEndIdx === -1 || closingTag.trim() !== this.node.name
239+
? this.node.end
240+
: tagEndIdx + this.node.start;
233241
transform(this.str, endStart, this.node.end, this.endTransformation);
234242
}
235243
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"code": "unclosed-element",
3+
"start": {
4+
"line": 1,
5+
"column": 0,
6+
"character": 0
7+
},
8+
"end": {
9+
"line": 1,
10+
"column": 1,
11+
"character": 1
12+
},
13+
"pos": 0,
14+
"frame": "1: <div>\n ^\n2: <i></i>\n3: <div></div>",
15+
"name": "ParseError"
16+
}
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+
async () => { { svelteHTML.createElement("div", {});
4+
{ svelteHTML.createElement("p", {}); }
5+
6+
{ svelteHTML.createElement("div", {}); }}};
7+
return { props: /** @type {Record<string, never>} */ ({}), exports: {}, bindings: "", slots: {}, events: {} }}
8+
const Input__SvelteComponent_ = __sveltets_2_isomorphic_component(__sveltets_2_partial(__sveltets_2_with_any_event($$render())));
9+
/*Ωignore_startΩ*/type Input__SvelteComponent_ = InstanceType<typeof Input__SvelteComponent_>;
10+
/*Ωignore_endΩ*/export default Input__SvelteComponent_;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
<p></p>
3+
4+
<div></div>

0 commit comments

Comments
 (0)