From: Michał Gołębiowski Date: Wed, 4 Feb 2015 13:36:18 +0000 (+0100) Subject: CSS: Add a support test for the hack for .css('marginRight') etc. X-Git-Tag: 3.0.0-alpha1+compat~125 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=25bc6809c56026aa4f21dbb73eb950ba2677eb21;p=jquery.git CSS: Add a support test for the hack for .css('marginRight') etc. This hack turns out to be needed by Android 4.0-4.3. Add a support test so that the hack is invoked only where needed. Refs 3747cc642a48d2a5a8ac83069f66bddd33bea301 Refs gh-1815 Refs gh-1820 Refs gh-1842 Closes gh-2061 --- diff --git a/src/css/curCSS.js b/src/css/curCSS.js index c3210f186..c1a0d2de1 100644 --- a/src/css/curCSS.js +++ b/src/css/curCSS.js @@ -3,8 +3,9 @@ define([ "../core", "./var/rnumnonpx", "./var/rmargin", + "./support", "../selector" // contains -], function( exports, jQuery, rnumnonpx, rmargin ) { +], function( exports, jQuery, rnumnonpx, rmargin, support ) { var getStyles, curCSS, rposition = /^(top|right|bottom|left)$/; @@ -36,13 +37,12 @@ if ( window.getComputedStyle ) { ret = jQuery.style( elem, name ); } - // Support: Android 4.0-4.3 // A tribute to the "awesome hack by Dean Edwards" // Android Browser returns percentage for some values, // but width seems to be reliably pixels. // This is against the CSSOM draft spec: // http://dev.w3.org/csswg/cssom/#resolved-values - if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) { + if ( !support.pixelMarginRight() && rnumnonpx.test( ret ) && rmargin.test( name ) ) { // Remember the original values width = style.width; diff --git a/src/css/support.js b/src/css/support.js index e8f8dd4b7..2898f0cb5 100644 --- a/src/css/support.js +++ b/src/css/support.js @@ -4,8 +4,7 @@ define([ ], function( jQuery, support ) { (function() { - // Minified: var b,c,d,e,f,g, h,i - var div, style, a, pixelPositionVal, boxSizingReliableVal, + var div, container, style, a, pixelPositionVal, boxSizingReliableVal, pixelMarginRightVal, reliableHiddenOffsetsVal, reliableMarginRightVal; // Setup @@ -33,21 +32,37 @@ define([ div.cloneNode( true ).style.backgroundClip = ""; support.clearCloneStyle = div.style.backgroundClip === "content-box"; + container = document.createElement( "div" ); + container.style.cssText = "border:0;width:8px;height:0;top:0;left:-9999px;" + + "padding:0;margin-top:1px;position:absolute"; + div.innerHTML = ""; + container.appendChild( div ); + jQuery.extend(support, { reliableHiddenOffsets: function() { - if ( reliableHiddenOffsetsVal == null ) { + if ( pixelPositionVal == null ) { computeStyleTests(); } return reliableHiddenOffsetsVal; }, boxSizingReliable: function() { - if ( boxSizingReliableVal == null ) { + // We're checking for pixelPositionVal here instead of boxSizingReliableVal + // since that compresses better and they're computed together anyway. + if ( pixelPositionVal == null ) { computeStyleTests(); } return boxSizingReliableVal; }, + pixelMarginRight: function() { + // Support: Android 4.0-4.3 + if ( pixelPositionVal == null ) { + computeStyleTests(); + } + return pixelMarginRightVal; + }, + pixelPosition: function() { if ( pixelPositionVal == null ) { computeStyleTests(); @@ -55,9 +70,9 @@ define([ return pixelPositionVal; }, - // Support: Android 2.3 reliableMarginRight: function() { - if ( reliableMarginRightVal == null ) { + // Support: Android 2.3 + if ( pixelPositionVal == null ) { computeStyleTests(); } return reliableMarginRightVal; @@ -65,8 +80,7 @@ define([ }); function computeStyleTests() { - // Minified: var b,c,d,j - var div, container, contents, + var contents, divStyle, body = document.body; if ( !body || !body.style ) { @@ -75,17 +89,16 @@ define([ } // Setup - div = document.createElement( "div" ); - container = document.createElement( "div" ); - container.style.cssText = "position:absolute;border:0;width:0;height:0;top:0;left:-9999px"; - body.appendChild( container ).appendChild( div ); + body.appendChild( container ); div.style.cssText = // Support: Android 2.3 // Vendor-prefix box-sizing "-webkit-box-sizing:border-box;box-sizing:border-box;" + - "display:block;margin-top:1%;top:1%;" + - "border:1px;padding:1px;width:4px;position:absolute"; + "position:absolute;display:block;" + + "margin:0;margin-top:1%;margin-right:50%;" + + "border:1px;padding:1px;" + + "top:1%;height:4px;width:50%"; // Support: IE<9 // Assume reasonable values in the absence of getComputedStyle @@ -94,9 +107,10 @@ define([ // Check for getComputedStyle so that this code is not run in IE<9. if ( window.getComputedStyle ) { - pixelPositionVal = ( window.getComputedStyle( div, null ) || {} ).top !== "1%"; - boxSizingReliableVal = - ( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px"; + divStyle = window.getComputedStyle( div, null ); + pixelPositionVal = ( divStyle || {} ).top !== "1%"; + boxSizingReliableVal = ( divStyle || { height: "4px" } ).height === "4px"; + pixelMarginRightVal = ( divStyle || { marginRight: "4px" } ).marginRight === "4px"; // Support: Android 2.3 // Div with explicit width and no margin-right incorrectly @@ -136,6 +150,7 @@ define([ reliableHiddenOffsetsVal = contents[ 0 ].offsetHeight === 0; } + // Teardown body.removeChild( container ); } diff --git a/test/unit/support.js b/test/unit/support.js index 86745c758..74692c06a 100644 --- a/test/unit/support.js +++ b/test/unit/support.js @@ -94,6 +94,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": true, "radioValue": true, "reliableHiddenOffsets": true, @@ -125,6 +126,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": false, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": true, "radioValue": false, "reliableHiddenOffsets": true, @@ -156,6 +158,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": false, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": true, "radioValue": false, "reliableHiddenOffsets": true, @@ -187,6 +190,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": false, "ownLast": true, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": false, "reliableHiddenOffsets": false, @@ -218,6 +222,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true, @@ -249,6 +254,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true, @@ -280,6 +286,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": true, "radioValue": true, "reliableHiddenOffsets": true, @@ -311,6 +318,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true, @@ -342,6 +350,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true, @@ -373,6 +382,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": true, "optSelected": true, "ownLast": false, + "pixelMarginRight": false, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true, @@ -404,6 +414,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec "optDisabled": false, "optSelected": true, "ownLast": false, + "pixelMarginRight": true, "pixelPosition": false, "radioValue": true, "reliableHiddenOffsets": true,