Skip to content

Instantly share code, notes, and snippets.

@blasten
Created January 20, 2016 20:12
Show Gist options
  • Save blasten/fe1c0c2d77a16c6d3b16 to your computer and use it in GitHub Desktop.
Save blasten/fe1c0c2d77a16c6d3b16 to your computer and use it in GitHub Desktop.
function checkSelector(node, selectors) {
if (selectors.length === 0) {
return false;
}
var currentSel = selectors.shift();
if (currentSel === '>') {
return checkSelector(node, selectors);
}
if (currentSel.charAt(0) === '.' && node.classList.contains(currentSel.substr(1))) {
return true;
}
return false;
}
function querySelectorAll(node, selector) {
var selectors = selectors.replace(/\s+/g, ' ').split(' ');
var nodeMatches = [];
RecursiveDFSTraversal(node, function(node, fnArgs) {
var selectors = fnArgs.selectors.slice();
var selectorMatched = checkSelector(node, selectors);
if (selectorMatched) {
if (selectors.length === 0) {
nodeMatches.push(node);
// don't expand this subtree
return false;
} else {
return {selectors: selectors};
}
}
// don't expand this subtree
return false;
},
{
selectors: selectors
});
return nodeMatches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment