Skip to content

Commit

Permalink
Merge pull request #72 from seanpdoyle/span-template-boundary
Browse files Browse the repository at this point in the history
Render across `<template>` element boundaries
  • Loading branch information
keithamus authored Nov 11, 2024
2 parents 80a59f2 + a27cd0b commit e4ef636
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/template-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function* collectParts(el: DocumentFragment): Generator<TemplatePart> {
const walker = el.ownerDocument.createTreeWalker(el, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, null)
let node
while ((node = walker.nextNode())) {
if (node instanceof Element && node.hasAttributes()) {
if (node instanceof HTMLTemplateElement) {
for (const part of collectParts(node.content)) {
yield part
}
} else if (node instanceof Element && node.hasAttributes()) {
for (let i = 0; i < node.attributes.length; i += 1) {
const attr = node.attributes.item(i)
if (attr && attr.value.includes('{{')) {
Expand Down
9 changes: 9 additions & 0 deletions test/template-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('template-instance', () => {
root.appendChild(instance)
expect(root.innerHTML).to.equal(`<div>Hello world</div>`)
})
it('applies data to nested templated element nodes', () => {
const root = document.createElement('div')
const template = Object.assign(document.createElement('template'), {
innerHTML: '<template><div>{{x}}</div></template>',
})
root.appendChild(new TemplateInstance(template, {x: 'Hello world'}))

expect(root.innerHTML).to.equal('<template><div>Hello world</div></template>')
})
it('can render into partial text nodes', () => {
const template = document.createElement('template')
const originalHTML = `Hello {{x}}!`
Expand Down

0 comments on commit e4ef636

Please sign in to comment.