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

Commit 485214e

Browse files
author
Scott J. Miles
committed
implement node re-querying when a document acquires new scripts nodes in the middle of parsing; probably impairs performance, brittle wrt node injections above the current parser point
1 parent 81a093a commit 485214e

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/Parser.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,27 @@ var importParser = {
2323
script: 'parseScript',
2424
style: 'parseGeneric'
2525
},
26-
parse: function(inDocument) {
27-
if (!inDocument.__importParsed) {
26+
parse: function(document) {
27+
if (!document.__importParsed) {
2828
// only parse once
29-
inDocument.__importParsed = true;
29+
document.__importParsed = true;
3030
// all parsable elements in inDocument (depth-first pre-order traversal)
31-
var elts = inDocument.querySelectorAll(importParser.selectors);
31+
var elts = document.querySelectorAll(importParser.selectors);
32+
// memoize the number of scripts
33+
var scriptCount = document.scripts.length;
3234
// for each parsable node type, call the mapped parsing method
33-
forEach(elts, function(e) {
35+
for (var i=0, e; i<elts.length && (e=elts[i]); i++) {
3436
importParser[importParser.map[e.localName]](e);
35-
});
37+
// if a script was injected, we need to requery our nodes
38+
// TODO(sjmiles): injecting nodes above the current script will
39+
// result in errors
40+
if (scriptCount !== document.scripts.length) {
41+
// memoize the new count
42+
scriptCount = document.scripts.length;
43+
// ensure we have any new nodes in our list
44+
elts = document.querySelectorAll(importParser.selectors);
45+
}
46+
}
3647
}
3748
},
3849
parseLink: function(linkElt) {

0 commit comments

Comments
 (0)