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

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
<<<<<<< HEAD
"name": "tko",
=======
"name": "tko"
>>>>>>> 4507cbffd9fdbcdb8f673aab18bd0953a85c8033
"homepage": "https://tko.io/",
"license": "MIT",
"author": "The Knockout.js Team",
Expand Down
6 changes: 3 additions & 3 deletions packages/bind/src/applyBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function getBindingProvider () {
}

function isProviderForNode (provider, node) {
const nodeTypes = provider.FOR_NODE_TYPES || [1, 3, 8]
const nodeTypes = provider.FOR_NODE_TYPES || [1, 3, 8, 11]
return nodeTypes.includes(node.nodeType)
}

Expand Down Expand Up @@ -363,7 +363,7 @@ export function applyBindingsToNode (node, bindings, viewModelOrBindingContext)

export function applyBindingsToDescendants (viewModelOrBindingContext, rootNode) {
const asyncBindingsApplied = new Set()
if (rootNode.nodeType === 1 || rootNode.nodeType === 8) {
if (rootNode.nodeType === 1 || rootNode.nodeType === 8 || rootNode.nodeType === 11) {
const bindingContext = getBindingContext(viewModelOrBindingContext)
applyBindingsToDescendantsInternal(bindingContext, rootNode, asyncBindingsApplied)
return new BindingResult({asyncBindingsApplied, rootNode, bindingContext})
Expand All @@ -384,7 +384,7 @@ export function applyBindings (viewModelOrBindingContext, rootNode, extendContex
if (!rootNode) {
throw Error('ko.applyBindings: could not find window.document.body; has the document been loaded?')
}
} else if (rootNode.nodeType !== 1 && rootNode.nodeType !== 8) {
} else if (rootNode.nodeType !== 1 && rootNode.nodeType !== 8 && rootNode.nodeType !== 11) {
throw Error('ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node')
}
const rootContext = getBindingContext(viewModelOrBindingContext, extendContextCallback)
Expand Down
29 changes: 14 additions & 15 deletions packages/utils.parser/src/Identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
isWriteableObservable, isObservable
} from '@tko/observable'

import {
IDStart, IDContinue
} from './identifierExpressions'

export default class Identifier {
constructor (parser, token, dereferences) {
this.token = token
Expand Down Expand Up @@ -152,25 +148,28 @@ export default class Identifier {

/**
* Determine if a character is a valid item in an identifier.
* Note that we do not check whether the first item is a number, nor do we
* support unicode identifiers here.
* Won't catch reserved words iterating through one char at a time.
*
* From: http://stackoverflow.com/a/9337047
* @param {String} ch The character
* @param {Boolean} start Is the first character?
* @return {Boolean} True if this is a valid identifier
*/
// function is_identifier_char(ch) {
// return (ch >= 'A' && ch <= 'Z') ||
// (ch >= 'a' && ch <= 'z') ||
// (ch >= '0' && ch <= 9) ||
// ch === '_' || ch === '$';
// }
static is_valid_char(ch, start) {
let name = start ? ch
: '_'+ch
try {
Function('var ' + name)
} catch( e ) {
return false
}
return true
}
static is_valid_start_char (ch) {
return IDStart.test(ch)
return Identifier.is_valid_char(ch,true)
}

static is_valid_continue_char (ch) {
return IDContinue.test(ch)
return Identifier.is_valid_char(ch,false)
}

get [Node.isExpressionOrIdentifierSymbol] () { return true }
Expand Down
7 changes: 0 additions & 7 deletions packages/utils.parser/src/identifierExpressions.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/utils/src/dom/disposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var domDataKey = domData.nextKey()
// 1: Element
// 8: Comment
// 9: Document
var cleanableNodeTypes = { 1: true, 8: true, 9: true }
var cleanableNodeTypesWithDescendants = { 1: true, 9: true }
// 11: Document Fragment
var cleanableNodeTypes = { 1: true, 8: true, 9: true, 11: true }
var cleanableNodeTypesWithDescendants = { 1: true, 9: true , 11: true}

function getDisposeCallbacksCollection (node, createIfNotFound) {
var allDisposeCallbacks = domData.get(node, domDataKey)
Expand Down