Skip to content

Commit 0678eb7

Browse files
authored
Merge pull request #73 from seanpdoyle/accept-node-template-parts
Accept `Node` values as Template Parts
2 parents 38c304a + 28f61c6 commit 0678eb7

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/processors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function createProcessor(processPart: PartProcessor): TemplateTypeInit {
1919
}
2020

2121
export function processPropertyIdentity(part: TemplatePart, value: unknown): void {
22-
part.value = value instanceof Element ? value : String(value)
22+
part.value = value instanceof Node ? value : String(value)
2323
}
2424

2525
export function processBooleanAttribute(part: TemplatePart, value: unknown): boolean {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {TemplateInstance} from './template-instance.js'
22

33
export interface TemplatePart {
44
expression: string
5-
value: Element | string | null
5+
value: Node | string | null
66
}
77

88
type TemplateProcessCallback = (instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown) => void

test/template-instance.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ describe('template-instance', () => {
3636

3737
expect(root.innerHTML).to.equal('<template><div>Hello world</div></template>')
3838
})
39+
it('applies data to templated DocumentFragment nodes', () => {
40+
const template = document.createElement('template')
41+
const fragment = Object.assign(document.createElement('template'), {
42+
innerHTML: '<div>Hello world</div>',
43+
})
44+
const originalHTML = `{{x}}`
45+
template.innerHTML = originalHTML
46+
const instance = new TemplateInstance(template, {x: fragment.content})
47+
expect(template.innerHTML).to.equal(originalHTML)
48+
const root = document.createElement('div')
49+
root.appendChild(instance)
50+
expect(root.innerHTML).to.equal(`<div>Hello world</div>`)
51+
})
3952
it('can render into partial text nodes', () => {
4053
const template = document.createElement('template')
4154
const originalHTML = `Hello {{x}}!`

0 commit comments

Comments
 (0)