Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit 3c6bdc4

Browse files
authored
Merge pull request #8 from github/fix-html-allow-for-nested-text-nodes-in-iterables
Fix html allow for nested text nodes in iterables
2 parents f638aa5 + e865f63 commit 3c6bdc4

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/html.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function processSubTemplate(part: TemplatePart, value: unknown): boolean {
1919

2020
function processDocumentFragment(part: TemplatePart, value: unknown): boolean {
2121
if (value instanceof DocumentFragment && part instanceof NodeTemplatePart) {
22-
part.replace((value as unknown) as ChildNode)
22+
if (value.childNodes.length) part.replace(...value.childNodes)
2323
return true
2424
}
2525
return false
@@ -37,9 +37,9 @@ function processIterable(part: TemplatePart, value: unknown): boolean {
3737
if (item instanceof TemplateResult) {
3838
const fragment = document.createDocumentFragment()
3939
item.renderInto(fragment)
40-
nodes.push(...fragment.children)
40+
nodes.push(...fragment.childNodes)
4141
} else if (item instanceof DocumentFragment) {
42-
nodes.push(...item.children)
42+
nodes.push(...item.childNodes)
4343
} else {
4444
nodes.push(String(item))
4545
}

test/html.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ describe('render', () => {
4040
render(main(child('Goodbye')), surface)
4141
expect(surface.innerHTML).to.equal('<div><span>Goodbye</span></div>')
4242
})
43+
44+
it('can nest document fragments and text nodes', () => {
45+
const main = frag => html`<span>${frag}</span>`
46+
const fragment = document.createDocumentFragment()
47+
fragment.append(new Text('Hello World'))
48+
render(main(fragment), surface)
49+
expect(surface.innerHTML).to.equal('<span>Hello World</span>')
50+
fragment.append(document.createTextNode('Hello Universe!'))
51+
render(main(fragment), surface)
52+
expect(surface.innerHTML).to.equal('<span>Hello Universe!</span>')
53+
})
4354
})
4455

4556
describe('iterables', () => {
@@ -51,6 +62,34 @@ describe('render', () => {
5162
expect(surface.innerHTML).to.equal('<div>fourfivesix</div>')
5263
})
5364

65+
it('supports iterables of Sub Templates with text nodes', () => {
66+
const main = list => html`<div>${list}</div>`
67+
let fragments = ['one', 'two', 'three'].map(text => html`${text}`)
68+
render(main(fragments), surface)
69+
expect(surface.innerHTML).to.equal('<div>onetwothree</div>')
70+
fragments = ['four', 'five', 'six'].map(text => html`${text}`)
71+
render(main(fragments), surface)
72+
expect(surface.innerHTML).to.equal('<div>fourfivesix</div>')
73+
})
74+
75+
it('supports iterables of fragments with text nodes', () => {
76+
const main = list => html`<div>${list}</div>`
77+
let fragments = ['one', 'two', 'three'].map(text => {
78+
const fragment = document.createDocumentFragment()
79+
fragment.append(new Text(text))
80+
return fragment
81+
})
82+
render(main(fragments), surface)
83+
expect(surface.innerHTML).to.equal('<div>onetwothree</div>')
84+
fragments = ['four', 'five', 'six'].map(text => {
85+
const fragment = document.createDocumentFragment()
86+
fragment.append(new Text(text))
87+
return fragment
88+
})
89+
render(main(fragments), surface)
90+
expect(surface.innerHTML).to.equal('<div>fourfivesix</div>')
91+
})
92+
5493
it('supports other strings iterables in nodes', () => {
5594
const main = list => html`<div>${list}</div>`
5695
render(main(new Set(['one', 'two', 'three'])), surface)

0 commit comments

Comments
 (0)