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

Commit e865f63

Browse files
committed
fix(html): Allow for nested text nodes in iterables
This changes the API from `children` to `childNodes` because `children` does not include `Text` nodes. diff --git src/html.ts src/html.ts index d0578b1..6fe4502 100644 --- src/html.ts +++ src/html.ts @@ -19,7 +19,7 @@ function processSubTemplate(part: TemplatePart, value: unknown): boolean { function processDocumentFragment(part: TemplatePart, value: unknown): boolean { if (value instanceof DocumentFragment && part instanceof NodeTemplatePart) { - part.replace((value as unknown) as ChildNode) + if (value.childNodes.length) part.replace(...value.childNodes) return true } return false diff --git test/html.ts test/html.ts index 6dbfc6c..0ff3ee2 100644 --- test/html.ts +++ test/html.ts @@ -40,6 +40,17 @@ describe('render', () => { render(main(child('Goodbye')), surface) expect(surface.innerHTML).to.equal('<div><span>Goodbye</span></div>') }) + + it('can nest document fragments and text nodes', () => { + const main = frag => html`<span>${frag}</span>` + const fragment = document.createDocumentFragment() + fragment.append(new Text('Hello World')) + render(main(fragment), surface) + expect(surface.innerHTML).to.equal('<span>Hello World</span>') + fragment.append(document.createTextNode('Hello Universe!')) + render(main(fragment), surface) + expect(surface.innerHTML).to.equal('<span>Hello Universe!</span>') + }) }) describe('iterables', () => {
1 parent 00a735f commit e865f63

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ describe('render', () => {
6262
expect(surface.innerHTML).to.equal('<div>fourfivesix</div>')
6363
})
6464

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+
6593
it('supports other strings iterables in nodes', () => {
6694
const main = list => html`<div>${list}</div>`
6795
render(main(new Set(['one', 'two', 'three'])), surface)

0 commit comments

Comments
 (0)