From dd8a5c99f186f81757288e14a3cd391c63691cf6 Mon Sep 17 00:00:00 2001 From: Michael Best Date: Tue, 16 Jul 2013 13:40:48 -1000 Subject: [PATCH 1/2] make virtualElements support binding without a colon like --- spec/bindingAttributeBehaviors.js | 12 ++++++++++++ src/virtualElements.js | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/spec/bindingAttributeBehaviors.js b/spec/bindingAttributeBehaviors.js index 867a2de10..af19f2510 100644 --- a/spec/bindingAttributeBehaviors.js +++ b/spec/bindingAttributeBehaviors.js @@ -356,6 +356,18 @@ describe('Binding attribute syntax', function() { expect(ko.contextFor(testNode.childNodes[1].childNodes[1]).$parentContext.customValue).toEqual('xyz'); }); + it('Should be able to use value-less binding in containerless binding', function() { + var initCalls = 0; + ko.bindingHandlers.test = { init: function () { initCalls++ } }; + ko.virtualElements.allowedBindings['test'] = true; + + testNode.innerHTML = "Hello Some text Goodbye"; + ko.applyBindings(null, testNode); + + expect(initCalls).toEqual(1); + expect(testNode).toContainText("Hello Some text Goodbye"); + }); + it('Should not allow multiple applyBindings calls for the same element', function() { testNode.innerHTML = "
"; diff --git a/src/virtualElements.js b/src/virtualElements.js index c8b9b2724..88e3a61f7 100644 --- a/src/virtualElements.js +++ b/src/virtualElements.js @@ -12,7 +12,7 @@ // So, use node.text where available, and node.nodeValue elsewhere var commentNodesHaveTextProperty = document && document.createComment("test").text === ""; - var startCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*ko(?:\s+(.+\s*\:[\s\S]*))?\s*$/; + var startCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*ko(?:\s+(.+\s*\:?[\s\S]+))?\s*$/; var endCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*\/ko\s*$/; var htmlTagsWithOptionallyClosingChildren = { 'ul': true, 'ol': true }; From 7d334c1f0d64a356bfcd96341e0d68316c525c26 Mon Sep 17 00:00:00 2001 From: Michael Best Date: Fri, 30 Aug 2013 11:38:47 -1000 Subject: [PATCH 2/2] Simplify virtual element regex and use "test" instead of "match" --- src/binding/bindingProvider.js | 2 +- src/virtualElements.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/binding/bindingProvider.js b/src/binding/bindingProvider.js index faa48499a..fe5f71714 100644 --- a/src/binding/bindingProvider.js +++ b/src/binding/bindingProvider.js @@ -9,7 +9,7 @@ 'nodeHasBindings': function(node) { switch (node.nodeType) { case 1: return node.getAttribute(defaultBindingAttributeName) != null; // Element - case 8: return ko.virtualElements.virtualNodeBindingValue(node) != null; // Comment node + case 8: return ko.virtualElements.hasBindingValue(node); // Comment node default: return false; } }, diff --git a/src/virtualElements.js b/src/virtualElements.js index 88e3a61f7..5be19c3b3 100644 --- a/src/virtualElements.js +++ b/src/virtualElements.js @@ -12,16 +12,16 @@ // So, use node.text where available, and node.nodeValue elsewhere var commentNodesHaveTextProperty = document && document.createComment("test").text === ""; - var startCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*ko(?:\s+(.+\s*\:?[\s\S]+))?\s*$/; + var startCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*ko(?:\s+([\s\S]+))?\s*$/; var endCommentRegex = commentNodesHaveTextProperty ? /^$/ : /^\s*\/ko\s*$/; var htmlTagsWithOptionallyClosingChildren = { 'ul': true, 'ol': true }; function isStartComment(node) { - return (node.nodeType == 8) && (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(startCommentRegex); + return (node.nodeType == 8) && startCommentRegex.test(commentNodesHaveTextProperty ? node.text : node.nodeValue); } function isEndComment(node) { - return (node.nodeType == 8) && (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(endCommentRegex); + return (node.nodeType == 8) && endCommentRegex.test(commentNodesHaveTextProperty ? node.text : node.nodeValue); } function getVirtualChildren(startComment, allowUnbalanced) { @@ -148,8 +148,10 @@ return node.nextSibling; }, + hasBindingValue: isStartComment, + virtualNodeBindingValue: function(node) { - var regexMatch = isStartComment(node); + var regexMatch = (commentNodesHaveTextProperty ? node.text : node.nodeValue).match(startCommentRegex); return regexMatch ? regexMatch[1] : null; },