From c43795cf91e03ed76da30f500b717f35769d968c Mon Sep 17 00:00:00 2001 From: Steve Orvell Date: Mon, 27 Jan 2014 18:53:13 -0800 Subject: [PATCH] basic compatibility with SD polyfill. --- src/HTMLImports.js | 21 +++++++++++---------- src/Parser.js | 2 +- src/boot.js | 14 +++++++++++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/HTMLImports.js b/src/HTMLImports.js index 5daedbc..07405d7 100644 --- a/src/HTMLImports.js +++ b/src/HTMLImports.js @@ -72,7 +72,7 @@ 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 @@ -80,10 +80,9 @@ if (!useNative) { 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 @@ -91,6 +90,11 @@ if (!useNative) { } parser.parseNext(); }, + bootDocument: function(doc) { + this.loadSubtree(doc); + this.observe(doc); + parser.parseNext(); + }, loadedAll: function() { parser.parseNext(); } @@ -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 @@ -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' || diff --git a/src/Parser.js b/src/Parser.js index e204024..415d3c0 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -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)); diff --git a/src/boot.js b/src/boot.js index e5cd3ba..0eed555 100644 --- a/src/boot.js +++ b/src/boot.js @@ -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); + } } })();