Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RubenJnl
Forked from psebborn/countCSSRules.js
Created August 8, 2014 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubenJnl/5ea286e256123cb6d53f to your computer and use it in GitHub Desktop.
Save RubenJnl/5ea286e256123cb6d53f to your computer and use it in GitHub Desktop.
function countCSSRules() {
var results = '',
log = '';
if (!document.styleSheets) {
return;
}
for (var i = 0; i < document.styleSheets.length; i++) {
countSheet(document.styleSheets[i]);
}
function countSheet(sheet) {
var count = 0, countRules = 0;
if (sheet && sheet.cssRules) {
for (var j = 0, l = sheet.cssRules.length; j < l; j++) {
if (!sheet.cssRules[j].selectorText) {
continue;
}
count += sheet.cssRules[j].selectorText.split(',').length;
}
countRules += sheet.cssRules.length;
log += '\nFile: ' + (sheet.href ? sheet.href : 'inline <style> tag');
log += '\nRules: ' + sheet.cssRules.length;
log += '\nSelectors: ' + count;
log += '\n--------------------------';
if (count >= 4096) {
results += '\n********************************\nWARNING:\n There are ' + count + ' CSS Selectors in the stylesheet ' + sheet.href + ' - IE will ignore the last ' + (count - 4096) + ' selectors!\n';
} else if (countRules >= 4096) {
results += '\n********************************\nWARNING:\n There are ' + countRules + ' CSS rules in the stylesheet ' + sheet.href + ' - IE will ignore the last ' + (countRules - 4096) + ' rules!\n';
}
}
}
console.log(log);
console.log(results);
};
countCSSRules();
@RubenJnl
Copy link
Author

RubenJnl commented Aug 8, 2014

Added a warning for the rules. Found out that the maximum numer of 4096 counts for rules as well as for selectors in IE7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment