Skip to content

Commit a25d564

Browse files
authored
Merge pull request #387 from knockout/claude/compassionate-tesla-O7UfD
fix: use mapped bindingName in AttributeMustacheProvider
2 parents 113ec6e + 686d495 commit a25d564

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tko/provider.mustache": patch
3+
---
4+
5+
Fix AttributeMustacheProvider to use the mapped binding name (e.g. `css`) instead of the raw attribute name (e.g. `class`) when looking up and emitting a direct binding. In handler sets that register `css` but not a `class` alias, `class="{{ expr }}"` previously fell through to `attr.class` instead of activating the `css` binding handler.

packages/provider.mustache/spec/attributeInterpolationSpec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ describe('Attribute Interpolation Markup Provider', function () {
167167
expect(testNode.getAttribute('class')).to.equal('test')
168168
})
169169

170+
it('Should map class attribute to css binding', function () {
171+
testNode.setAttribute('class', '{{expr}}')
172+
const bindings: any[] = Array.from(provider.bindingObjects(testNode, ctxStub({ expr: 'active' })))
173+
expect(bindings.length).to.equal(1)
174+
expect(bindings[0]).to.have.property('css')
175+
expect(bindings[0]).to.not.have.property('class')
176+
expect(bindings[0]).to.not.have.property('attr.class')
177+
})
178+
170179
it('Should convert value and checked attributes to two-way bindings', function () {
171180
const input = document.createElement('input')
172181
input.type = 'checkbox'

packages/provider.mustache/src/AttributeMustacheProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ export default class AttributeMustacheProvider extends Provider {
8080

8181
getPossibleDirectBinding(attrName: string | number) {
8282
const bindingName = this.ATTRIBUTES_BINDING_MAP[attrName]
83-
return bindingName && this.bindingHandlers.get(attrName)
83+
return bindingName && this.bindingHandlers.get(bindingName)
8484
}
8585

8686
*bindingObjects(node: Element, context: any) {
8787
for (const [attrName, parts] of this.bindingParts(node, context)) {
8888
const bindingForAttribute = this.getPossibleDirectBinding(attrName)
89-
const handler: string = bindingForAttribute ? attrName : `attr.${attrName}`
89+
const bindingName = this.ATTRIBUTES_BINDING_MAP[attrName]
90+
const handler: string = bindingForAttribute ? bindingName : `attr.${attrName}`
9091
const accessorFn = bindingForAttribute
9192
? (...v: any) => this.partsTogether(parts, context, node, ...v)
9293
: (...v: any) => ({ [attrName]: this.partsTogether(parts, context, node, ...v) })

0 commit comments

Comments
 (0)