Skip to content

Instantly share code, notes, and snippets.

@bruslim
Forked from psebborn/countCSSRules.js
Last active December 15, 2015 13:59
Show Gist options
  • Save bruslim/5271366 to your computer and use it in GitHub Desktop.
Save bruslim/5271366 to your computer and use it in GitHub Desktop.
(function() {
var results = '',
log = '';
function countSheet(sheet) {
var count = 0;
if (!sheet || !sheet.cssRules) return;
for (var j = 0; j < sheet.cssRules.length; j++) {
if (!sheet.cssRules[j] || !sheet.cssRules[j].selectorText) continue;
count += sheet.cssRules[j].selectorText.split(',').length;
}
log += '\nFile: ' + (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 rules in the stylesheet '
+ sheet.href + ' - IE will ignore the last ' + (count - 4096) + ' rules!\n';
}
}
if (!document.styleSheets) return;
for (var i = 0; i < document.styleSheets.length; i++) {
countSheet(document.styleSheets[i]);
}
if (!console) return;
if (log && log.length > 0) console.log(log);
if (results && results.length > 0) console.log(results);
})();
@bruslim
Copy link
Author

bruslim commented Apr 3, 2013

cleaned up the original code, added console check for ie - but you shouldn't be including this in production code

it doesn't count imported style-sheets, this script is only really useful if you concatenate all of your style-sheets together.

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