Created
January 20, 2016 20:12
-
-
Save blasten/fe1c0c2d77a16c6d3b16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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