-
-
Save amejiarosario/520f26057d0bd17a917b to your computer and use it in GitHub Desktop.
This file contains 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
// Count number of stylesheets files and selectors (per file and total) | |
(function countCSSRules() { | |
var results = '', | |
log = '', | |
total = 0; | |
if (!document.styleSheets) { | |
console.log('No stylesheets found!'); | |
return; | |
} else { | |
console.log('Stylesheets count: ' + document.styleSheets.length); | |
} | |
for (var i = 0; i < document.styleSheets.length; i++) { | |
countSheet(document.styleSheets[i]); | |
} | |
function countSheet(sheet) { | |
var count = 0; | |
if (sheet && sheet.cssRules) { | |
for (var j = 0, l = sheet.cssRules.length; j < l; j++) { | |
//Check for imports | |
if (sheet.cssRules[j] instanceof CSSImportRule) | |
{ | |
countSheet(sheet.cssRules[j].styleSheet); | |
} | |
if( !sheet.cssRules[j].selectorText ) { | |
continue; | |
} | |
count += sheet.cssRules[j].selectorText.split(',').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 rules in the stylesheet ' + sheet.href + ' - IE will ignore the last ' + (count - 4096) + ' rules!\n'; | |
} | |
} | |
total += count; | |
} | |
console.log(log); | |
console.log(results); | |
console.log('\nTotal selectors: ' + total); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment