From 015328787cd2e52efbc77d1254ebcac13cc4ac53 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 16 Aug 2011 18:00:44 -0400 Subject: [PATCH] Fixes #10021. Allow negative relative values for `.css()` (e.g., `"+=-20px"`) since `.animate()` already allows it. Useful for when the relative value is a variable. --- src/css.js | 17 ++++++++--------- test/unit/css.js | 8 +++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/css.js b/src/css.js index efd881653..da0dd20c8 100644 --- a/src/css.js +++ b/src/css.js @@ -6,8 +6,7 @@ var ralpha = /alpha\([^)]*\)/i, rupper = /([A-Z]|^ms)/g, rnumpx = /^-?\d+(?:px)?$/i, rnum = /^-?\d/, - rrelNum = /^[+\-]=/, - rrelNumFilter = /[^+\-\.\de]+/g, + rrelNum = /^([+\-])=([+\-\.\de]+)/, cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssWidth = [ "Left", "Right" ], @@ -84,18 +83,18 @@ jQuery.extend({ if ( value !== undefined ) { type = typeof value; - // Make sure that NaN and null values aren't set. See: #7116 - if ( type === "number" && isNaN( value ) || value == null ) { - return; - } - // convert relative number strings (+= or -=) to relative numbers. #7345 - if ( type === "string" && rrelNum.test( value ) ) { - value = +value.replace( rrelNumFilter, "" ) + parseFloat( jQuery.css( elem, name ) ); + if ( type === "string" && (ret = rrelNum.exec( value )) ) { + value = (ret[1] === "+"? +ret[2] : -ret[2]) + parseFloat( jQuery.css( elem, name ) ); // Fixes bug #9237 type = "number"; } + // Make sure that NaN and null values aren't set. See: #7116 + if ( type === "number" && isNaN( value ) || value == null ) { + return; + } + // If a number was passed in, add 'px' to the (except for certain CSS properties) if ( type === "number" && !jQuery.cssNumber[ origName ] ) { value += "px"; diff --git a/test/unit/css.js b/test/unit/css.js index c01acdd68..acad497eb 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -109,7 +109,7 @@ test("css(String|Hash)", function() { }); test("css() explicit and relative values", function() { - expect(27); + expect(29); var $elem = jQuery("#nothiddendiv"); $elem.css({ width: 1, height: 1, paddingLeft: "1px", opacity: 1 }); @@ -141,6 +141,12 @@ test("css() explicit and relative values", function() { $elem.css( "width", "-=9px" ); equals( $elem.width(), 1, "'-=9px' on width (params)" ); + $elem.css( "width", "-=-9px" ); + equals( $elem.width(), 10, "'-=-9px' on width (params)" ); + + $elem.css( "width", "+=-9px" ); + equals( $elem.width(), 1, "'+=-9px' on width (params)" ); + $elem.css({ paddingLeft: "+=4" }); equals( $elem.css("paddingLeft"), "5px", "'+=4' on paddingLeft (hash)" ); -- 2.39.5