Skip to content

Commit

Permalink
ensure innerHTML for tempalte is done on template element. depends on:s…
Browse files Browse the repository at this point in the history
  • Loading branch information
lidlanca authored Mar 12, 2022
1 parent f5eb70b commit 33fbe4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ export class HtmlTag {
// html tag nodes
n: ChildNode[];
// target
t: HTMLElement;
t: HTMLElement | DocumentFragment;
// anchor
a: HTMLElement;

Expand All @@ -665,7 +665,7 @@ export class HtmlTag {
m(html: string, target: HTMLElement, anchor: HTMLElement = null) {
if (!this.e) {
this.e = element(target.nodeName as keyof HTMLElementTagNameMap);
this.t = target;
this.t = target.tagName !== 'TEMPLATE' ? target : (target as HTMLTemplateElement).content;
this.c(html);
}

Expand All @@ -674,7 +674,7 @@ export class HtmlTag {

h(html: string) {
this.e.innerHTML = html;
this.n = Array.from(this.e.childNodes);
this.n = Array.from(this.e.nodeName == 'TEMPLATE' ? (this.e as HTMLTemplateElement).content.childNodes : this.e.childNodes);
}

i(anchor) {
Expand Down
12 changes: 12 additions & 0 deletions test/runtime/samples/template/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
<div>foo</div>
</template>
<template id="t2">123</template>
<template id="t3">1<b>B</b>1</template>
`,

test({ assert, target }) {
Expand All @@ -27,5 +28,16 @@ export default {
assert.equal(template2.content.firstChild.textContent, '123');
assert.htmlEqual(template2.innerHTML, '123');

const template3 = target.querySelector('#t3');
assert.equal(template3.childNodes.length, 0);
assert.equal(template3.content.childNodes.length, 3);
// test: (with hydration from ssr rendered html)
// out of order render.
// <template>1{@html '2'}3</template> may render as <template>321</template> for ssr+hydration case.
// we bypass it by using symmetric siblings. hence <template> is not fully stable for this edge case.
assert.equal(template3.content.childNodes[0].textContent, '1');
assert.equal(template3.content.childNodes[1].outerHTML, '<b>B</b>');
assert.equal(template3.content.childNodes[2].textContent, '1');

}
};
3 changes: 2 additions & 1 deletion test/runtime/samples/template/main.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template id="t1">
<div>foo</div>
</template>
<template id="t2">123</template>
<template id="t2">123</template>
<template id="t3">1{@html '<b>B</b>'}1</template>

0 comments on commit 33fbe4b

Please sign in to comment.