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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed May 17, 2014
1 parent 1c796ec commit 1ee7357
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ function watchImportsLoad(callback, doc) {
var loaded = 0, l = imports.length;
function checkDone(d) {
if (loaded == l) {
// go async to ensure parser isn't stuck on a script tag
requestAnimationFrame(callback);
callback && callback();
}
}
function loadedImport(e) {
Expand All @@ -240,10 +239,50 @@ function watchImportsLoad(callback, doc) {
}

function isImportLoaded(link) {
return useNative ? (link.import && (link.import.readyState !== 'loading')) :
return useNative ? (link.import && (link.import.readyState !== 'loading')) || link.__loaded :
link.__importParsed;
}

// TODO(sorvell): install a mutation observer to see if HTMLImports have loaded
// this is a workaround for https://www.w3.org/Bugs/Public/show_bug.cgi?id=25007
// and should be removed when this bug is addressed.
if (useNative) {
new MutationObserver(function(mxns) {
for (var i=0, l=mxns.length, m; (i < l) && (m=mxns[i]); i++) {
if (m.addedNodes) {
handleImports(m.addedNodes);
}
}
}).observe(document.head, {childList: true});

function handleImports(nodes) {
for (var i=0, l=nodes.length, n; (i<l) && (n=nodes[i]); i++) {
if (isImport(n)) {
handleImport(n);
}
}
}

function isImport(element) {
return element.localName === 'link' && element.rel === 'import';
}

function handleImport(element) {
var loaded = element.import;
if (loaded) {
markTargetLoaded({target: element});
} else {
element.addEventListener('load', markTargetLoaded);
element.addEventListener('error', markTargetLoaded);
}
}

function markTargetLoaded(event) {
event.target.__loaded = true;
}

}

// exports
scope.hasNative = hasNative;
scope.useNative = useNative;
Expand Down
36 changes: 36 additions & 0 deletions test/html/load-404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html>
<head>
<title>load ready 404 test</title>
<script>
</script>
<script src="../../../tools/test/htmltest.js"></script>
<script src="../../../tools/test/chai/chai.js"></script>
<script src="../../html-imports.js"></script>
<link rel="import" href="imports/404-1.html">
<link rel="import" href="imports/404-2.html">
<link rel="import" href="imports/404-3.html">
<link rel="import" href="imports/404-4.html">

</head>
<body>
<script>
var loaded = false;
document.addEventListener('HTMLImportsLoaded', function() {
loaded = true;
check();
});

// wait some time and then fail if no load event is fired
var timeout = setTimeout(function() {
check();
}, 2000);

function check() {
clearTimeout(timeout);
chai.assert.ok(loaded, '404\'d imports are loaded');
done();
}
</script>
</body>
</html>
1 change: 1 addition & 0 deletions test/js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ htmlSuite('HTMLImports', function() {
htmlTest('html/style-links.html');
htmlTest('html/style-paths.html');
htmlTest('html/load.html');
htmlTest('html/load-404.html');
htmlTest('html/currentScript.html');
htmlTest('html/dedupe.html');
htmlTest('html/dynamic.html');
Expand Down

0 comments on commit 1ee7357

Please sign in to comment.