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

Commit 00a735f

Browse files
committed
fix(html): Allow for document fragments with text nodes.
This changes the API from `children` to `childNodes` because `children` does not include `Text` nodes.
1 parent c768956 commit 00a735f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/html.ts

Lines changed: 1 addition & 1 deletion
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

test/html.ts

Lines changed: 11 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', () => {

0 commit comments

Comments
 (0)