Last active
January 20, 2017 12:39
-
-
Save mootari/b3cac7c914386e4d2803c665dbf214ac 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
(function() { | |
function parseSelectors(rule, index) { | |
rule.selectorText.split(/, */).map(function(t) { | |
var p = /\.[A-z0-9-_]+/g, m; | |
while(m = p.exec(t)) { | |
if(!index.hasOwnProperty(m[0])) { | |
index[m[0]] = 0; | |
} | |
index[m[0]]++; | |
} | |
}); | |
} | |
function parseRules(rules, index) { | |
Array.prototype.map.call(rules, function(rule) { | |
switch(rule.type) { | |
case 1: | |
parseSelectors(rule, index); | |
break; | |
case 3: | |
parseRules(rule.styleSheet.cssRules, index); | |
break; | |
case 4: | |
parseRules(rule.cssRules, index); | |
break; | |
default: | |
//debugger; | |
} | |
}); | |
} | |
console.findCssClasses = function(search) { | |
var cssClasses = {}; | |
Array.prototype.map.call(document.styleSheets, function(sheet) { | |
sheet.cssRules && parseRules(sheet.cssRules, cssClasses); | |
}); | |
var list = []; | |
for(var n in cssClasses) { | |
if(cssClasses.hasOwnProperty(n)) { | |
list.push(n); | |
} | |
} | |
list.sort(function(a, b) {return a.toLowerCase() < b.toLowerCase() ? -1 : 1;}); | |
if(arguments.length) { | |
list = list.filter(function(s) { return s.search(search) > -1; }); | |
} | |
console.log(list.join('\n')); | |
} | |
console.log('Usage: console.findCssClasses(str)'); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment