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

Commit

Permalink
basic compatibility with SD polyfill.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Jan 28, 2014
1 parent a04e8ef commit c43795c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,29 @@ if (!useNative) {
// see https://code.google.com/p/chromium/issues/detail?id=249381.
elt.__resource = resource;
if (isDocumentLink(elt)) {
var doc = importer.documents[url];
var doc = this.documents[url];
// if we've never seen a document at this url
if (!doc) {
// generate an HTMLDocument from data
doc = makeDocument(resource, url);
doc.__importLink = elt;
// TODO(sorvell): we cannot use MO to detect parsed nodes because
// SD polyfill does not report these as mutations.
importer.loadSubtree(doc);
importer.observe(doc);
this.bootDocument(doc);
// cache document
importer.documents[url] = doc;
this.documents[url] = doc;
}
// don't store import record until we're actually loaded
// store document resource
elt.import = doc;
}
parser.parseNext();
},
bootDocument: function(doc) {
this.loadSubtree(doc);
this.observe(doc);
parser.parseNext();
},
loadedAll: function() {
parser.parseNext();
}
Expand Down Expand Up @@ -152,16 +156,13 @@ if (!useNative) {
var importer = {};
}

var wrappedDoc = window.ShadowDOMPolyfill ? wrap(document) : document;


// NOTE: We cannot polyfill document.currentScript because it's not possible
// both to override and maintain the ability to capture the native value;
// therefore we choose to expose _currentScript both when native imports
// and the polyfill are in use.
Object.defineProperty(wrappedDoc, '_currentScript', {
Object.defineProperty(mainDoc, '_currentScript', {
get: function() {
return HTMLImports.currentScript || wrappedDoc.currentScript;
return HTMLImports.currentScript || mainDoc.currentScript;
},
writeable: true,
configurable: true
Expand All @@ -171,7 +172,7 @@ Object.defineProperty(wrappedDoc, '_currentScript', {
// which may not be desireable; calls should resolve in the correct order,
// however.
function whenImportsReady(callback, doc) {
doc = doc || wrappedDoc;
doc = doc || mainDoc;
// if document is loading, wait and try again
var requiredState = HTMLImports.isIE ? 'complete' : 'interactive';
var isReady = (doc.readyState === 'complete' ||
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var importParser = {
this.markParsingComplete(scriptElt);
},
nextToParse: function() {
return !this.parsingElement && this.nextToParseInDoc(document);
return !this.parsingElement && this.nextToParseInDoc(mainDoc);
},
nextToParseInDoc: function(doc, link) {
var nodes = doc.querySelectorAll(this.parseSelectorsForNode(doc));
Expand Down
14 changes: 13 additions & 1 deletion src/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ HTMLImports.whenImportsReady(function() {
});

if (!HTMLImports.useNative) {
HTMLImports.observe(doc);
function bootstrap() {
HTMLImports.importer.bootDocument(doc);
}

// TODO(sorvell): SD polyfill does *not* generate mutations for nodes added
// by the parser. For this reason, we must wait until the dom exists to
// bootstrap.
if (document.readyState === 'complete' ||
(document.readyState === 'interactive' && !window.attachEvent)) {
bootstrap();
} else {
document.addEventListener('DOMContentLoaded', bootstrap);
}
}

})();

0 comments on commit c43795c

Please sign in to comment.