Skip to content

Commit

Permalink
Fix knockout#1162 - renderTemplate on IE8
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Oct 24, 2013
1 parent c59104c commit ffcd256
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/templatingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,20 @@ describe('Templating', function() {
// Since the actual template markup was invalid, we don't really care what the
// resulting DOM looks like. We are only verifying there were no exceptions.
});

it('Should be possible to render a template to a document fragment', function() {
// Represents https://github.com/knockout/knockout/issues/1162
// This was failing on IE8
ko.setTemplateEngine(new dummyTemplateEngine({
myTemplate: "<p>myval: [js: myVal]</p>" // The whitespace after the closing span is what triggers the strange HTML parsing
}));

var testDocFrag = document.createDocumentFragment();
ko.renderTemplate("myTemplate", { myVal: 123 }, null, testDocFrag);

// Can't use .toContainHtml directly on doc frags, so check DOM structure manually
expect(testDocFrag.childNodes.length).toEqual(1);
expect(testDocFrag.childNodes[0].tagName).toEqual("P");
expect(testDocFrag.childNodes[0]).toContainHtml("myval: 123");
});
})
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ ko.utils = (function () {
domNodeIsContainedBy: function (node, containedByNode) {
if (node === containedByNode)
return true;
if (node.nodeType === 11)
return false; // Fixes issue #1162 - can't use node.contains for document fragments on IE8
if (containedByNode.contains)
return containedByNode.contains(node.nodeType === 3 ? node.parentNode : node);
if (containedByNode.compareDocumentPosition)
Expand Down

0 comments on commit ffcd256

Please sign in to comment.