Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render across <template> element boundaries #72

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading