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

Commit

Permalink
implement node re-querying when a document acquires new scripts nodes…
Browse files Browse the repository at this point in the history
… in the middle of parsing; probably impairs performance, brittle wrt node injections above the current parser point
  • Loading branch information
Scott J. Miles committed Nov 9, 2013
1 parent 81a093a commit 485214e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@ var importParser = {
script: 'parseScript',
style: 'parseGeneric'
},
parse: function(inDocument) {
if (!inDocument.__importParsed) {
parse: function(document) {
if (!document.__importParsed) {
// only parse once
inDocument.__importParsed = true;
document.__importParsed = true;
// all parsable elements in inDocument (depth-first pre-order traversal)
var elts = inDocument.querySelectorAll(importParser.selectors);
var elts = document.querySelectorAll(importParser.selectors);
// memoize the number of scripts
var scriptCount = document.scripts.length;
// for each parsable node type, call the mapped parsing method
forEach(elts, function(e) {
for (var i=0, e; i<elts.length && (e=elts[i]); i++) {
importParser[importParser.map[e.localName]](e);
});
// if a script was injected, we need to requery our nodes
// TODO(sjmiles): injecting nodes above the current script will
// result in errors
if (scriptCount !== document.scripts.length) {
// memoize the new count
scriptCount = document.scripts.length;
// ensure we have any new nodes in our list
elts = document.querySelectorAll(importParser.selectors);
}
}
}
},
parseLink: function(linkElt) {
Expand Down

0 comments on commit 485214e

Please sign in to comment.