Skip to content

Commit fb341cc

Browse files
authored
[fix] textContent should not be set for <template> element. (#7297)
* [fix] textContent should not be set for <template> element. * tidy - name convetion. minor refactor extract "is template" check to a variable and replace usages. * test template with text content * update html in test
1 parent df75dd7 commit fb341cc

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/compiler/compile/render_dom/wrappers/Element/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,12 @@ export default class ElementWrapper extends Wrapper {
420420
}
421421

422422
// insert static children with textContent or innerHTML
423+
// skip textcontent for <template>. append nodes to TemplateElement.content instead
423424
const can_use_textcontent = this.can_use_textcontent();
424-
if (!this.node.namespace && (this.can_use_innerhtml || can_use_textcontent) && this.fragment.nodes.length > 0) {
425+
const is_template = this.node.name === 'template';
426+
const is_template_with_text_content = is_template && can_use_textcontent;
427+
428+
if (!is_template_with_text_content && !this.node.namespace && (this.can_use_innerhtml || can_use_textcontent) && this.fragment.nodes.length > 0) {
425429
if (this.fragment.nodes.length === 1 && this.fragment.nodes[0].node.type === 'Text') {
426430
block.chunks.create.push(
427431
b`${node}.textContent = ${string_literal((this.fragment.nodes[0] as TextWrapper).data)};`
@@ -452,7 +456,7 @@ export default class ElementWrapper extends Wrapper {
452456
this.fragment.nodes.forEach((child: Wrapper) => {
453457
child.render(
454458
block,
455-
this.node.name === 'template' ? x`${node}.content` : node,
459+
is_template ? x`${node}.content` : node,
456460
nodes
457461
);
458462
});

test/runtime/samples/template/_config.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ export default {
22
// solo: 1,
33

44
html: `
5-
<template>
6-
<div>foo</div>
7-
</template>
5+
<template id="t1">
6+
<div>foo</div>
7+
</template>
8+
<template id="t2">123</template>
89
`,
910

1011
test({ assert, target }) {
11-
const template = target.querySelector('template');
12-
12+
13+
const template = target.querySelector('#t1');
1314
assert.htmlEqual(template.innerHTML, `
14-
<div>foo</div>
15-
`);
16-
15+
<div>foo</div>
16+
`);
1717
const content = template.content.cloneNode(true);
1818
const div = content.children[0];
1919
assert.htmlEqual(div.outerHTML, `
2020
<div>foo</div>
2121
`);
22+
23+
24+
const template2 = target.querySelector('#t2');
25+
assert.equal(template2.childNodes.length, 0);
26+
assert.equal(template2.content.childNodes.length, 1);
27+
assert.equal(template2.content.firstChild.textContent, '123');
28+
assert.htmlEqual(template2.innerHTML, '123');
29+
2230
}
2331
};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<template>
1+
<template id="t1">
22
<div>foo</div>
3-
</template>
3+
</template>
4+
<template id="t2">123</template>

0 commit comments

Comments
 (0)