Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit c60dabc

Browse files
committed
Merge pull request #278 from oneeman/detect-clone-feature
Only use `cloneNode` when appropriate
2 parents cdb7df3 + f9d97f0 commit c60dabc

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

packages/dom-helper/lib/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ var canRemoveSvgViewBoxAttribute = doc && (doc.createElementNS ? (function(docum
3737
return !element.getAttribute('viewBox');
3838
})(doc) : true);
3939

40+
var canClone = doc && (function(document){
41+
var element = document.createElement('div');
42+
element.appendChild( document.createTextNode(' '));
43+
element.appendChild( document.createTextNode(' '));
44+
var clonedElement = element.cloneNode(true);
45+
return clonedElement.childNodes[0].nodeValue === ' ';
46+
})(doc);
47+
4048
// This is not the namespace of the element, but of
4149
// the elements inside that elements.
4250
function interiorNamespace(element){
@@ -119,6 +127,7 @@ function DOMHelper(_document){
119127
if (!this.document) {
120128
throw new Error("A document object must be passed to the DOMHelper, or available on the global scope");
121129
}
130+
this.canClone = canClone;
122131
this.namespace = null;
123132
}
124133

packages/htmlbars-compiler/lib/template-compiler.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ TemplateCompiler.prototype.endProgram = function(program, programDepth) {
102102
this.getHydrationHooks(indent + ' ', this.hydrationCompiler.hooks) +
103103
indent+' dom.detectNamespace(contextualElement);\n' +
104104
indent+' var fragment;\n' +
105-
indent+' if (this.cachedFragment === null) {\n' +
106-
indent+' fragment = this.build(dom);\n' +
107-
indent+' if (this.hasRendered) {\n' +
108-
indent+' this.cachedFragment = fragment;\n' +
109-
indent+' } else {\n' +
110-
indent+' this.hasRendered = true;\n' +
105+
indent+' if (env.useFragmentCache && dom.canClone) {\n' +
106+
indent+' if (this.cachedFragment === null) {\n' +
107+
indent+' fragment = this.build(dom);\n' +
108+
indent+' if (this.hasRendered) {\n' +
109+
indent+' this.cachedFragment = fragment;\n' +
110+
indent+' } else {\n' +
111+
indent+' this.hasRendered = true;\n' +
112+
indent+' }\n' +
111113
indent+' }\n' +
112-
indent+' }\n' +
113-
indent+' if (this.cachedFragment) {\n' +
114-
indent+' fragment = dom.cloneNode(this.cachedFragment, true);\n' +
114+
indent+' if (this.cachedFragment) {\n' +
115+
indent+' fragment = dom.cloneNode(this.cachedFragment, true);\n' +
116+
indent+' }\n' +
117+
indent+' } else {\n' +
118+
indent+' fragment = this.build(dom);\n' +
115119
indent+' }\n' +
116120
hydrationProgram +
117121
indent+' return fragment;\n' +

packages/htmlbars-compiler/tests/html-compiler-test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ function commonSetup() {
9898
dom: new DOMHelper(),
9999
hooks: hooks,
100100
helpers: helpers,
101-
partials: partials
101+
partials: partials,
102+
useFragmentCache: true
102103
};
103104
}
104105

@@ -437,6 +438,15 @@ test("Simple data binding on fragments", function() {
437438
equalTokens(fragment, '<div><p>brown cow</p> to the world</div>');
438439
});
439440

441+
test("second render respects whitespace", function () {
442+
var template = compile('Hello {{ foo }} ');
443+
template.render({}, env, document.createElement('div'));
444+
var fragment = template.render({}, env, document.createElement('div'));
445+
equal(fragment.childNodes.length, 3, 'fragment contains 3 text nodes');
446+
equal(getTextContent(fragment.childNodes[0]), 'Hello ', 'first text node ends with one space character');
447+
equal(getTextContent(fragment.childNodes[2]), ' ', 'last text node contains one space character');
448+
});
449+
440450
test("morph receives escaping information", function() {
441451
expect(3);
442452

0 commit comments

Comments
 (0)