Created
March 11, 2014 17:30
-
-
Save mirceapiturca/9490754 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
// Unfortunately there is no native way to 'normalize a selector' | |
// This little trick makes the browser to do the job instead of implementing your tokenizer and parser | |
function normalizeSelectors(aSelector) { | |
var parser = new DOMParser(), | |
string = '<style>' + aSelector + '{}</style>', | |
doc = parser.parseFromString(string, "text/html"), | |
cssRules = doc.querySelector('style').sheet.cssRules; | |
if (cssRules[0]) { | |
return cssRules[0].selectorText | |
} else { | |
// selector is invalid... | |
return null | |
} | |
} | |
normalizeSelectors('foo bar') // -> returns 'foo bar' | |
normalizeSelectors('foo bar: fee') // -> returns null as this is a bad selector |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo at http://jsfiddle.net/4ez3k/1/