Created
April 20, 2009 20:07
-
-
Save subtleGradient/98719 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var Matcher = (function(M){ | |
| function Matcher(context, selectorObj){ | |
| M.matcher = []; | |
| // GET | |
| M.matcher.push( | |
| M.getByAll (context, selectorObj) || | |
| M.getByClass (context, selectorObj) || | |
| M.getById (context, selectorObj) || | |
| M.getByTag (context, selectorObj) | |
| ); | |
| // PREPARE TO LOOP | |
| M.prepFilterByAll (context, selectorObj); | |
| M.prepFilterById (context, selectorObj); | |
| M.prepFilterByClass (context, selectorObj); | |
| M.prepFilterByTag (context, selectorObj); | |
| M.prepFilterByParents(context, selectorObj); | |
| // FILTER | |
| M.matcher.push(M.eachStart); | |
| var filters = [ | |
| M.filterByAll (context, selectorObj), | |
| M.filterById (context, selectorObj), | |
| M.filterByClass (context, selectorObj), | |
| M.filterByTag (context, selectorObj), | |
| M.filterByParents(context, selectorObj), | |
| ]; | |
| for (var i=0; i < filters.length; i++) { | |
| if (!filters[i]) continue; | |
| M.matcher.push(M.filterBy(filters[i])); | |
| } | |
| M.matcher.push(M.eachEnd); | |
| try{ | |
| eval(M.matcher.join('')); | |
| }catch(e){ | |
| console.error(e); | |
| }; | |
| console.log(selectorObj); | |
| console.log(M.matcher.join("\n")); | |
| return M.nodes; | |
| }; | |
| return Matcher; | |
| })({ | |
| nodes:[], | |
| getByAll: function(context, selectorObj){ return ''; }, | |
| getById: function(context, selectorObj){ | |
| var id = selectorObj.id; | |
| if (!id) return ''; | |
| selectorObj.id = null; | |
| return "M.nodes = context.getElementById('" + id + "');"; | |
| }, | |
| getByTag: function(context, selectorObj){ | |
| var tag = selectorObj.tag || '*'; | |
| selectorObj.tag = null; | |
| return "M.nodes = context.getElementsByTagName('" + tag + "');"; | |
| }, | |
| getByClass: function(context, selectorObj){ | |
| if (!context.getElementsByClassName) return ''; | |
| var classes = selectorObj.classes; | |
| if (!classes) return ''; | |
| selectorObj.classes = null; | |
| return "M.nodes = context.getElementsByClassName(" + classes.join(' ') + ");"; | |
| }, | |
| eachStart:"each: if (M.nodes) { for (var i=0, node, parsedNodes = []; node = M.nodes[i++];) {", | |
| eachEnd:"parsedNodes.push(node); } M.nodes = parsedNodes; };", | |
| filterBy : function(filterBy){ | |
| return [' if (!(', filterBy, ')) continue each;'].join(''); | |
| }, | |
| filterByAll : function(context, selectorObj){ }, | |
| filterByClass : function(context, selectorObj){ return 'selectorObj.classNameRegex.test(node.className)'; }, | |
| filterById : function(context, selectorObj){ }, | |
| filterByTag : function(context, selectorObj){ }, | |
| filterByParents: function(context, selectorObj){ }, | |
| prepFilterByAll : function(context, selectorObj){}, | |
| prepFilterById : function(context, selectorObj){}, | |
| prepFilterByClass : function(context, selectorObj) { | |
| selectorObj.classNameRegex = /(^|\s)class1 class2($|\s)/; | |
| }, | |
| prepFilterByTag : function(context, selectorObj){}, | |
| prepFilterByParents: function(context, selectorObj){}, | |
| 0:0 | |
| }); | |
| // ========= | |
| // = Tests = | |
| // ========= | |
| // @require 'tm-file:///Users/taylott/Projects/MooTools/SubtleSlickSpeed/tests/selectors/template.js' | |
| // console.assert(document.getElementsByTagName('*').length === 1824, 'wrong number of elements'); | |
| // console.assert(Matcher(document, { tag:'*' }).length === 1824, 'Should get all elements'); | |
| // console.assert(Matcher(document, { tag:'div' }).length === document.getElementsByTagName('div').length, 'Should get all div elements'); | |
| Matcher(document, { tag:'div' }); | |
| Matcher(document, { tag:'ul', id:'nav' }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment