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

Implement InnerTemplatePart class #75

Merged
merged 1 commit into from
Dec 13, 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
11 changes: 11 additions & 0 deletions src/inner-template-part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NodeTemplatePart} from './node-template-part.js'

export class InnerTemplatePart extends NodeTemplatePart {
constructor(public template: HTMLTemplateElement) {
super(template, template.getAttribute('expression') ?? '')
}

get directive(): string {
return this.template.getAttribute('directive') ?? ''
}
}
12 changes: 6 additions & 6 deletions src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import type {TemplatePart, TemplateTypeInit} from './types.js'
import type {TemplateInstance} from './template-instance.js'
import {AttributeTemplatePart} from './attribute-template-part.js'

type PartProcessor = (part: TemplatePart, value: unknown) => void
type PartProcessor = (part: TemplatePart, value: unknown, state: unknown) => void

export function createProcessor(processPart: PartProcessor): TemplateTypeInit {
return {
processCallback(_: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void {
if (typeof params !== 'object' || !params) return
processCallback(_: TemplateInstance, parts: Iterable<TemplatePart>, state: unknown): void {
if (typeof state !== 'object' || !state) return
for (const part of parts) {
if (part.expression in params) {
const value = (params as Record<string, unknown>)[part.expression] ?? ''
processPart(part, value)
if (part.expression in state) {
const value = (state as Record<string, unknown>)[part.expression] ?? ''
processPart(part, value, state)
}
}
},
Expand Down
9 changes: 7 additions & 2 deletions src/template-instance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {parse} from './template-string-parser.js'
import {AttributeValueSetter, AttributeTemplatePart} from './attribute-template-part.js'
import {InnerTemplatePart} from './inner-template-part.js'
import {NodeTemplatePart} from './node-template-part.js'
import {propertyIdentity} from './processors.js'
import {TemplatePart, TemplateTypeInit} from './types.js'
Expand All @@ -9,8 +10,12 @@ function* collectParts(el: DocumentFragment): Generator<TemplatePart> {
let node
while ((node = walker.nextNode())) {
if (node instanceof HTMLTemplateElement) {
for (const part of collectParts(node.content)) {
yield part
if (node.hasAttribute('directive')) {
yield new InnerTemplatePart(node)
} else {
for (const part of collectParts(node.content)) {
yield part
}
Comment on lines +13 to +18
Copy link
Contributor Author

@seanpdoyle seanpdoyle Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keithamus Should this conditional be flattened to treat all HTMLTemplateElement instances as InnerTemplatePart instances?

That way, ww could migrate the current collectParts behavior into this package's built-in processors while still enabling consumers that provide their own custom processors with an integration seam to handle <template> elements however they see fit.

I'm not entirely sure what the change would look like in the processors themselves. Maybe it'd be best to follow this change up with that exploration separately?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree, let's follow up separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review! I've opened #76 to remove the special-casing and make InnerTemplatePart available to custom processors.

}
} else if (node instanceof Element && node.hasAttributes()) {
for (let i = 0; i < node.attributes.length; i += 1) {
Expand Down
20 changes: 20 additions & 0 deletions test/processors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from '@open-wc/testing'
import {TemplateInstance} from '../src/template-instance'
import {InnerTemplatePart} from '../src/inner-template-part'
import type {TemplateTypeInit} from '../src/types'
import {createProcessor} from '../src/processors'
describe('createProcessor', () => {
Expand Down Expand Up @@ -29,4 +30,23 @@ describe('createProcessor', () => {
instance.update({y: 'world'})
expect(calls).to.eql(0)
})

describe('handling InnerTemplatePart', () => {
beforeEach(() => {
processor = createProcessor(part => {
if (part instanceof InnerTemplatePart) calls += 1
})
})

it('detects InnerTemplatePart instances with <template> element', () => {
template.innerHTML = '<template directive="if" expression="x">{{x}}</template>'
new TemplateInstance(template, {x: true}, processor)
expect(calls).to.eql(1)
})

it('does not detect InnerTemplatePart instances without <template> element', () => {
new TemplateInstance(template, {x: true}, processor)
expect(calls).to.eql(0)
})
})
})
29 changes: 28 additions & 1 deletion test/template-instance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {expect} from '@open-wc/testing'
import {TemplateInstance} from '../src/template-instance'
import {NodeTemplatePart} from '../src/node-template-part'
import {propertyIdentityOrBooleanAttribute, createProcessor} from '../src/processors'
import {InnerTemplatePart} from '../src/inner-template-part'
import {processPropertyIdentity, propertyIdentityOrBooleanAttribute, createProcessor} from '../src/processors'

describe('template-instance', () => {
it('applies data to templated text nodes', () => {
Expand Down Expand Up @@ -354,5 +355,31 @@ describe('template-instance', () => {
expect(processCallCount).to.equal(2)
})
})

describe('handling InnerTemplatePart', () => {
it('makes outer state available to inner parts', () => {
const processor = createProcessor((part, value, state) => {
if (part instanceof InnerTemplatePart && part.directive === 'if') {
if (typeof state === 'object' && (state as Record<string, unknown>)[part.expression]) {
part.replace(new TemplateInstance(part.template, state, processor))
} else {
part.replace()
}
} else {
processPropertyIdentity(part, value)
}
})
const template = Object.assign(document.createElement('template'), {
innerHTML: '{{x}}<template directive="if" expression="y">{{y}}</template>',
})

const root = document.createElement('div')
root.appendChild(new TemplateInstance(template, {x: 'x', y: 'y'}, processor))
expect(root.innerHTML).to.equal('xy')

root.replaceChildren(new TemplateInstance(template, {x: 'x', y: false}, processor))
expect(root.innerHTML).to.equal('x')
})
})
})
})