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

Commit

Permalink
minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Sep 29, 2014
1 parent 78d3cb7 commit 40d6b02
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
12 changes: 5 additions & 7 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
*/
(function(scope) {

// imports
var useNative = scope.useNative;
var flags = scope.flags;
var IMPORT_LINK_TYPE = 'import';

// TODO(sorvell): SD polyfill intrusion
var mainDoc = window.ShadowDOMPolyfill ?
ShadowDOMPolyfill.wrapIfNeeded(document) : document;
var IMPORT_LINK_TYPE = scope.IMPORT_LINK_TYPE;

if (!useNative) {

// imports
var rootDocument = scope.rootDocument;
var xhr = scope.xhr;
var Loader = scope.Loader;
var parser = scope.parser;
Expand Down Expand Up @@ -60,7 +58,7 @@ if (!useNative) {
// find the proper set of load selectors for a given node
loadSelectorsForNode: function(node) {
var doc = node.ownerDocument || node;
return doc === mainDoc ? this.documentPreloadSelectors :
return doc === rootDocument ? this.documentPreloadSelectors :
this.importsPreloadSelectors;
},

Expand Down Expand Up @@ -170,7 +168,7 @@ if (!useNative) {
};

Object.defineProperty(document, 'baseURI', baseURIDescriptor);
Object.defineProperty(mainDoc, 'baseURI', baseURIDescriptor);
Object.defineProperty(rootDocument, 'baseURI', baseURIDescriptor);
}

// IE shim for CustomEvent
Expand Down
5 changes: 4 additions & 1 deletion src/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
*/
(function(scope){

// imports
var IMPORT_LINK_TYPE = scope.IMPORT_LINK_TYPE;
var importSelector = 'link[rel=' + IMPORT_LINK_TYPE + ']';
var importer = scope.importer;
var parser = scope.parser;

var importSelector = 'link[rel=' + IMPORT_LINK_TYPE + ']';


// we track mutations for addedNodes, looking for imports
function handler(mutations) {
for (var i=0, l=mutations.length, m; (i<l) && (m=mutations[i]); i++) {
Expand Down
12 changes: 6 additions & 6 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
*/
(function(scope) {

var IMPORT_LINK_TYPE = 'import';
// imports
var rootDocument = scope.rootDocument;
var flags = scope.flags;
var isIE = scope.isIE;
// TODO(sorvell): SD polyfill intrusion
var mainDoc = window.ShadowDOMPolyfill ?
window.ShadowDOMPolyfill.wrapIfNeeded(document) : document;
var IMPORT_LINK_TYPE = scope.IMPORT_LINK_TYPE;

// importParser
// highlander object to manage parsing of imports
Expand Down Expand Up @@ -237,7 +236,7 @@ var importParser = {
// determine the next element in the tree which should be parsed
nextToParse: function() {
this._mayParse = [];
return !this.parsingElement && (this.nextToParseInDoc(mainDoc) ||
return !this.parsingElement && (this.nextToParseInDoc(rootDocument) ||
this.nextToParseDynamic());
},

Expand Down Expand Up @@ -268,7 +267,8 @@ var importParser = {
// return the set of parse selectors relevant for this node.
parseSelectorsForNode: function(node) {
var doc = node.ownerDocument || node;
return doc === mainDoc ? this.documentSelectors : this.importsSelectors;
return doc === rootDocument ? this.documentSelectors :
this.importsSelectors;
},

isParsed: function(node) {
Expand Down
27 changes: 13 additions & 14 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

(function(scope) {

var hasNative = ('import' in document.createElement('link'));
var IMPORT_LINK_TYPE = 'import';
var hasNative = (IMPORT_LINK_TYPE in document.createElement('link'));
var useNative = hasNative;

var isIE = /Trident/.test(navigator.userAgent);

// TODO(sorvell): SD polyfill intrusion
Expand All @@ -20,7 +20,7 @@ var wrap = function(node) {
return hasShadowDOMPolyfill ? ShadowDOMPolyfill.wrapIfNeeded(node) : node;
};

var mainDoc = wrap(document);
var rootDocument = wrap(document);

// NOTE: We cannot polyfill document.currentScript because it's not possible
// both to override and maintain the ability to capture the native value;
Expand All @@ -40,14 +40,14 @@ var currentScriptDescriptor = {
};

Object.defineProperty(document, '_currentScript', currentScriptDescriptor);
Object.defineProperty(mainDoc, '_currentScript', currentScriptDescriptor);
Object.defineProperty(rootDocument, '_currentScript', currentScriptDescriptor);

// call a callback when all HTMLImports in the document at call (or at least
// document ready) time have loaded.
// 1. ensure the document is in a ready state (has dom), then
// 2. watch for loading of imports and call callback when done
function whenImportsReady(callback, doc) {
doc = doc || mainDoc;
function whenReady(callback, doc) {
doc = doc || rootDocument;
// if document is loading, wait and try again
whenDocumentReady(function() {
watchImportsLoad(callback, doc);
Expand Down Expand Up @@ -87,8 +87,8 @@ function watchImportsLoad(callback, doc) {
var imports = doc.querySelectorAll('link[rel=import]');
var loaded = 0, l = imports.length;
function checkDone(d) {
if (loaded == l) {
callback && callback();
if ((loaded == l) && callback) {
callback();
}
}
function loadedImport(e) {
Expand Down Expand Up @@ -181,21 +181,20 @@ if (useNative) {
// have loaded. This event is required to simulate the script blocking
// behavior of native imports. A main document script that needs to be sure
// imports have loaded should wait for this event.
whenImportsReady(function() {
whenReady(function() {
HTMLImports.ready = true;
HTMLImports.readyTime = new Date().getTime();
mainDoc.dispatchEvent(
rootDocument.dispatchEvent(
new CustomEvent('HTMLImportsLoaded', {bubbles: true})
);
});

// exports
scope.useNative = useNative;
scope.isImportLoaded = isImportLoaded;
scope.whenReady = whenImportsReady;
scope.whenReady = whenReady;
scope.rootDocument = rootDocument;
scope.IMPORT_LINK_TYPE = IMPORT_LINK_TYPE;
scope.isIE = isIE;

// deprecated
scope.whenImportsReady = whenImportsReady;

})(window.HTMLImports);

0 comments on commit 40d6b02

Please sign in to comment.