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

Commit

Permalink
Fixes #53 and matches native behavior for imports that have error'd: …
Browse files Browse the repository at this point in the history
…the `import` property is now null in this case.
  • Loading branch information
sorvell committed Aug 8, 2014
1 parent 06a0fd4 commit 289f06d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ if (!useNative) {
if (isDocumentLink(elt)) {
var doc = this.documents[url];
// if we've never seen a document at this url
if (!doc) {
if (doc === undefined) {
// 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.
this.bootDocument(doc);
doc = err ? null : makeDocument(resource, url);
if (doc) {
doc.__importLink = elt;
// note, we cannot use MO to detect parsed nodes because
// SD polyfill does not report these as mutations.
this.bootDocument(doc);
}
// cache document
this.documents[url] = doc;
}
Expand Down
22 changes: 13 additions & 9 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ var importParser = {
if (HTMLImports.__importsParsingHook) {
HTMLImports.__importsParsingHook(elt);
}
elt.import.__importParsed = true;
if (elt.import) {
elt.import.__importParsed = true;
}
this.markParsingComplete(elt);
// fire load event
if (elt.__resource && !elt.__error) {
Expand Down Expand Up @@ -218,13 +220,15 @@ var importParser = {
return !this.parsingElement && this.nextToParseInDoc(mainDoc);
},
nextToParseInDoc: function(doc, link) {
var nodes = doc.querySelectorAll(this.parseSelectorsForNode(doc));
for (var i=0, l=nodes.length, p=0, n; (i<l) && (n=nodes[i]); i++) {
if (!this.isParsed(n)) {
if (this.hasResource(n)) {
return nodeIsImport(n) ? this.nextToParseInDoc(n.import, n) : n;
} else {
return;
if (doc) {
var nodes = doc.querySelectorAll(this.parseSelectorsForNode(doc));
for (var i=0, l=nodes.length, p=0, n; (i<l) && (n=nodes[i]); i++) {
if (!this.isParsed(n)) {
if (this.hasResource(n)) {
return nodeIsImport(n) ? this.nextToParseInDoc(n.import, n) : n;
} else {
return;
}
}
}
}
Expand All @@ -240,7 +244,7 @@ var importParser = {
return node.__importParsed;
},
hasResource: function(node) {
if (nodeIsImport(node) && !node.import) {
if (nodeIsImport(node) && (node.import === undefined)) {
return false;
}
return true;
Expand Down
3 changes: 3 additions & 0 deletions test/html/HTMLImports.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

Object.keys(HTMLImports.importer.documents).forEach(function(key) {
var doc = HTMLImports.importer.documents[key];
if (!doc) {
return;
}
var links = doc.querySelectorAll('link[rel=import]');
Array.prototype.forEach.call(links, function(link) {
var href = link.getAttribute('href');
Expand Down
14 changes: 10 additions & 4 deletions test/html/load-404.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
<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">
<script>
var errors = 0;
function errorHandler() {
errors++;
}
</script>
<link rel="import" href="imports/404-1.html" onerror="errorHandler()">
<link rel="import" href="imports/404-2.html" onerror="errorHandler()">

</head>
<body>
Expand All @@ -29,6 +33,8 @@
function check() {
clearTimeout(timeout);
chai.assert.ok(loaded, '404\'d imports are loaded');
chai.assert.equal(document.querySelector('link').import, null, '404\'d link.import is null');
chai.assert.equal(errors, 2, '404\'d generate error event');
done();
}
</script>
Expand Down

0 comments on commit 289f06d

Please sign in to comment.