From 123dd72e808800d670769f9bca75c6f2c866326b Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 4 Apr 2011 11:21:15 -0700 Subject: [PATCH] Bug 7345; Add support for explicit/relative string values in .css - modified from original pull req by brandonaron #78 --- src/css.js | 10 +++++++++- test/unit/css.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/css.js b/src/css.js index 17ac136bb..d23549fa4 100644 --- a/src/css.js +++ b/src/css.js @@ -7,6 +7,8 @@ var ralpha = /alpha\([^)]*\)/i, rupper = /([A-Z]|^ms)/g, rnumpx = /^-?\d+(?:px)?$/i, rnum = /^-?\d/, + rrelNum = /=/, + rrelString = /[^+\-\de]+/g, cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssWidth = [ "Left", "Right" ], @@ -75,7 +77,7 @@ jQuery.extend({ } // Make sure that we're working with the right name - var ret, origName = jQuery.camelCase( name ), + var ret, parsed, type, origName = jQuery.camelCase( name ), style = elem.style, hooks = jQuery.cssHooks[ origName ]; name = jQuery.cssProps[ origName ] || origName; @@ -87,6 +89,12 @@ jQuery.extend({ return; } + // convert string to number or relative number + if ( type === "string" ) { + parsed = +value.replace( rrelString, '' ); + value = rrelNum.test( value ) ? parsed + jQuery.css( elem, name ) : parsed; + } + // If a number was passed in, add 'px' to the (except for certain CSS properties) if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) { value += "px"; diff --git a/test/unit/css.js b/test/unit/css.js index 8ae8fcb34..c4724fcd0 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -105,6 +105,27 @@ test("css(String|Hash)", function() { equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on null." ); }); +test("css() explicit and relative values", function() { + var $elem = jQuery('#nothiddendiv'); + + $elem.css({ width: 1, height: 1 }); + ok( [$elem.width(), $elem.height()], [1,1] ); + $elem.css({ width: "+=9", height: "+=9" }); + ok( [$elem.width(), $elem.height()], [10,10] ); + $elem.css({ width: "-=9", height: "-=9" }); + ok( [$elem.width(), $elem.height()], [1,1] ); + $elem.css({ width: "+=9px", height: "+=9px" }); + ok( [$elem.width(), $elem.height()], [10,10] ); + $elem.css({ width: "-=9px", height: "-=9px" }); + ok( [$elem.width(), $elem.height()], [1,1] ); + $elem.css("width", "+=9").css("height", "+=9"); + ok( [$elem.width(), $elem.height()], [10,10] ); + $elem.css("width", "+9").css("height", "+9"); + ok( [$elem.width(), $elem.height()], [9,9] ); + $elem.css("width", "-9").css("height", "-9"); + ok( [$elem.width(), $elem.height()], [0,0] ); +}); + test("css(String, Object)", function() { expect(22); -- 2.39.5