]> source.dussan.org Git - jquery.git/commitdiff
Rewrite of the support module. We use a temporary body element in order not to have...
authorjaubourg <j@ubourg.net>
Thu, 7 Apr 2011 04:51:37 +0000 (06:51 +0200)
committerjaubourg <j@ubourg.net>
Thu, 7 Apr 2011 04:51:37 +0000 (06:51 +0200)
src/support.js

index 4c309562f7c17c6a7d17ebfd1c2a5a24af514c5b..d5625bed24bea3744ebb85f118427883f0a13880 100644 (file)
@@ -1,44 +1,58 @@
 (function( jQuery ) {
 
-(function() {
-
-       jQuery.support = {};
-
-       var div = document.createElement("div");
-
-       div.style.display = "none";
+jQuery.support = (function() {
+
+       var div = document.createElement( "div" ),
+               all,
+               a,
+               select,
+               opt,
+               input,
+               support,
+               fragment,
+               body,
+               bodyStyle,
+               tds,
+               events,
+               eventName,
+               i,
+               isSupported;
+
+       // Preliminary tests
        div.innerHTML = "   <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/>";
 
-       var all = div.getElementsByTagName("*"),
-               a = div.getElementsByTagName("a")[0],
-               select = document.createElement("select"),
-               opt = select.appendChild( document.createElement("option") ),
-               input = div.getElementsByTagName("input")[0];
+       all = div.getElementsByTagName( "*" );
+       a = div.getElementsByTagName( "a" )[ 0 ];
 
        // Can't get basic test support
        if ( !all || !all.length || !a ) {
-               return;
+               return {};
        }
 
-       jQuery.support = {
+       // First batch of supports tests
+       select = document.createElement( "select" );
+       opt = select.appendChild( document.createElement("option") );
+       input = div.getElementsByTagName( "input" )[ 0 ];
+
+       support = {
                // IE strips leading whitespace when .innerHTML is used
-               leadingWhitespace: div.firstChild.nodeType === 3,
+               leadingWhitespace: ( div.firstChild.nodeType === 3 ),
 
                // Make sure that tbody elements aren't automatically inserted
                // IE will insert them into empty tables
-               tbody: !div.getElementsByTagName("tbody").length,
+               tbody: !div.getElementsByTagName( "tbody" ).length,
 
                // Make sure that link elements get serialized correctly by innerHTML
                // This requires a wrapper element in IE
-               htmlSerialize: !!div.getElementsByTagName("link").length,
+               htmlSerialize: !!div.getElementsByTagName( "link" ).length,
 
                // Get the style information from getAttribute
-               // (IE uses .cssText insted)
+               // (IE uses .cssText instead)
                style: /red/.test( a.getAttribute("style") ),
 
                // Make sure that URLs aren't manipulated
                // (IE normalizes it by default)
-               hrefNormalized: a.getAttribute("href") === "/a",
+               hrefNormalized: ( a.getAttribute( "href" ) === "/a" ),
 
                // Make sure that element opacity exists
                // (IE uses filter instead)
                optSelected: opt.selected,
 
                // Will be defined later
+               submitBubbles: true,
+               changeBubbles: true,
                deleteExpando: true,
-               optDisabled: false,
-               checkClone: false,
                noCloneEvent: true,
-               noCloneChecked: true,
-               boxModel: null,
                inlineBlockNeedsLayout: false,
                shrinkWrapBlocks: false,
-               reliableHiddenOffsets: true,
                reliableMarginRight: true
        };
 
+       // Make sure checked status is properly cloned
        input.checked = true;
-       jQuery.support.noCloneChecked = input.cloneNode( true ).checked;
+       support.noCloneChecked = input.cloneNode( true ).checked;
 
        // Make sure that the options inside disabled selects aren't marked as disabled
-       // (WebKit marks them as diabled)
+       // (WebKit marks them as disabled)
        select.disabled = true;
-       jQuery.support.optDisabled = !opt.disabled;
-
-       var _scriptEval = null;
-       jQuery.support.scriptEval = function() {
-               if ( _scriptEval === null ) {
-                       var root = document.documentElement,
-                               script = document.createElement("script"),
-                               id = "script" + jQuery.now();
-
-                       // Make sure that the execution of code works by injecting a script
-                       // tag with appendChild/createTextNode
-                       // (IE doesn't support this, fails, and uses .text instead)
-                       try {
-                               script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
-                       } catch(e) {}
-
-                       root.insertBefore( script, root.firstChild );
-
-                       if ( window[ id ] ) {
-                               _scriptEval = true;
-                               delete window[ id ];
-                       } else {
-                               _scriptEval = false;
-                       }
-
-                       root.removeChild( script );
-               }
-
-               return _scriptEval;
-       };
+       support.optDisabled = !opt.disabled;
 
        // Test to see if it's possible to delete an expando from an element
        // Fails in Internet Explorer
        try {
                delete div.test;
-
-       } catch(e) {
-               jQuery.support.deleteExpando = false;
+       } catch( e ) {
+               support.deleteExpando = false;
        }
 
        if ( !div.addEventListener && div.attachEvent && div.fireEvent ) {
-               div.attachEvent("onclick", function click() {
+               div.attachEvent( "onclick", function click() {
                        // Cloning a node shouldn't copy over any
                        // bound event handlers (IE does this)
-                       jQuery.support.noCloneEvent = false;
-                       div.detachEvent("onclick", click);
-               });
-               div.cloneNode(true).fireEvent("onclick");
+                       support.noCloneEvent = false;
+                       div.detachEvent( "onclick", click );
+               } );
+               div.cloneNode( true ).fireEvent( "onclick" );
        }
 
-       div = document.createElement("div");
        div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>";
 
-       var fragment = document.createDocumentFragment();
+       fragment = document.createDocumentFragment();
        fragment.appendChild( div.firstChild );
 
        // WebKit doesn't clone checked state correctly in fragments
-       jQuery.support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked;
+       support.checkClone = fragment.cloneNode( true ).cloneNode( true ).lastChild.checked;
 
-       // Figure out if the W3C box model works as expected
-       // document.body must exist before we can do this
-       jQuery(function() {
-               var div = document.createElement("div"),
-                       body = document.getElementsByTagName("body")[0];
-
-               // Frameset documents with no body should not run this code
-               if ( !body ) {
-                       return;
-               }
+       div.innerHTML = "";
 
-               div.style.width = div.style.paddingLeft = "1px";
-               body.appendChild( div );
-               jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
-
-               if ( "zoom" in div.style ) {
-                       // Check if natively block-level elements act like inline-block
-                       // elements when setting their display to 'inline' and giving
-                       // them layout
-                       // (IE < 8 does this)
-                       div.style.display = "inline";
-                       div.style.zoom = 1;
-                       jQuery.support.inlineBlockNeedsLayout = div.offsetWidth === 2;
-
-                       // Check if elements with layout shrink-wrap their children
-                       // (IE 6 does this)
-                       div.style.display = "";
-                       div.innerHTML = "<div style='width:4px;'></div>";
-                       jQuery.support.shrinkWrapBlocks = div.offsetWidth !== 2;
-               }
+       // Figure out if the W3C box model works as expected
+       div.style.width = div.style.paddingLeft = "1px";
+
+       // We use our own, invisible, body
+       body = document.createElement( "body" );
+       bodyStyle = {
+               visibility: "hidden",
+               width: 0,
+               height: 0,
+               border: 0,
+               margin: 0
+       };
+       for ( i in bodyStyle ) {
+               body.style[ i ] = bodyStyle[ i ];
+       }
+       body.appendChild( div );
+       document.documentElement.appendChild( body );
+
+       support.boxModel = div.offsetWidth === 2;
+
+       if ( "zoom" in div.style ) {
+               // Check if natively block-level elements act like inline-block
+               // elements when setting their display to 'inline' and giving
+               // them layout
+               // (IE < 8 does this)
+               div.style.display = "inline";
+               div.style.zoom = 1;
+               support.inlineBlockNeedsLayout = ( div.offsetWidth === 2 );
+
+               // Check if elements with layout shrink-wrap their children
+               // (IE 6 does this)
+               div.style.display = "";
+               div.innerHTML = "<div style='width:4px;'></div>";
+               support.shrinkWrapBlocks = ( div.offsetWidth !== 2 );
+       }
 
-               div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
-               var tds = div.getElementsByTagName("td");
-
-               // Check if table cells still have offsetWidth/Height when they are set
-               // to display:none and there are still other visible table cells in a
-               // table row; if so, offsetWidth/Height are not reliable for use when
-               // determining if an element has been hidden directly using
-               // display:none (it is still safe to use offsets if a parent element is
-               // hidden; don safety goggles and see bug #4512 for more information).
-               // (only IE 8 fails this test)
-               jQuery.support.reliableHiddenOffsets = tds[0].offsetHeight === 0;
-
-               tds[0].style.display = "";
-               tds[1].style.display = "none";
-
-               // Check if empty table cells still have offsetWidth/Height
-               // (IE < 8 fail this test)
-               jQuery.support.reliableHiddenOffsets = jQuery.support.reliableHiddenOffsets && tds[0].offsetHeight === 0;
-               div.innerHTML = "";
-
-               // Check if div with explicit width and no margin-right incorrectly
-               // gets computed margin-right based on width of container. For more
-               // info see bug #3333
-               // Fails in WebKit before Feb 2011 nightlies
-               // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
-               if ( document.defaultView && document.defaultView.getComputedStyle ) {
-                       div.style.width = "1px";
-                       div.style.marginRight = "0";
-                       jQuery.support.reliableMarginRight = ( parseInt(document.defaultView.getComputedStyle(div, null).marginRight, 10) || 0 ) === 0;
-               }
+       div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
+       tds = div.getElementsByTagName( "td" );
+
+       // Check if table cells still have offsetWidth/Height when they are set
+       // to display:none and there are still other visible table cells in a
+       // table row; if so, offsetWidth/Height are not reliable for use when
+       // determining if an element has been hidden directly using
+       // display:none (it is still safe to use offsets if a parent element is
+       // hidden; don safety goggles and see bug #4512 for more information).
+       // (only IE 8 fails this test)
+       isSupported = ( tds[ 0 ].offsetHeight === 0 );
+
+       tds[ 0 ].style.display = "";
+       tds[ 1 ].style.display = "none";
+
+       // Check if empty table cells still have offsetWidth/Height
+       // (IE < 8 fail this test)
+       support.reliableHiddenOffsets = isSupported && ( tds[ 0 ].offsetHeight === 0 );
+       div.innerHTML = "";
+
+       // Check if div with explicit width and no margin-right incorrectly
+       // gets computed margin-right based on width of container. For more
+       // info see bug #3333
+       // Fails in WebKit before Feb 2011 nightlies
+       // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
+       if ( document.defaultView && document.defaultView.getComputedStyle ) {
+               div.style.width = "1px";
+               div.style.marginRight = "0";
+               support.reliableMarginRight =
+                       ( parseInt( document.defaultView.getComputedStyle(div).marginRight, 10 ) || 0 ) === 0;
+       }
 
-               body.removeChild( div ).style.display = "none";
-               div = tds = null;
-       });
+       // Remove the body element we added
+       document.documentElement.removeChild( body );
 
        // Technique from Juriy Zaytsev
        // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
-       var eventSupported = function( eventName ) {
-               var el = document.createElement("div");
-               eventName = "on" + eventName;
-
-               // We only care about the case where non-standard event systems
-               // are used, namely in IE. Short-circuiting here helps us to
-               // avoid an eval call (in setAttribute) which can cause CSP
-               // to go haywire. See: https://developer.mozilla.org/en/Security/CSP
-               if ( !el.attachEvent ) {
-                       return true;
-               }
-
-               var isSupported = (eventName in el);
-               if ( !isSupported ) {
-                       el.setAttribute(eventName, "return;");
-                       isSupported = typeof el[eventName] === "function";
+       // We only care about the case where non-standard event systems
+       // are used, namely in IE. Short-circuiting here helps us to
+       // avoid an eval call (in setAttribute) which can cause CSP
+       // to go haywire. See: https://developer.mozilla.org/en/Security/CSP
+       if ( div.attachEvent ) {
+               for( i in {
+                       submit: 1,
+                       change: 1,
+                       focusin: 1
+               } ) {
+                       eventName = "on" + i;
+                       isSupported = ( eventName in div );
+                       if ( !isSupported ) {
+                               div.setAttribute( eventName, "return;" );
+                               isSupported = ( typeof div[ eventName ] === "function" );
+                       }
+                       support[ i + "Bubbles" ] = isSupported;
                }
-               return isSupported;
-       };
-
-       jQuery.support.submitBubbles = eventSupported("submit");
-       jQuery.support.changeBubbles = eventSupported("change");
+       }
 
        // release memory in IE
-       div = all = a = null;
+       body = div = all = a = tds = undefined;
+
+       return support;
 })();
+
+// Keep track of boxModel
+jQuery.boxModel = jQuery.support.boxModel;
+
 })( jQuery );