diff options
author | timmywil <tim.willison@thisismedium.com> | 2011-06-06 23:35:16 -0400 |
---|---|---|
committer | timmywil <tim.willison@thisismedium.com> | 2011-06-06 23:35:16 -0400 |
commit | 75203de7435b7f32c69da1dde88aa6708bf6bb7d (patch) | |
tree | af3ca2da288b538472cee948a3e45f2d2f809a0a /src/css.js | |
parent | 80ad14bd14467c547c2867f2677ca581aa29bf33 (diff) | |
download | jquery-75203de7435b7f32c69da1dde88aa6708bf6bb7d.tar.gz jquery-75203de7435b7f32c69da1dde88aa6708bf6bb7d.zip |
Optimize width/height retrieval (moved logic to getWH, removed adjustWH). Supplements #9441, #9300.
Diffstat (limited to 'src/css.js')
-rw-r--r-- | src/css.js | 101 |
1 files changed, 39 insertions, 62 deletions
diff --git a/src/css.js b/src/css.js index 47e6f31e7..cc249ce79 100644 --- a/src/css.js +++ b/src/css.js @@ -172,51 +172,13 @@ jQuery.each(["height", "width"], function( i, name ) { if ( computed ) { if ( elem.offsetWidth !== 0 ) { - val = getWH( elem, name, extra ); + return getWH( elem, name, extra ); } else { jQuery.swap( elem, cssShow, function() { val = getWH( elem, name, extra ); }); } - if ( val <= 0 ) { - val = curCSS( elem, name, name ); - - if ( val === "0px" && currentStyle ) { - val = currentStyle( elem, name, name ); - } - - if ( val != null ) { - fellback = true; - } - } - - if ( !fellback && ( val < 0 || val == null ) ) { - val = elem.style[ name ]; - 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 ) { - 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; } }, @@ -224,7 +186,7 @@ jQuery.each(["height", "width"], function( i, name ) { set: function( elem, value ) { if ( rnumpx.test( value ) ) { // ignore negative width and height values #1599 - value = parseFloat(value); + value = parseFloat( value ); if ( value >= 0 ) { return value + "px"; @@ -347,36 +309,51 @@ if ( document.documentElement.currentStyle ) { curCSS = getComputedStyle || currentStyle; function getWH( elem, name, extra ) { - var val = name === "width" ? elem.offsetWidth : elem.offsetHeight; - if ( extra === "border" ) { - return val; - } + // Start with offset property + var val = name === "width" ? elem.offsetWidth : elem.offsetHeight, + which = name === "width" ? cssWidth : cssHeight; - if ( !extra ) { - val -= adjustWH( elem, name, "padding"); + if ( extra !== "margin" && extra !== "border" ) { + jQuery.each( which, function() { + val -= parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0; + if ( !extra ) { + val -= parseFloat( jQuery.css( elem, "padding" + this ) ) || 0; + } + }); } - if ( extra !== "margin" ) { - val -= adjustWH( elem, name, "border", "Width"); + if ( val > 0 ) { + if ( extra === "margin" ) { + jQuery.each( which, function() { + val += parseFloat( jQuery.css( elem, extra + this ) ) || 0; + }); + } + return val + "px"; } - return val; -} - -function adjustWH( elem, name, prepend, append ) { - var which = name === "width" ? cssWidth : cssHeight, - val = 0; - - if( !append ){ - append = ""; + // Fall back to computed then uncomputed css if necessary + val = curCSS( elem, name, name ); + if ( val < 0 || val == null ) { + val = elem.style[ name ] || 0; + } + // Normalize "", auto, and prepare for extra + val = parseFloat( val ) || 0; + + // Add padding, border, margin + if ( extra ) { + jQuery.each( which, function() { + val += parseFloat( jQuery.css( elem, "padding" + this ) ) || 0; + if ( extra !== "padding" ) { + val += parseFloat( jQuery.css( elem, "border" + this + "Width" ) ) || 0; + } + if ( extra === "margin" ) { + val += parseFloat( jQuery.css( elem, extra + this ) ) || 0; + } + }); } - jQuery.each( which, function() { - val += parseFloat( jQuery.css( elem, prepend + this + append ) ) || 0; - }); - - return val; + return val + "px"; } if ( jQuery.expr && jQuery.expr.filters ) { |