Skip to content

Commit 3747cc6

Browse files
committed
CSS: Restore the hack to get pixels for .css('width') 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 gh-1815 Refs gh-1820 Closes gh-1842
1 parent 1ba45fc commit 3747cc6

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/css/curCSS.js

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

910
function curCSS( elem, name, computed ) {
10-
var ret;
11+
var width, minWidth, maxWidth, ret,
12+
style = elem.style;
1113

1214
computed = computed || getStyles( elem );
1315

@@ -22,6 +24,29 @@ function curCSS( elem, name, computed ) {
2224
if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) {
2325
ret = jQuery.style( elem, name );
2426
}
27+
28+
// Support: Android 4.0-4.3
29+
// A tribute to the "awesome hack by Dean Edwards"
30+
// Android Browser returns percentage for some values,
31+
// but width seems to be reliably pixels.
32+
// This is against the CSSOM draft spec:
33+
// http://dev.w3.org/csswg/cssom/#resolved-values
34+
if ( !support.pixelMarginRight() && rnumnonpx.test( ret ) && rmargin.test( name ) ) {
35+
36+
// Remember the original values
37+
width = style.width;
38+
minWidth = style.minWidth;
39+
maxWidth = style.maxWidth;
40+
41+
// Put in the new values to get a computed value out
42+
style.minWidth = style.maxWidth = style.width = ret;
43+
ret = computed.width;
44+
45+
// Revert the changed values
46+
style.width = width;
47+
style.minWidth = minWidth;
48+
style.maxWidth = maxWidth;
49+
}
2550
}
2651

2752
return ret !== undefined ?

src/css/support.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define([
66
], function( jQuery, document, documentElement, support ) {
77

88
(function() {
9-
var pixelPositionVal, boxSizingReliableVal,
9+
var pixelPositionVal, boxSizingReliableVal, pixelMarginRightVal,
1010
container = document.createElement( "div" ),
1111
div = document.createElement( "div" );
1212

@@ -20,7 +20,7 @@ define([
2020
div.cloneNode( true ).style.backgroundClip = "";
2121
support.clearCloneStyle = div.style.backgroundClip === "content-box";
2222

23-
container.style.cssText = "border:0;width:0;height:0;top:0;left:-9999px;margin-top:1px;" +
23+
container.style.cssText = "border:0;width:8px;height:0;top:0;left:-9999px;margin-top:1px;" +
2424
"position:absolute";
2525
container.appendChild( div );
2626

@@ -61,6 +61,16 @@ define([
6161
}
6262
return boxSizingReliableVal;
6363
},
64+
pixelMarginRight: function() {
65+
if ( pixelMarginRightVal == null ) {
66+
div.style.cssText = "display:block;width:50%;margin-right:50%";
67+
documentElement.appendChild( container );
68+
pixelMarginRightVal =
69+
window.getComputedStyle( div, null ).marginRight === "4px";
70+
documentElement.removeChild( container );
71+
}
72+
return pixelMarginRightVal;
73+
},
6474
reliableMarginRight: function() {
6575

6676
// Support: Android 2.3

test/unit/support.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
6666
"noCloneChecked": true,
6767
"optDisabled": true,
6868
"optSelected": true,
69+
"pixelMarginRight": true,
6970
"pixelPosition": true,
7071
"radioValue": true,
7172
"reliableMarginRight": true
@@ -83,6 +84,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
8384
"noCloneChecked": false,
8485
"optDisabled": true,
8586
"optSelected": false,
87+
"pixelMarginRight": true,
8688
"pixelPosition": true,
8789
"radioValue": false,
8890
"reliableMarginRight": true
@@ -100,6 +102,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
100102
"noCloneChecked": false,
101103
"optDisabled": true,
102104
"optSelected": false,
105+
"pixelMarginRight": true,
103106
"pixelPosition": true,
104107
"radioValue": false,
105108
"reliableMarginRight": true
@@ -117,6 +120,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
117120
"noCloneChecked": true,
118121
"optDisabled": true,
119122
"optSelected": true,
123+
"pixelMarginRight": true,
120124
"pixelPosition": false,
121125
"radioValue": true,
122126
"reliableMarginRight": true
@@ -134,6 +138,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
134138
"noCloneChecked": true,
135139
"optDisabled": true,
136140
"optSelected": true,
141+
"pixelMarginRight": true,
137142
"pixelPosition": false,
138143
"radioValue": true,
139144
"reliableMarginRight": true
@@ -151,6 +156,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
151156
"noCloneChecked": true,
152157
"optDisabled": true,
153158
"optSelected": true,
159+
"pixelMarginRight": true,
154160
"pixelPosition": true,
155161
"radioValue": true,
156162
"reliableMarginRight": true
@@ -168,6 +174,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
168174
"noCloneChecked": true,
169175
"optDisabled": true,
170176
"optSelected": true,
177+
"pixelMarginRight": true,
171178
"pixelPosition": false,
172179
"radioValue": true,
173180
"reliableMarginRight": true
@@ -185,6 +192,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
185192
"noCloneChecked": true,
186193
"optDisabled": true,
187194
"optSelected": true,
195+
"pixelMarginRight": true,
188196
"pixelPosition": false,
189197
"radioValue": true,
190198
"reliableMarginRight": true
@@ -202,6 +210,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
202210
"noCloneChecked": true,
203211
"optDisabled": true,
204212
"optSelected": true,
213+
"pixelMarginRight": false,
205214
"pixelPosition": false,
206215
"radioValue": true,
207216
"reliableMarginRight": true
@@ -219,6 +228,7 @@ testIframeWithCallback( "Check CSP (https://developer.mozilla.org/en-US/docs/Sec
219228
"noCloneChecked": true,
220229
"optDisabled": false,
221230
"optSelected": true,
231+
"pixelMarginRight": true,
222232
"pixelPosition": false,
223233
"radioValue": true,
224234
"reliableMarginRight": false

0 commit comments

Comments
 (0)