Skip to content

Commit 0153287

Browse files
committed
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.
1 parent b22c904 commit 0153287

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/css.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ var ralpha = /alpha\([^)]*\)/i,
66
rupper = /([A-Z]|^ms)/g,
77
rnumpx = /^-?\d+(?:px)?$/i,
88
rnum = /^-?\d/,
9-
rrelNum = /^[+\-]=/,
10-
rrelNumFilter = /[^+\-\.\de]+/g,
9+
rrelNum = /^([+\-])=([+\-\.\de]+)/,
1110

1211
cssShow = { position: "absolute", visibility: "hidden", display: "block" },
1312
cssWidth = [ "Left", "Right" ],
@@ -84,18 +83,18 @@ jQuery.extend({
8483
if ( value !== undefined ) {
8584
type = typeof value;
8685

87-
// Make sure that NaN and null values aren't set. See: #7116
88-
if ( type === "number" && isNaN( value ) || value == null ) {
89-
return;
90-
}
91-
9286
// convert relative number strings (+= or -=) to relative numbers. #7345
93-
if ( type === "string" && rrelNum.test( value ) ) {
94-
value = +value.replace( rrelNumFilter, "" ) + parseFloat( jQuery.css( elem, name ) );
87+
if ( type === "string" && (ret = rrelNum.exec( value )) ) {
88+
value = (ret[1] === "+"? +ret[2] : -ret[2]) + parseFloat( jQuery.css( elem, name ) );
9589
// Fixes bug #9237
9690
type = "number";
9791
}
9892

93+
// Make sure that NaN and null values aren't set. See: #7116
94+
if ( type === "number" && isNaN( value ) || value == null ) {
95+
return;
96+
}
97+
9998
// If a number was passed in, add 'px' to the (except for certain CSS properties)
10099
if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
101100
value += "px";

test/unit/css.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ test("css(String|Hash)", function() {
109109
});
110110

111111
test("css() explicit and relative values", function() {
112-
expect(27);
112+
expect(29);
113113
var $elem = jQuery("#nothiddendiv");
114114

115115
$elem.css({ width: 1, height: 1, paddingLeft: "1px", opacity: 1 });
@@ -141,6 +141,12 @@ test("css() explicit and relative values", function() {
141141
$elem.css( "width", "-=9px" );
142142
equals( $elem.width(), 1, "'-=9px' on width (params)" );
143143

144+
$elem.css( "width", "-=-9px" );
145+
equals( $elem.width(), 10, "'-=-9px' on width (params)" );
146+
147+
$elem.css( "width", "+=-9px" );
148+
equals( $elem.width(), 1, "'+=-9px' on width (params)" );
149+
144150
$elem.css({ paddingLeft: "+=4" });
145151
equals( $elem.css("paddingLeft"), "5px", "'+=4' on paddingLeft (hash)" );
146152

0 commit comments

Comments
 (0)