Permalink
Browse files

applying margin addition AFTER width without it is determined. Fixes …

…#9300
  • Loading branch information...
mikesherov committed May 27, 2011
1 parent 48b01d3 commit a365e90393f9c82d18b6f8824d772a185b8e248f
Showing with 38 additions and 21 deletions.
  1. +38 −21 src/css.js
View
@@ -168,12 +168,11 @@ jQuery.curCSS = jQuery.css;
jQuery.each(["height", "width"], function( i, name ) {
jQuery.cssHooks[ name ] = {
get: function( elem, computed, extra ) {
- var val;
+ var val, fellback = false;
if ( computed ) {
if ( elem.offsetWidth !== 0 ) {
val = getWH( elem, name, extra );
-
} else {
jQuery.swap( elem, cssShow, function() {
val = getWH( elem, name, extra );
@@ -188,20 +187,29 @@ jQuery.each(["height", "width"], function( i, name ) {
}
if ( val != null ) {
- // Should return "auto" instead of 0, use 0 for
- // temporary backwards-compat
- return val === "" || val === "auto" ? "0px" : val;
+ fellback = true;
}
}
- if ( val < 0 || val == null ) {
+ if ( !fellback && ( val < 0 || val == null ) ) {
val = elem.style[ name ];
- // Should return "auto" instead of 0, use 0 for
- // temporary backwards-compat
- return val === "" || val === "auto" ? "0px" : val;
+ fellback = true;
+ }
+
+ // Should return "auto" instead of 0, use 0 for
+ // temporary backwards-compat
+ if ( fellback && ( val === "" || val === "auto" ) ) {
+ val = "0px";
+ } else if ( typeof val !== "string" ) {
+ val += "px";
+ }
+
+ if ( extra === "margin" ) {
+ val = parseFloat( val ) || 0;
+ val += adjustWH( elem, name, "margin" );
}
- return typeof val === "string" ? val : val + "px";
+ return val;
}
},
@@ -331,24 +339,33 @@ if ( document.documentElement.currentStyle ) {
curCSS = getComputedStyle || currentStyle;
function getWH( elem, name, extra ) {
- var which = name === "width" ? cssWidth : cssHeight,
- val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
+ var val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
if ( extra === "border" ) {
return val;
}
- jQuery.each( which, function() {
- if ( !extra ) {
- val -= parseFloat(jQuery.css( elem, "padding" + this )) || 0;
- }
+ if ( !extra ) {
+ val -= adjustWH( elem, name, "padding");
+ }
+
+ if ( extra !== "margin" ) {
+ val -= adjustWH( elem, name, "border", "Width");
+ }
- if ( extra === "margin" ) {
- val += parseFloat(jQuery.css( elem, "margin" + this )) || 0;
+ return val;
+}
- } else {
- val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0;
- }
+function adjustWH( elem, name, prepend, append ) {
+ var which = name === "width" ? cssWidth : cssHeight,
+ val = 0;
+
+ if( !append ){
+ append = "";
+ }
+
+ jQuery.each( which, function() {
+ val += parseFloat( jQuery.css( elem, prepend + this + append ) ) || 0;
});
return val;

0 comments on commit a365e90

Please sign in to comment.