aboutsummaryrefslogtreecommitdiffstats
path: root/src/css.js
diff options
context:
space:
mode:
authorMike Sherov <mike.sherov@gmail.com>2011-06-06 23:13:37 -0400
committertimmywil <tim.willison@thisismedium.com>2011-06-06 23:13:37 -0400
commit80ad14bd14467c547c2867f2677ca581aa29bf33 (patch)
treec234ebc62604a1895706c80c7ddfad4b06c66b0c /src/css.js
parent07420566452622f37b01e69bbbdcbeeb5317e065 (diff)
downloadjquery-80ad14bd14467c547c2867f2677ca581aa29bf33.tar.gz
jquery-80ad14bd14467c547c2867f2677ca581aa29bf33.zip
Add margin after checking width. Add tests. Fixes #9441. Fixes #9300.
Diffstat (limited to 'src/css.js')
-rw-r--r--src/css.js67
1 files changed, 46 insertions, 21 deletions
diff --git a/src/css.js b/src/css.js
index e3ab270fc..47e6f31e7 100644
--- a/src/css.js
+++ b/src/css.js
@@ -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,37 @@ 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";
}
- return typeof val === "string" ? val : val + "px";
+ if ( extra ) {
+ val = parseFloat( val ) || 0;
+ if ( fellback ) {
+ val += adjustWH( elem, name, "padding" );
+ if ( extra !== "padding" ) {
+ val += adjustWH( elem, name, "border", "Width" );
+ }
+ }
+ if ( extra === "margin" ) {
+ val += adjustWH( elem, name, "margin" );
+ }
+ }
+
+ return val;
}
},
@@ -331,24 +347,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 += parseFloat(jQuery.css( elem, "margin" + this )) || 0;
+ if ( extra !== "margin" ) {
+ val -= adjustWH( elem, name, "border", "Width");
+ }
- } else {
- val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0;
- }
+ return val;
+}
+
+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;