Skip to content

Commit

Permalink
Accept Node values as Template Parts
Browse files Browse the repository at this point in the history
Related to [#62][] (see [3.2. Template Parts and Custom Template Process Callback][])
Follow-up to [#65][]

While the changes made in [#65][] improved support for `Element` parts,
the expansion from `string` to `Element | string` was not broadened
enough. While all `Element` instances are DOM Nodes, not all DOM Nodes
are `Element` instances.

For example, this change enables support for [DocumentFragment][]
instances (generated from classes like [Range][] or properties like
[HTMLTemplateElement.content][]), where prior support resulted in
templating the `[object DocumentFragment]` text string instead of the
fragment's `Node` instances.

[#62]: #62
[3.2. Template Parts and Custom Template Process Callback]: https://github.com/WICG/webcomponents/blob/159b1600bab02fe9cd794825440a98537d53b389/proposals/Template-Instantiation.md#32-template-parts-and-custom-template-process-callback
[#65]: #65
[DocumentFragment]: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
[HTMLTemplateElement.content]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement/content
  • Loading branch information
seanpdoyle committed Dec 9, 2024
1 parent 38c304a commit 28f61c6
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 28f61c6

Please sign in to comment.