Created
February 18, 2009 04:50
-
-
Save subtleGradient/66197 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 SubtleSlickParse = (function(){ | |
| function SubtleSlickParse(CSS3_Selectors){ | |
| var selector = ''+CSS3_Selectors; | |
| // if(cache[selector]) return cache[selector]; | |
| parsedSelectors = []; | |
| while (selector != (selector = selector.replace(parseregexp, parser))); | |
| return cache[''+CSS3_Selectors] = parsedSelectors; | |
| }; | |
| var parseregexp = new RegExp("\ | |
| (?x)\ | |
| ^(?:\ | |
| \\s+$\ | |
| |(?: \\s* (,) \\s* ) # Combinator\n\ | |
| |(?: \\s* (\\s|\\>|\\+|\\~) \\s* ) # Combinator\n\ | |
| |(?: ( \\w+ ) \\b ) # Tag Name\n\ | |
| |(?: \\# ( [a-z][a-z0-9_-]* ) \\b ) # ID\n\ | |
| |(?: \\. ( [a-z][a-z0-9_-]* ) \\b ) # ClassName\n\ | |
| |(?: \\[ ( ([-_:a-z0-9]+) (?: ([|~^$*]?=) (?: \"([^\"]*)\" | '([^']*)' | ([^\\]]*) ) )? ) \\] ) # attribute\n\ | |
| |(?: :+ ( [a-z][a-z0-9_-]* ) \\b (?: \\( ([^\\)]+) \\) )? ) # PseudoClassPseudoClassValue\n\ | |
| )".replace(/\(\?x\)|\s+#.*$|\s+/gim,''),'i' | |
| ); | |
| var map = { | |
| // Natural replace function argument position | |
| rawMatch : 0, | |
| offset : -2, | |
| string : -1, | |
| // Replace function argument position | |
| separator : 1, | |
| combinator : 2, | |
| combinatorChild : 2, | |
| tagName : 3, | |
| id : 4, | |
| className : 5, | |
| attribute : 6, | |
| attributeKey : 7, | |
| attributeOperator : 8, | |
| attributeValueDouble : 9, | |
| attributeValueSingle : 10, | |
| attributeValue : 11, | |
| pseudoClass : 12, | |
| pseudoClassValue : 13 | |
| }; | |
| var cache = {}; | |
| SubtleSlickParse.cache = cache; | |
| var parsedSelectors; | |
| var these_selectors; | |
| var this_selector; | |
| function parser(){ | |
| var a = arguments; | |
| SubtleSlickParse.debug && console.log({ | |
| argumentsLength : a.length, | |
| rawMatch : a[map.rawMatch], | |
| offset : a[arguments.length-2], | |
| string : a[arguments.length-1], | |
| separator : a[map.separator], | |
| combinator : a[map.combinator], | |
| combinatorChild : a[map.combinatorChild], | |
| tagName : a[map.tagName], | |
| id : a[map.id], | |
| className : a[map.className], | |
| attribute : a[map.attribute], | |
| attributeKey : a[map.attributeKey], | |
| attributeOperator : a[map.attributeOperator], | |
| attributeValue : a[map.attributeValue] || a[map.attributeValueDouble] || a[map.attributeValueSingle], | |
| pseudoClass : a[map.pseudoClass], | |
| pseudoClassValue : a[map.pseudoClassValue] | |
| }); | |
| if (!parsedSelectors.length || a[map.separator]) { | |
| // Make a new selector! | |
| parsedSelectors.push([]); | |
| these_selectors = parsedSelectors[parsedSelectors.length-1]; | |
| if (parsedSelectors.length-1) return ''; | |
| } | |
| if (!these_selectors.length || a[map.combinatorChild] || a[map.combinator]) { | |
| // Make a new simple selector! | |
| these_selectors.push({ | |
| combinator: a[map.combinatorChild] || a[map.combinator], | |
| tag : null, | |
| id : null, | |
| parsed:{ | |
| pseudos :[], | |
| classes :[], | |
| attributes :[] | |
| } | |
| }); | |
| this_selector = these_selectors[these_selectors.length-1]; | |
| if (these_selectors.length-1) return ''; | |
| } | |
| if (a[map.tagName ]) return (this_selector.tag = a[map.tagName])&&''; | |
| if (a[map.id ]) return (this_selector.id = a[map.id ])&&''; | |
| if (a[map.className ]) return this_selector.parsed.classes.push(a[map.className]); | |
| if (a[map.attribute ]) return this_selector.parsed.attributes.push({ | |
| name : a[map.attributeKey], | |
| operator : a[map.attributeOperator], | |
| value : a[map.attributeValue] || a[map.attributeValueDouble] || a[map.attributeValueSingle] | |
| })&&''; | |
| if (a[map.pseudoClass]) return this_selector.parsed.pseudos.push({ | |
| name : a[map.pseudoClass], | |
| value : a[map.pseudoClassValue] | |
| })&&''; | |
| return ''; | |
| }; | |
| return SubtleSlickParse; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment