Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
use staging document for binding elements
Browse files Browse the repository at this point in the history
This is how bind is going to work in a world where custom elements created callback dont fire inside of <template>.

R=arv
BUG=

Review URL: https://codereview.appspot.com/19990044
  • Loading branch information
rafaelw committed Nov 20, 2013
1 parent ebf0462 commit 07cfa2e
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/TemplateBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@
}

// http://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/templates/index.html#dfn-template-contents-owner
function getTemplateContentsOwner(doc) {
function getOrCreateTemplateContentsOwner(template) {
var doc = template.ownerDocument
if (!doc.defaultView)
return doc;
var d = doc.templateContentsOwner_;
Expand All @@ -284,6 +285,20 @@
return d;
}

function getTemplateStagingDocument(template) {
if (!template.stagingDocument_) {
var owner = template.content.ownerDocument;
if (!owner.stagingDocument_) {
owner.stagingDocument_ = owner.implementation.createHTMLDocument('');
console.log('created');
}

template.stagingDocument_ = owner.stagingDocument_;
}

return template.stagingDocument_;
}

// For non-template browsers, the parser will disallow <template> in certain
// locations, so we allow "attribute templates" which combine the template
// element with the top-level container node of the content, e.g.
Expand Down Expand Up @@ -357,7 +372,7 @@

if (!isNative) {
fixTemplateElementPrototype(templateElement);
var doc = getTemplateContentsOwner(templateElement.ownerDocument);
var doc = getOrCreateTemplateContentsOwner(templateElement);
templateElement.content_ = doc.createDocumentFragment();
}

Expand Down Expand Up @@ -530,7 +545,8 @@
content.bindingMap_ = map;
}

var instance = deepCloneIgnoreTemplateContent(content);
var stagingDocument = getTemplateStagingDocument(this);
var instance = deepCloneIgnoreTemplateContent(content, stagingDocument);

addMapBindings(instance, map, model, delegate, bound);
// TODO(rafaelw): We can do this more lazily, but setting a sentinel
Expand Down Expand Up @@ -790,14 +806,14 @@
addBindings(child, model, delegate);
}

function deepCloneIgnoreTemplateContent(node) {
var clone = node.cloneNode(false);
function deepCloneIgnoreTemplateContent(node, stagingDocument) {
var clone = stagingDocument.importNode(node, false);
if (node.isTemplate_) {
return clone;
}

for (var child = node.firstChild; child; child = child.nextSibling) {
clone.appendChild(deepCloneIgnoreTemplateContent(child))
clone.appendChild(deepCloneIgnoreTemplateContent(child, stagingDocument))
}

return clone;
Expand Down

0 comments on commit 07cfa2e

Please sign in to comment.