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

Commit 72836e7

Browse files
committed
fix(html): only call update() after initial render
Calling update on the create step meant this was initializing some DocumentFragments twice, which causes them to empty out their contents, therefore elements were not getting rendered correctly. Aside from that, it's unnecessary work to call update with exactly the same contents!
1 parent 7a15332 commit 72836e7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/html.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class TemplateResult {
9595
} else {
9696
element.appendChild(instance)
9797
}
98+
return
9899
}
99100
renderedTemplateInstances.get(element)!.update((this.values as unknown) as Record<string, unknown>)
100101
}

test/html.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ describe('render', () => {
1616
surface = document.createElement('section')
1717
})
1818

19+
it('calls `createCallback` on first render', () => {
20+
const main = (x = null) => html`<div class="${x}"></div>`
21+
let called = false
22+
const instance = main()
23+
instance.processor = {
24+
processCallback() {
25+
throw new Error('Expected processCallback to not be called')
26+
},
27+
createCallback() {
28+
called = true
29+
}
30+
}
31+
instance.renderInto(surface)
32+
expect(called).to.equal(true)
33+
})
34+
1935
it('memoizes by TemplateResult#template, updating old templates with new values', () => {
2036
const main = (x = null) => html`<div class="${x}"></div>`
2137
render(main('foo'), surface)
@@ -51,6 +67,17 @@ describe('render', () => {
5167
render(main(fragment), surface)
5268
expect(surface.innerHTML).to.equal('<span>Hello Universe!</span>')
5369
})
70+
71+
it('renders DocumentFragments nested in sub templates nested in arrays', () => {
72+
const sub = () => {
73+
const frag = document.createDocumentFragment()
74+
frag.appendChild(document.createElement('div'))
75+
return html`<span>${frag}</span>`
76+
}
77+
const main = () => html`<div>${[sub(), sub()]}</div>`
78+
render(main(), surface)
79+
expect(surface.innerHTML).to.contain('<div><span><div></div></span><span><div></div></span></div>')
80+
})
5481
})
5582

5683
describe('iterables', () => {

0 commit comments

Comments
 (0)