Improve relative string performance in .css and some code cleanup

This commit is contained in:
Dan Heberden 2011-04-04 16:48:24 -07:00
parent 123dd72e80
commit 44a3b5839e
2 changed files with 37 additions and 25 deletions

View file

@ -7,8 +7,8 @@ var ralpha = /alpha\([^)]*\)/i,
rupper = /([A-Z]|^ms)/g, rupper = /([A-Z]|^ms)/g,
rnumpx = /^-?\d+(?:px)?$/i, rnumpx = /^-?\d+(?:px)?$/i,
rnum = /^-?\d/, rnum = /^-?\d/,
rrelNum = /=/, rrelNum = /^[+\-]=/,
rrelString = /[^+\-\de]+/g, rrelNumFilter = /[^+\-\.\de]+/g,
cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssShow = { position: "absolute", visibility: "hidden", display: "block" },
cssWidth = [ "Left", "Right" ], cssWidth = [ "Left", "Right" ],
@ -77,26 +77,27 @@ jQuery.extend({
} }
// Make sure that we're working with the right name // 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 ]; style = elem.style, hooks = jQuery.cssHooks[ origName ];
name = jQuery.cssProps[ origName ] || origName; name = jQuery.cssProps[ origName ] || origName;
// Check if we're setting a value // Check if we're setting a value
if ( value !== undefined ) { if ( value !== undefined ) {
type = typeof value;
// Make sure that NaN and null values aren't set. See: #7116 // 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; return;
} }
// convert string to number or relative number // convert relative number strings (+= or -=) to relative numbers. #7345
if ( type === "string" ) { if ( type === "string" && rrelNum.test( value ) ) {
parsed = +value.replace( rrelString, '' ); value = +value.replace( rrelNumFilter, '' ) + parseFloat( jQuery.css( elem, name ) );
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 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"; value += "px";
} }

View file

@ -106,24 +106,35 @@ test("css(String|Hash)", function() {
}); });
test("css() explicit and relative values", function() { test("css() explicit and relative values", function() {
expect(9);
var $elem = jQuery('#nothiddendiv'); var $elem = jQuery('#nothiddendiv');
$elem.css({ width: 1, height: 1 }); $elem.css({ width: 1, height: 1 });
ok( [$elem.width(), $elem.height()], [1,1] ); equals( $elem.width(), 1, "Initial css set or width/height works (hash)" );
$elem.css({ width: "+=9", height: "+=9" });
ok( [$elem.width(), $elem.height()], [10,10] ); $elem.css({ width: "+=9" });
$elem.css({ width: "-=9", height: "-=9" }); equals( $elem.width(), 10, "'+=9' on width (hash)" );
ok( [$elem.width(), $elem.height()], [1,1] );
$elem.css({ width: "+=9px", height: "+=9px" }); $elem.css({ width: "-=9" });
ok( [$elem.width(), $elem.height()], [10,10] ); equals( $elem.width(), 1, "'-=9' on width (hash)" );
$elem.css({ width: "-=9px", height: "-=9px" });
ok( [$elem.width(), $elem.height()], [1,1] ); $elem.css({ width: "+=9px" });
$elem.css("width", "+=9").css("height", "+=9"); equals( $elem.width(), 10, "'+=9px' on width (hash)" );
ok( [$elem.width(), $elem.height()], [10,10] );
$elem.css("width", "+9").css("height", "+9"); $elem.css({ width: "-=9px" });
ok( [$elem.width(), $elem.height()], [9,9] ); equals( $elem.width(), 1, "'-=9px' on width (hash)" );
$elem.css("width", "-9").css("height", "-9");
ok( [$elem.width(), $elem.height()], [0,0] ); $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() { test("css(String, Object)", function() {