Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/knockout/knockout
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Sep 8, 2013
2 parents da39a30 + 357da61 commit bc1b7a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions spec/bindingAttributeBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,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 <!-- ko test -->Some text<!-- /ko --> 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 = "<div data-bind='text: \"Some Text\"'></div>";

Expand Down
2 changes: 1 addition & 1 deletion src/binding/bindingProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand Down
10 changes: 6 additions & 4 deletions src/virtualElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// So, use node.text where available, and node.nodeValue elsewhere
var commentNodesHaveTextProperty = document && document.createComment("test").text === "<!--test-->";

var startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*ko(?:\s+(.+\s*\:[\s\S]*))?\s*-->$/ : /^\s*ko(?:\s+(.+\s*\:[\s\S]*))?\s*$/;
var startCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*ko(?:\s+([\s\S]+))?\s*-->$/ : /^\s*ko(?:\s+([\s\S]+))?\s*$/;
var endCommentRegex = commentNodesHaveTextProperty ? /^<!--\s*\/ko\s*-->$/ : /^\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) {
Expand Down Expand Up @@ -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;
},

Expand Down

0 comments on commit bc1b7a3

Please sign in to comment.