Skip to content

Commit

Permalink
Merge pull request #73 from seanpdoyle/accept-node-template-parts
Browse files Browse the repository at this point in the history
Accept `Node` values as Template Parts
  • Loading branch information
keithamus authored Dec 9, 2024
2 parents 38c304a + 28f61c6 commit 0678eb7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function createProcessor(processPart: PartProcessor): TemplateTypeInit {
}

export function processPropertyIdentity(part: TemplatePart, value: unknown): void {
part.value = value instanceof Element ? value : String(value)
part.value = value instanceof Node ? value : String(value)
}

export function processBooleanAttribute(part: TemplatePart, value: unknown): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {TemplateInstance} from './template-instance.js'

export interface TemplatePart {
expression: string
value: Element | string | null
value: Node | string | null
}

type TemplateProcessCallback = (instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown) => void
Expand Down
13 changes: 13 additions & 0 deletions test/template-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ describe('template-instance', () => {

expect(root.innerHTML).to.equal('<template><div>Hello world</div></template>')
})
it('applies data to templated DocumentFragment nodes', () => {
const template = document.createElement('template')
const fragment = Object.assign(document.createElement('template'), {
innerHTML: '<div>Hello world</div>',
})
const originalHTML = `{{x}}`
template.innerHTML = originalHTML
const instance = new TemplateInstance(template, {x: fragment.content})
expect(template.innerHTML).to.equal(originalHTML)
const root = document.createElement('div')
root.appendChild(instance)
expect(root.innerHTML).to.equal(`<div>Hello world</div>`)
})
it('can render into partial text nodes', () => {
const template = document.createElement('template')
const originalHTML = `Hello {{x}}!`
Expand Down

0 comments on commit 0678eb7

Please sign in to comment.