Skip to content

Commit 7f6a991

Browse files
mikesherovdmethvin
authored andcommitted
Fix #10639. Make percent-specified margins return px values in WebKit.
1 parent 2c75a99 commit 7f6a991

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/css.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var ralpha = /alpha\([^)]*\)/i,
77
rnumpx = /^-?\d+(?:px)?$/i,
88
rnumnopx = /^-?\d+(?!px)[^\d\s]+$/i,
99
rrelNum = /^([\-+])=([\-+.\de]+)/,
10+
rmargin = /^margin/,
1011

1112
cssShow = { position: "absolute", visibility: "hidden", display: "block" },
1213
cssWidth = [ "Left", "Right" ],
@@ -256,7 +257,7 @@ jQuery(function() {
256257

257258
if ( document.defaultView && document.defaultView.getComputedStyle ) {
258259
getComputedStyle = function( elem, name ) {
259-
var ret, defaultView, computedStyle;
260+
var ret, defaultView, computedStyle, width, style = elem.style;
260261

261262
name = name.replace( rupper, "-$1" ).toLowerCase();
262263

@@ -268,6 +269,16 @@ if ( document.defaultView && document.defaultView.getComputedStyle ) {
268269
}
269270
}
270271

272+
// A tribute to the "awesome hack by Dean Edwards"
273+
// WebKit uses "computed value (percentage if specified)" instead of "used value" for margins
274+
// which is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
275+
if ( !jQuery.support.pixelMargin && computedStyle && rmargin.test( name ) && rnumnopx.test( ret ) ) {
276+
width = style.width;
277+
style.width = ret;
278+
ret = computedStyle.width;
279+
style.width = width;
280+
}
281+
271282
return ret;
272283
};
273284
}
@@ -299,7 +310,7 @@ if ( document.documentElement.currentStyle ) {
299310
if ( rsLeft ) {
300311
elem.runtimeStyle.left = elem.currentStyle.left;
301312
}
302-
style.left = name === "fontSize" ? "1em" : ( ret || 0 );
313+
style.left = name === "fontSize" ? "1em" : ret;
303314
ret = style.pixelLeft + "px";
304315

305316
// Revert the changed values

src/support.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ jQuery.support = (function() {
9191
noCloneEvent: true,
9292
inlineBlockNeedsLayout: false,
9393
shrinkWrapBlocks: false,
94-
reliableMarginRight: true
94+
reliableMarginRight: true,
95+
pixelMargin: true
9596
};
9697

9798
// Make sure checked status is properly cloned
@@ -277,6 +278,11 @@ jQuery.support = (function() {
277278
offsetSupport.subtractsBorderForOverflowNotVisible = ( inner.offsetTop === -5 );
278279
offsetSupport.doesNotIncludeMarginInBodyOffset = ( body.offsetTop !== conMarginTop );
279280

281+
if( window.getComputedStyle ) {
282+
div.style.marginTop = "1%";
283+
support.pixelMargin = ( window.getComputedStyle( div, null ) || { marginTop: 0 } ).marginTop !== "1%";
284+
}
285+
280286
body.removeChild( container );
281287
div = container = null;
282288

test/unit/css.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,11 @@ test("Do not append px to 'fill-opacity' #9548", 1, function() {
542542
equal( jQuery(this).css("fill-opacity"), 1, "Do not append px to 'fill-opacity'");
543543
});
544544

545+
});
546+
547+
test("outerWidth(true) and css('margin') returning % instead of px in Webkit, see #10639", function() {
548+
var container = jQuery( "<div/>" ).width(400).appendTo( "#qunit-fixture" ),
549+
el = jQuery( "<div/>" ).css({ width: "50%", marginRight: "50%" }).appendTo( container );
550+
551+
equal( el.outerWidth(true), 400, "outerWidth(true) and css('margin') returning % instead of px in Webkit, see #10639" );
545552
});

0 commit comments

Comments
 (0)