Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Last active June 27, 2016 08:22
Show Gist options
  • Select an option

  • Save dkesberg/f4c97c98f4b72a4ea705233ddf1f2dcb to your computer and use it in GitHub Desktop.

Select an option

Save dkesberg/f4c97c98f4b72a4ea705233ddf1f2dcb to your computer and use it in GitHub Desktop.
IE9 css can contain only up to 4095 selectors
// IE9 css can contain only up to 4095 selectors
// source: 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