Skip to content

Instantly share code, notes, and snippets.

@milsyobtaf
Created March 17, 2015 19:35
Show Gist options
  • Select an option

  • Save milsyobtaf/f1595ad212177c2b0f6b to your computer and use it in GitHub Desktop.

Select an option

Save milsyobtaf/f1595ad212177c2b0f6b to your computer and use it in GitHub Desktop.

Revisions

  1. milsyobtaf created this gist Mar 17, 2015.
    36 changes: 36 additions & 0 deletions countCSSRules
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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;
    if (sheet && sheet.cssRules) {
    for (var j = 0, l = sheet.cssRules.length; j < l; j++) {
    var rule = sheet.cssRules[j];
    if (rule instanceof CSSImportRule) {
    countSheet(rule.styleSheet);
    }
    if( !rule.selectorText ) {
    continue;
    }
    count += rule.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';
    }
    }
    }
    console.log(log);
    console.log(results);
    };
    countCSSRules();