Skip to content

Commit

Permalink
Fix #10639. Make percent-specified margins return px values in WebKit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov authored and dmethvin committed Dec 7, 2011
1 parent 2c75a99 commit 7f6a991
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var ralpha = /alpha\([^)]*\)/i,
rnumpx = /^-?\d+(?:px)?$/i,
rnumnopx = /^-?\d+(?!px)[^\d\s]+$/i,
rrelNum = /^([\-+])=([\-+.\de]+)/,
rmargin = /^margin/,

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

if ( document.defaultView && document.defaultView.getComputedStyle ) {
getComputedStyle = function( elem, name ) {
var ret, defaultView, computedStyle;
var ret, defaultView, computedStyle, width, style = elem.style;

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

Expand All @@ -268,6 +269,16 @@ if ( document.defaultView && document.defaultView.getComputedStyle ) {
}
}

// A tribute to the "awesome hack by Dean Edwards"
// WebKit uses "computed value (percentage if specified)" instead of "used value" for margins
// which is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
if ( !jQuery.support.pixelMargin && computedStyle && rmargin.test( name ) && rnumnopx.test( ret ) ) {
width = style.width;
style.width = ret;
ret = computedStyle.width;
style.width = width;
}

return ret;
};
}
Expand Down Expand Up @@ -299,7 +310,7 @@ if ( document.documentElement.currentStyle ) {
if ( rsLeft ) {
elem.runtimeStyle.left = elem.currentStyle.left;
}
style.left = name === "fontSize" ? "1em" : ( ret || 0 );
style.left = name === "fontSize" ? "1em" : ret;
ret = style.pixelLeft + "px";

// Revert the changed values
Expand Down
8 changes: 7 additions & 1 deletion src/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ jQuery.support = (function() {
noCloneEvent: true,
inlineBlockNeedsLayout: false,
shrinkWrapBlocks: false,
reliableMarginRight: true
reliableMarginRight: true,
pixelMargin: true
};

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

if( window.getComputedStyle ) {
div.style.marginTop = "1%";
support.pixelMargin = ( window.getComputedStyle( div, null ) || { marginTop: 0 } ).marginTop !== "1%";
}

body.removeChild( container );
div = container = null;

Expand Down
7 changes: 7 additions & 0 deletions test/unit/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,11 @@ test("Do not append px to 'fill-opacity' #9548", 1, function() {
equal( jQuery(this).css("fill-opacity"), 1, "Do not append px to 'fill-opacity'");
});

});

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

equal( el.outerWidth(true), 400, "outerWidth(true) and css('margin') returning % instead of px in Webkit, see #10639" );
});

0 comments on commit 7f6a991

Please sign in to comment.