Skip to content
Open
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
10 changes: 10 additions & 0 deletions .changeset/fix-component-provider-parser-globals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@tko/provider.component": patch
---

Fix component params globals resolution in `ComponentProvider`.

`getComponentParams` now instantiates `Parser` with `new Parser()` and passes
provider globals to `parser.parse(...)`, matching canonical parser usage in
other providers. This restores resolution of globals in component `params`
expressions (for example, `params="answer: GLOBAL_CONST"`).
23 changes: 23 additions & 0 deletions packages/provider.component/spec/componentProviderBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ describe('Components: Provider', function () {
applyBindings({}, ne)
// No error raised.
})

it('resolves globals in component params', function () {
const provider = new MultiProvider({
providers: [new DataBindProvider(), new ComponentProvider()],
globals: { GLOBAL_CONST: 42 }
})
options.bindingProviderInstance = provider
bindingHandlers = provider.bindingHandlers
bindingHandlers.set(componentBindings)
bindingHandlers.set(coreBindings)

components.register('xenon', {
viewModel: function (params) {
expect(params.answer).to.equal(42)
},
template: '<span>ok</span>',
synchronous: true,
ignoreCustomElementWarning: true
})
const xe = document.createElement('xenon')
xe.setAttribute('params', 'answer: GLOBAL_CONST')
applyBindings({}, xe)
})
})

/* describe("nodeParamsToObject", function() {
Expand Down
4 changes: 2 additions & 2 deletions packages/provider.component/src/ComponentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export default class ComponentProvider extends Provider {
return { $raw: {} }
}

const parser = new (Parser as any)(node, context, this.globals) as Parser
const parser = new Parser()
const paramsString = (node.getAttribute('params') || '').trim()
const accessors = parser.parse(paramsString, context, undefined, node)
const accessors = parser.parse(paramsString, context, this.globals, node)
if (!accessors || Object.keys(accessors).length === 0) {
return { $raw: {} }
}
Expand Down
6 changes: 1 addition & 5 deletions packages/utils.parser/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ type InnerFilterType = (value: any, ignored: any, context: any, globals: any, no
type FilterType = InnerFilterType & { precedence: number }

/**
* Construct a new Parser instance with new Parser(node, context)
* @param {Node} node The DOM element from which we parsed the
* content.
* @param {object} context The Knockout context.
* @param {object} globals An object containing any desired globals.
* Parser for binding and params expressions.
*/
export default class Parser {
ch: any
Expand Down
Loading