Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added insertRule testing for older non IE browsers

  • Loading branch information...
commit b01d242343213cad3c205d2881905624373af96e 1 parent 87ccd77
Ryan Seddon authored July 26, 2011

Showing 1 changed file with 16 additions and 5 deletions. Show diff stats Hide diff stats

  1. 21  modernizr.js
21  modernizr.js
@@ -139,7 +139,7 @@ window.Modernizr = (function( window, document, undefined ) {
139 139
                 */
140 140
      testSelector = function( selector ) {
141 141
      
142  
-       var ret = false, style, sheet, rules, cssText;
  142
+       var ret = false, style, sheet, rules, cssText, insertRule;
143 143
            qS = document.querySelector;
144 144
        
145 145
        if(!!qS) {
@@ -151,18 +151,29 @@ window.Modernizr = (function( window, document, undefined ) {
151 151
          return ret;
152 152
        }
153 153
        
154  
-       ret = injectElementWithStyles(selector + '{}', function( node ) {
  154
+       ret = injectElementWithStyles( selector + '{}', function( node ) {
155 155
          style = node.lastChild;
156 156
          sheet = style.sheet || style.styleSheet;
157 157
          rules = sheet.cssRules || sheet.rules;
158  
-         cssText = sheet.cssRules && sheet.cssRules[0] ? rules[0].cssText : sheet.cssText || "";
  158
+         insertRule = sheet.insertRule;
  159
+         cssText = sheet.cssRules && sheet.cssRules[0] ? rules[0].cssText : sheet.cssText || '';
159 160
          
160 161
          /* Easy way out for IE8+ and other browser that don't support querySelector */
161 162
          if( sheet && !rules.length ) {
162 163
            return false;
163 164
          } else {
164  
-           // Some pseudo element properties that have equivilent pseudo classes e.g ::first-line will return false positive so do a indexOf check
165  
-           return !/unknown/i.test( cssText ) && !~~cssText.indexOf(selector);
  165
+           // insertRule will work the same way as querySelector an invalid style will throw an exception
  166
+           if( insertRule ) {
  167
+             try {
  168
+               sheet.insertRule( selector + '{}', 0 );
  169
+               ret = true;
  170
+             } catch(e){}
  171
+             
  172
+             return ret;
  173
+           } else {
  174
+             // Some pseudo element properties that have equivilent pseudo classes e.g ::first-line will return false positive so do a indexOf check
  175
+             return !/unknown/i.test( cssText ) && !~~cssText.indexOf( selector );
  176
+           }
166 177
          }
167 178
        });
168 179
        

0 notes on commit b01d242

Please sign in to comment.
Something went wrong with that request. Please try again.