Last active
August 29, 2015 14:04
-
-
Save furf/db843b06ff77749883e4 to your computer and use it in GitHub Desktop.
Find CSS selectors greater than n
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 findLongSelectors(n) { | |
var sheets = document.styleSheets; | |
var splitRule = /\s*,\s*/; | |
var splitWhitespace = /\s+/; | |
var longRules = []; | |
[].forEach.call(document.styleSheets, function(styleSheet) { | |
if (!styleSheet.cssRules) return; | |
[].forEach.call(styleSheet.cssRules, function(cssRule) { | |
if (!cssRule.selectorText) return; | |
var selectorTexts = cssRule.selectorText.split(splitRule); | |
selectorTexts.forEach(function(selectorText) { | |
var selectors = selectorText.split(splitWhitespace); | |
var filtered = selectors.filter(function(selector) { | |
return selector.length > 1; | |
}); | |
if (filtered.length > n) { | |
longRules.push(selectorText); | |
} | |
}); | |
}); | |
}); | |
return longRules; | |
} |
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 longRules = findLongSelectors(3); | |
console.log(longRules.join('\n')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment