Skip to content

Commit 25bc680

Browse files
committed
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 3747cc6 Refs gh-1815 Refs gh-1820 Refs gh-1842 Closes gh-2061
1 parent 5e5489c commit 25bc680

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/css/curCSS.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ define([
33
"../core",
44
"./var/rnumnonpx",
55
"./var/rmargin",
6+
"./support",
67
"../selector" // contains
7-
], function( exports, jQuery, rnumnonpx, rmargin ) {
8+
], function( exports, jQuery, rnumnonpx, rmargin, support ) {
89

910
var getStyles, curCSS,
1011
rposition = /^(top|right|bottom|left)$/;
@@ -36,13 +37,12 @@ if ( window.getComputedStyle ) {
3637
ret = jQuery.style( elem, name );
3738
}
3839

39-
// Support: Android 4.0-4.3
4040
// A tribute to the "awesome hack by Dean Edwards"
4141
// Android Browser returns percentage for some values,
4242
// but width seems to be reliably pixels.
4343
// This is against the CSSOM draft spec:
4444
// http://dev.w3.org/csswg/cssom/#resolved-values
45-
if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) {
45+
if ( !support.pixelMarginRight() && rnumnonpx.test( ret ) && rmargin.test( name ) ) {
4646

4747
// Remember the original values
4848
width = style.width;

src/css/support.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ define([
44
], function( jQuery, support ) {
55

66
(function() {
7-
// Minified: var b,c,d,e,f,g, h,i
8-
var div, style, a, pixelPositionVal, boxSizingReliableVal,
7+
var div, container, style, a, pixelPositionVal, boxSizingReliableVal, pixelMarginRightVal,
98
reliableHiddenOffsetsVal, reliableMarginRightVal;
109

1110
// Setup
@@ -33,40 +32,55 @@ define([
3332
div.cloneNode( true ).style.backgroundClip = "";
3433
support.clearCloneStyle = div.style.backgroundClip === "content-box";
3534

35+
container = document.createElement( "div" );
36+
container.style.cssText = "border:0;width:8px;height:0;top:0;left:-9999px;" +
37+
"padding:0;margin-top:1px;position:absolute";
38+
div.innerHTML = "";
39+
container.appendChild( div );
40+
3641
jQuery.extend(support, {
3742
reliableHiddenOffsets: function() {
38-
if ( reliableHiddenOffsetsVal == null ) {
43+
if ( pixelPositionVal == null ) {
3944
computeStyleTests();
4045
}
4146
return reliableHiddenOffsetsVal;
4247
},
4348

4449
boxSizingReliable: function() {
45-
if ( boxSizingReliableVal == null ) {
50+
// We're checking for pixelPositionVal here instead of boxSizingReliableVal
51+
// since that compresses better and they're computed together anyway.
52+
if ( pixelPositionVal == null ) {
4653
computeStyleTests();
4754
}
4855
return boxSizingReliableVal;
4956
},
5057

58+
pixelMarginRight: function() {
59+
// Support: Android 4.0-4.3
60+
if ( pixelPositionVal == null ) {
61+
computeStyleTests();
62+
}
63+
return pixelMarginRightVal;
64+
},
65+
5166
pixelPosition: function() {
5267
if ( pixelPositionVal == null ) {
5368
computeStyleTests();
5469
}
5570
return pixelPositionVal;
5671
},
5772

58-
// Support: Android 2.3
5973
reliableMarginRight: function() {
60-
if ( reliableMarginRightVal == null ) {
74+
// Support: Android 2.3
75+
if ( pixelPositionVal == null ) {
6176
computeStyleTests();
6277
}
6378
return reliableMarginRightVal;
6479
}
6580
});
6681

6782
function computeStyleTests() {
68-
// Minified: var b,c,d,j
69-
var div, container, contents,
83+
var contents, divStyle,
7084
body = document.body;
7185

7286
if ( !body || !body.style ) {
@@ -75,17 +89,16 @@ define([
7589
}
7690

7791
// Setup
78-
div = document.createElement( "div" );
79-
container = document.createElement( "div" );
80-
container.style.cssText = "position:absolute;border:0;width:0;height:0;top:0;left:-9999px";
81-
body.appendChild( container ).appendChild( div );
92+
body.appendChild( container );
8293

8394
div.style.cssText =
8495
// Support: Android 2.3
8596
// Vendor-prefix box-sizing
8697
"-webkit-box-sizing:border-box;box-sizing:border-box;" +
87-
"display:block;margin-top:1%;top:1%;" +
88-
"border:1px;padding:1px;width:4px;position:absolute";
98+
"position:absolute;display:block;" +
99+
"margin:0;margin-top:1%;margin-right:50%;" +
100+
"border:1px;padding:1px;" +
101+
"top:1%;height:4px;width:50%";
89102

90103
// Support: IE<9
91104
// Assume reasonable values in the absence of getComputedStyle
@@ -94,9 +107,10 @@ define([
94107

95108
// Check for getComputedStyle so that this code is not run in IE<9.
96109
if ( window.getComputedStyle ) {
97-
pixelPositionVal = ( window.getComputedStyle( div, null ) || {} ).top !== "1%";
98-
boxSizingReliableVal =
99-
( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px";
110+
divStyle = window.getComputedStyle( div, null );
111+
pixelPositionVal = ( divStyle || {} ).top !== "1%";
112+
boxSizingReliableVal = ( divStyle || { height: "4px" } ).height === "4px";
113+
pixelMarginRightVal = ( divStyle || { marginRight: "4px" } ).marginRight === "4px";
100114

101115
// Support: Android 2.3
102116
// Div with explicit width and no margin-right incorrectly
@@ -136,6 +150,7 @@ define([
136150
reliableHiddenOffsetsVal = contents[ 0 ].offsetHeight === 0;
137151
}
138152

153+
// Teardown
139154
body.removeChild( container );
140155
}
141156

test/unit/support.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
9494
"optDisabled": true,
9595
"optSelected": true,
9696
"ownLast": false,
97+
"pixelMarginRight": true,
9798
"pixelPosition": true,
9899
"radioValue": true,
99100
"reliableHiddenOffsets": true,
@@ -125,6 +126,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
125126
"optDisabled": true,
126127
"optSelected": false,
127128
"ownLast": false,
129+
"pixelMarginRight": true,
128130
"pixelPosition": true,
129131
"radioValue": false,
130132
"reliableHiddenOffsets": true,
@@ -156,6 +158,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
156158
"optDisabled": true,
157159
"optSelected": false,
158160
"ownLast": false,
161+
"pixelMarginRight": true,
159162
"pixelPosition": true,
160163
"radioValue": false,
161164
"reliableHiddenOffsets": true,
@@ -187,6 +190,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
187190
"optDisabled": true,
188191
"optSelected": false,
189192
"ownLast": true,
193+
"pixelMarginRight": true,
190194
"pixelPosition": false,
191195
"radioValue": false,
192196
"reliableHiddenOffsets": false,
@@ -218,6 +222,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
218222
"optDisabled": true,
219223
"optSelected": true,
220224
"ownLast": false,
225+
"pixelMarginRight": true,
221226
"pixelPosition": false,
222227
"radioValue": true,
223228
"reliableHiddenOffsets": true,
@@ -249,6 +254,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
249254
"optDisabled": true,
250255
"optSelected": true,
251256
"ownLast": false,
257+
"pixelMarginRight": true,
252258
"pixelPosition": false,
253259
"radioValue": true,
254260
"reliableHiddenOffsets": true,
@@ -280,6 +286,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
280286
"optDisabled": true,
281287
"optSelected": true,
282288
"ownLast": false,
289+
"pixelMarginRight": true,
283290
"pixelPosition": true,
284291
"radioValue": true,
285292
"reliableHiddenOffsets": true,
@@ -311,6 +318,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
311318
"optDisabled": true,
312319
"optSelected": true,
313320
"ownLast": false,
321+
"pixelMarginRight": true,
314322
"pixelPosition": false,
315323
"radioValue": true,
316324
"reliableHiddenOffsets": true,
@@ -342,6 +350,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
342350
"optDisabled": true,
343351
"optSelected": true,
344352
"ownLast": false,
353+
"pixelMarginRight": true,
345354
"pixelPosition": false,
346355
"radioValue": true,
347356
"reliableHiddenOffsets": true,
@@ -373,6 +382,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
373382
"optDisabled": true,
374383
"optSelected": true,
375384
"ownLast": false,
385+
"pixelMarginRight": false,
376386
"pixelPosition": false,
377387
"radioValue": true,
378388
"reliableHiddenOffsets": true,
@@ -404,6 +414,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
404414
"optDisabled": false,
405415
"optSelected": true,
406416
"ownLast": false,
417+
"pixelMarginRight": true,
407418
"pixelPosition": false,
408419
"radioValue": true,
409420
"reliableHiddenOffsets": true,

0 commit comments

Comments
 (0)