Skip to content

Instantly share code, notes, and snippets.

@ghoullier
Forked from psebborn/countCSSRules.js
Last active December 20, 2015 20:59
Show Gist options
  • Save ghoullier/6193911 to your computer and use it in GitHub Desktop.
Save ghoullier/6193911 to your computer and use it in GitHub Desktop.
Count CSS Rules
;(function () {
var results = [],
log = [],
list = document.styleSheets;
if (!list) {
return;
}
for (var i = 0, x = list.length; i < x; i++) {
countSheet(list[i]);
}
function countSheet(sheet) {
var count = 0,
rules;
if (sheet && sheet.cssRules) {
rules = sheet.cssRules
for (var j = 0, l = rules.length; j < l; j++) {
if( !rules[j].selectorText ) {
continue;
}
count += sheet.cssRules[j].selectorText.split(',').length;
}
log.push('\nFile: ' + (sheet.href ? sheet.href : 'inline <style> tag'));
log.push('\nRules: ' + sheet.cssRules.length);
log.push('\nSelectors: ' + count);
log.push('\n--------------------------');
if (count >= 4096) {
results.push('\n********************************\nWARNING:\n There are ' + count + ' CSS rules in the stylesheet ' + sheet.href + ' - IE will ignore the last ' + (count - 4096) + ' rules!\n');
}
}
}
console.log(log.join(''));
console.log(results.join(''));
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment