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

Commit

Permalink
registering an element after an import has loaded will now upgrade th…
Browse files Browse the repository at this point in the history
…e element inside the import.
  • Loading branch information
sorvell committed Jan 18, 2014
1 parent 8280fef commit c22127c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ if (useNative) {
// if initial parsing is complete
if (scope.ready) {
// upgrade any pre-existing nodes of this type
scope.upgradeAll(document);
scope.upgradeDocumentTree(document);
}
return definition.ctor;
}
Expand Down
37 changes: 29 additions & 8 deletions src/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license that can be found in the LICENSE file.
(function(scope){

var logFlags = window.logFlags || {};
var IMPORT_LINK_TYPE = window.HTMLImports ? HTMLImports.IMPORT_LINK_TYPE : 'none';

// walk the subtree rooted at node, applying 'find(element, data)' function
// to each element
Expand Down Expand Up @@ -213,10 +214,16 @@ function _removed(element) {
}
}

// SD polyfill intrustion due mainly to the fact that 'document'
// is not entirely wrapped
function wrapIfNeeded(node) {
return window.ShadowDOMPolyfill ? ShadowDOMPolyfill.wrapIfNeeded(node)
: node;
}

function inDocument(element) {
var p = element;
var doc = window.ShadowDOMPolyfill &&
window.ShadowDOMPolyfill.wrapIfNeeded(document) || document;
var doc = wrapIfNeeded(document);
while (p) {
if (p == doc) {
return true;
Expand Down Expand Up @@ -300,19 +307,33 @@ function observe(inRoot) {
observer.observe(inRoot, {childList: true, subtree: true});
}

function observeDocument(document) {
observe(document);
function observeDocument(doc) {
observe(doc);
}

function upgradeDocument(document) {
logFlags.dom && console.group('upgradeDocument: ', (document.URL || document._URL || '').split('/').pop());
addedNode(document);
function upgradeDocument(doc) {
logFlags.dom && console.group('upgradeDocument: ', (doc.baseURI).split('/').pop());
addedNode(doc);
logFlags.dom && console.groupEnd();
}

// exports
function upgradeDocumentTree(doc) {
doc = wrapIfNeeded(doc);
upgradeDocument(doc);
//console.log('upgradeDocumentTree: ', (doc.baseURI).split('/').pop());
// upgrade contained imported documents
var imports = doc.querySelectorAll('link[rel=' + IMPORT_LINK_TYPE + ']');
for (var i=0, l=imports.length, n; (i<l) && (n=imports[i]); i++) {
if (n.import && n.import.__parsed) {
upgradeDocumentTree(n.import);
}
}
}

// exports
scope.IMPORT_LINK_TYPE = IMPORT_LINK_TYPE;
scope.watchShadow = watchShadow;
scope.upgradeDocumentTree = upgradeDocumentTree;
scope.upgradeAll = addedNode;
scope.upgradeSubtree = addedSubtree;

Expand Down
13 changes: 7 additions & 6 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* license that can be found in the LICENSE file.
*/

(function() {
(function(scope) {

// import

var IMPORT_LINK_TYPE = window.HTMLImports ? HTMLImports.IMPORT_LINK_TYPE : 'none';
var IMPORT_LINK_TYPE = scope.IMPORT_LINK_TYPE;

// highlander object for parsing a document tree

Expand Down Expand Up @@ -43,8 +43,8 @@ var parser = {
}
},
parseImport: function(linkElt) {
if (linkElt.content) {
parser.parse(linkElt.content);
if (linkElt.import) {
parser.parse(linkElt.import);
}
}
};
Expand All @@ -58,6 +58,7 @@ var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);

// exports

CustomElements.parser = parser;
scope.parser = parser;
scope.IMPORT_LINK_TYPE = IMPORT_LINK_TYPE;

})();
})(window.CustomElements);

0 comments on commit c22127c

Please sign in to comment.