Skip to content

Instantly share code, notes, and snippets.

@furf
Last active August 29, 2015 14:04
Show Gist options
  • Save furf/db843b06ff77749883e4 to your computer and use it in GitHub Desktop.
Save furf/db843b06ff77749883e4 to your computer and use it in GitHub Desktop.
Find CSS selectors greater than n
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;
}
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