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

Commit

Permalink
add MutationObserver polyfill to loader
Browse files Browse the repository at this point in the history
Add msMatchesSelector to matches impl check

Fix propertyDescriptor for _currentScript

Polyfill document.baseURI for IE

Remove writable, IE doesn't like it

Only need to define baseURI on real document, don't need to keep the prop descriptor around

Add heuristic to fake load event for IE

Spec says that "load" must always be fired on style element, but IE fails to do so if its resources are either fully
text, or the @imports have already been processed asynchornously.

http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-style-element

add MutationObserver and WeakMap to karma serve list

Don't try to access CSSRules length if it doesn't exist

If we add event listeners first, don't need to call done explicitly
  • Loading branch information
dfreedm committed Jan 31, 2014
1 parent d2a0fa6 commit dec1f87
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
"../MutationObservers/build.json",
"src/scope.js",
"src/Loader.js",
"src/Parser.js",
Expand Down
4 changes: 3 additions & 1 deletion conf/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = function(karma) {
'HTMLImports/html-imports.js',
{pattern: 'tools/**/*.js', included: false},
{pattern: 'HTMLImports/src/*', included: false},
{pattern: 'HTMLImports/test/**/*', included: false}
{pattern: 'HTMLImports/test/**/*', included: false},
{pattern: 'MutationObservers/*.js', included: false},
{pattern: 'WeakMap/*.js', included: false}
],
}));
};
1 change: 1 addition & 0 deletions html-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var thisFile = 'html-imports.js';
var scopeName = 'HTMLImports';
var modules = [
'../MutationObservers/mutation-observers.js',
'src/scope.js',
'src/Loader.js',
'src/Parser.js',
Expand Down
14 changes: 12 additions & 2 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,23 @@ var currentScriptDescriptor = {
get: function() {
return HTMLImports.currentScript || document.currentScript;
},
writeable: true,
configurable: true
}
};

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

// Polyfill document.baseURI for browsers without it.
// ShadowDOM Polyfill wrapper should handle this automatically.
if (!document.baseURI) {
Object.defineProperty(document, 'baseURI', {
get: function() {
return window.location.href;
},
configurable: true
});
}

// TODO(sorvell): multiple calls will install multiple event listeners
// which may not be desireable; calls should resolve in the correct order,
// however.
Expand Down
5 changes: 3 additions & 2 deletions src/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var importSelector = 'link[rel=' + IMPORT_LINK_TYPE + ']';
var matches = HTMLElement.prototype.matches ||
HTMLElement.prototype.matchesSelector ||
HTMLElement.prototype.webkitMatchesSelector ||
HTMLElement.prototype.mozMatchesSelector;
HTMLElement.prototype.mozMatchesSelector ||
HTMLElement.prototype.msMatchesSelector;

var importer = scope.importer;

Expand All @@ -22,7 +23,7 @@ function handler(mutations) {
addedNodes(m.addedNodes);
}
}
};
}

function addedNodes(nodes) {
for (var i=0, l=nodes.length, n; (i<l) && (n=nodes[i]); i++) {
Expand Down
28 changes: 27 additions & 1 deletion src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ var importParser = {
};
elt.addEventListener('load', done);
elt.addEventListener('error', done);

// NOTE: IE does not fire "load" event for styles that have already loaded
// This is in violation of the spec, so we try our hardest to work around it
if (isIe && elt.localName === 'style') {
var fakeLoad = false;
// If there's not @import in the textContent, assume it has loaded
if (elt.textContent.indexOf('@import') == -1) {
fakeLoad = true;
// if we have a sheet, we have been parsed
} else if (elt.sheet) {
fakeLoad = true;
var csr = elt.sheet.cssRules;
var len = csr ? csr.length : 0;
// search the rules for @import's
for (var i = 0, r; (i < len) && (r = csr[i]); i++) {
if (r.type === CSSRule.IMPORT_RULE) {
// if every @import has resolved, fake the load
fakeLoad = fakeLoad && Boolean(r.styleSheet);
}
}
}
// dispatch a fake load event and continue parsing
if (fakeLoad) {
elt.dispatchEvent(new CustomEvent('load', {bubbles: false}));
}
}
},
parseScript: function(scriptElt) {
// acquire code to execute
Expand Down Expand Up @@ -216,4 +242,4 @@ scope.parser = importParser;
scope.path = path;
scope.isIE = isIe;

})(HTMLImports);
})(HTMLImports);

0 comments on commit dec1f87

Please sign in to comment.