tko icon indicating copy to clipboard operation
tko copied to clipboard

bind) Make ko.applyBindings work with nodeType 11 (Document Fragment)

Open avickers opened this issue 6 years ago • 5 comments
trafficstars

It should be possible to apply bindings to nodeType 11 (Document Fragment).

Among other cases, the shadowRoot of a Shadow DOM / Web Component is a document fragment.

Currently, it is necessary to create a wrapper div as a child of the shadowRoot and parent of the actual template. I can see no reason why binding to the shadowRoot directly should throw an error.

avickers avatar Apr 10 '19 22:04 avickers

The 3rd commit should probably be a separate pull request. I'll split it off if you'd like, Brian.

@mbest, the nodeType checks are inherited from Knockout 3. Can you think of a reason not to allow applying bindings to shadowRoot / Web Components? Would you like a pull request?

It would allow for a clean way of solving this: https://github.com/knockout/knockout/issues/2426 Helping KO to play better with 2015+ standards, as well as other libraries/frameworks that use custom elements/components.

avickers avatar Apr 14 '19 03:04 avickers

This makes sense. I'd expect tests, though.

mbest avatar Apr 15 '19 05:04 mbest

In principle this makes sense, and I'm happy to plug in the changes for recognizing type-11 nodes, subject to testing that verifies the behaviour.

What's the purpose of the changes to Identifier? I noted that they break CSP eval-unsafe, which is a hard requirement for some using TKO.

brianmhunt avatar Jul 11 '19 12:07 brianmhunt

The change to Identifier was only to shrink the package size. The Regex is something like 10K characters, but CSP explains why it needs to be that way. That commit is irrelevant to getting applyBindings to work with the shadowRoot.

avickers avatar Jul 11 '19 15:07 avickers

@avickers Got it, thanks. I wish there were something better than that Regex, but if there is I haven't seen it. 😞 Otherwise, this is just a verification test away from merging into the next alpha.

brianmhunt avatar Jul 11 '19 15:07 brianmhunt