From: Dan Heberden Date: Mon, 4 Apr 2011 23:48:24 +0000 (-0700) Subject: Improve relative string performance in .css and some code cleanup X-Git-Tag: 1.6b1~55^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=44a3b5839e6db9e3735b3d2d3035c6f79b3bcbe8;p=jquery.git Improve relative string performance in .css and some code cleanup --- diff --git a/src/css.js b/src/css.js index d23549fa4..65ec20f57 100644 --- a/src/css.js +++ b/src/css.js @@ -7,8 +7,8 @@ var ralpha = /alpha\([^)]*\)/i, rupper = /([A-Z]|^ms)/g, rnumpx = /^-?\d+(?:px)?$/i, rnum = /^-?\d/, - rrelNum = /=/, - rrelString = /[^+\-\de]+/g, + rrelNum = /^[+\-]=/, + rrelNumFilter = /[^+\-\.\de]+/g, cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssWidth = [ "Left", "Right" ], @@ -77,26 +77,27 @@ jQuery.extend({ } // Make sure that we're working with the right name - var ret, parsed, type, origName = jQuery.camelCase( name ), + var ret, type, origName = jQuery.camelCase( name ), style = elem.style, hooks = jQuery.cssHooks[ origName ]; name = jQuery.cssProps[ origName ] || origName; // Check if we're setting a value if ( value !== undefined ) { + type = typeof value; + // Make sure that NaN and null values aren't set. See: #7116 - if ( typeof value === "number" && isNaN( value ) || value == null ) { + if ( type === "number" && isNaN( value ) || value == null ) { 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; + // 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 a number was passed in, add 'px' to the (except for certain CSS properties) - if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) { + if ( type === "number" && !jQuery.cssNumber[ origName ] ) { value += "px"; } diff --git a/test/unit/css.js b/test/unit/css.js index c4724fcd0..08f50ef25 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -106,24 +106,35 @@ test("css(String|Hash)", function() { }); test("css() explicit and relative values", function() { + expect(9); 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] ); + equals( $elem.width(), 1, "Initial css set or width/height works (hash)" ); + + $elem.css({ width: "+=9" }); + equals( $elem.width(), 10, "'+=9' on width (hash)" ); + + $elem.css({ width: "-=9" }); + equals( $elem.width(), 1, "'-=9' on width (hash)" ); + + $elem.css({ width: "+=9px" }); + equals( $elem.width(), 10, "'+=9px' on width (hash)" ); + + $elem.css({ width: "-=9px" }); + equals( $elem.width(), 1, "'-=9px' on width (hash)" ); + + $elem.css( "width", "+=9" ); + equals( $elem.width(), 10, "'+=9' on width (params)" ); + + $elem.css( "width", "-=9" ) ; + equals( $elem.width(), 1, "'-=9' 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)" ); }); test("css(String, Object)", function() {