Last active
June 15, 2016 13:23
-
-
Save marcelweder/fded29412f2268a71799fa971e9e9466 to your computer and use it in GitHub Desktop.
Count all CSS-Rules in WebClient
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
// See also: http://stackoverflow.com/questions/9906794/internet-explorers-css-rules-limits | |
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) { | |
if (sheet && sheet.cssRules) { | |
var count = countSelectors(sheet); | |
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'; | |
} | |
} | |
} | |
function countSelectors(group) { | |
var count = 0 | |
for (var j = 0, l = group.cssRules.length; j < l; j++) { | |
var rule = group.cssRules[j]; | |
if (rule instanceof CSSImportRule) { | |
countSheet(rule.styleSheet); | |
} | |
if (rule instanceof CSSMediaRule) { | |
count += countSelectors(rule); | |
} | |
if( !rule.selectorText ) { | |
continue; | |
} | |
count += rule.selectorText.split(',').length; | |
} | |
return count; | |
} | |
console.log(log); | |
console.log(results); | |
}; | |
countCSSRules(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment