Skip to content

Commit aa3fabc

Browse files
mikesherovdmethvin
authored andcommitted
Fix #12088, Safari 5 and more percentages in getComputedStyle
In particular, min-width and max-width are taunting the awesome hack. Closes jquerygh-865.
1 parent ff7a434 commit aa3fabc

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

src/css.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ jQuery.extend({
276276
// and getComputedStyle here to produce a better gzip size
277277
if ( window.getComputedStyle ) {
278278
curCSS = function( elem, name ) {
279-
var ret, width,
279+
var ret, width, minWidth, maxWidth,
280280
computed = getComputedStyle( elem, null ),
281281
style = elem.style;
282282

@@ -288,13 +288,20 @@ if ( window.getComputedStyle ) {
288288
}
289289

290290
// A tribute to the "awesome hack by Dean Edwards"
291-
// WebKit uses "computed value (percentage if specified)" instead of "used value" for margins
292-
// which is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
293-
if ( !jQuery.support.pixelMargin && rmargin.test( name ) && rnumnonpx.test( ret ) ) {
291+
// Chrome < 17 and Safari 5.0 uses "computed value" instead of "used value" for margin-right
292+
// Safari 5.1.7 (at least) returns percentage for a larger set of values, but width seems to be reliably pixels
293+
// this is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
294+
if ( rnumnonpx.test( ret ) && !rposition.test( name ) ) {
294295
width = style.width;
295-
style.width = ret;
296+
minWidth = style.minWidth;
297+
maxWidth = style.maxWidth;
298+
299+
style.minWidth = style.maxWidth = style.width = ret;
296300
ret = computed.width;
301+
297302
style.width = width;
303+
style.minWidth = minWidth;
304+
style.maxWidth = maxWidth;
298305
}
299306
}
300307

src/support.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ jQuery.support = (function() {
9191
inlineBlockNeedsLayout: false,
9292
shrinkWrapBlocks: false,
9393
reliableMarginRight: true,
94-
pixelMargin: true,
9594
boxSizingReliable: true,
9695
pixelPosition: false
9796
};
@@ -220,7 +219,6 @@ jQuery.support = (function() {
220219
// The difference between window.getComputedStyle and getComputedStyle is
221220
// 7 bytes
222221
if ( window.getComputedStyle ) {
223-
support.pixelMargin = ( window.getComputedStyle( div, null ) || {} ).marginTop !== "1%";
224222
support.pixelPosition = ( window.getComputedStyle( div, null ) || {} ).top !== "1%";
225223
support.boxSizingReliable = ( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px";
226224

test/unit/css.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,15 @@ test("marginRight computed style (bug #3333)", function() {
641641
equal($div.css("marginRight"), "0px", "marginRight correctly calculated with a width and display block");
642642
});
643643

644+
test("box model properties incorrectly returning % instead of px, see #10639 and #12088", function() {
645+
var container = jQuery("<div/>").width( 400 ).appendTo("#qunit-fixture"),
646+
el = jQuery("<div/>").css({ "width": "50%", "marginRight": "50%" }).appendTo( container ),
647+
el2 = jQuery("<div/>").css({ "width": "50%", "minWidth": "300px", "marginLeft": "25%" }).appendTo( container );
648+
649+
equal( el.css("marginRight"), "200px", "css('marginRight') returning % instead of px, see #10639" );
650+
equal( el2.css("marginLeft"), "100px", "css('marginLeft') returning incorrect pixel value, see #12088" );
651+
});
652+
644653
test("jQuery.cssProps behavior, (bug #8402)", function() {
645654
var div = jQuery( "<div>" ).appendTo(document.body).css({
646655
"position": "absolute",

test/unit/dimensions.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,6 @@ test("getting dimensions shouldnt modify runtimeStyle see #9233", function() {
292292
$div.remove();
293293
});
294294

295-
test("outerWidth(true) returning % instead of px in Webkit, see #10639", function() {
296-
var container = jQuery( "<div/>" ).width(400).appendTo( "#qunit-fixture" ),
297-
el = jQuery( "<div/>" ).css({ "width": "50%", "marginRight": "50%" }).appendTo( container );
298-
299-
equal( el.outerWidth(true), 400, "outerWidth(true) and css('margin') returning % instead of px in Webkit, see #10639" );
300-
});
301-
302295
test( "getting dimensions of zero width/height table elements shouldn't alter dimensions", function() {
303296
expect( 1 );
304297

0 commit comments

Comments
 (0)