From d1285504fb636a077ae5b1e53e35959cc7faebc7 Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Wed, 9 Sep 2009 21:14:28 +0000 Subject: [PATCH] jquery core: Closes #5189. Added a generic function to handle getting/setting key-value/setting a hash. --- src/attributes.js | 30 +------------------------ src/core.js | 24 ++++++++++++++++++++ src/css.js | 50 +++++++---------------------------------- test/unit/attributes.js | 3 +++ 4 files changed, 36 insertions(+), 71 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index c4c750092..6aecf085c 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -1,34 +1,6 @@ jQuery.fn.extend({ attr: function( name, value ) { - var elem, options, isFunction = jQuery.isFunction(value); - - if ( typeof name === "string" ) { // A single attribute - if ( value === undefined ) { // Query it on first element - return this.length ? - jQuery.attr( this[0], name ) : - null; - } else { // Set it on all elements - for ( var i = 0, l = this.length; i < l; i++ ) { - elem = this[i]; - if ( isFunction ) - value = value.call(elem,i); - jQuery.attr( elem, name, value ); - } - } - } else { // Multiple attributes to set on all - options = name; - for ( var i = 0, l = this.length; i < l; i++ ) { - elem = this[i]; - for ( name in options ) { - value = options[name]; - if ( jQuery.isFunction(value) ) - value = value.call(elem,i); - jQuery.attr( elem, name, value ); - } - } - } - - return this; + return access(this, name, value, true, jQuery.attr); }, addClass: function( value ) { diff --git a/src/core.js b/src/core.js index d2f82a6db..b1dde53a0 100644 --- a/src/core.js +++ b/src/core.js @@ -553,6 +553,30 @@ function evalScript( i, elem ) { } } +function access( elems, key, value, exec, fn ) { + var l = elems.length; + + if ( typeof key === "object" ) { + for (var k in key) { + access(elems, k, key[k], exec, fn); + } + return elems; + } + + if (value !== undefined) { + exec = exec && jQuery.isFunction(value); + + for (var i = 0; i < l; i++) { + var elem = elems[i], + val = exec ? value.call(elem, i) : value; + fn(elem, key, val); + } + return elems; + } + + return l ? fn(elems[0], key) : null; +} + function now() { return (new Date).getTime(); } diff --git a/src/css.js b/src/css.js index 5dd8dd062..f47bc8e2b 100644 --- a/src/css.js +++ b/src/css.js @@ -17,51 +17,17 @@ var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, }; jQuery.fn.css = function( name, value ) { - var options = name, isFunction = jQuery.isFunction( value ); - - if ( typeof name === "string" ) { - // Are we setting the style? - if ( value === undefined ) { - return this.length ? - jQuery.css( this[0], name ) : - null; - - // Convert name, value params to options hash format - } else { - options = {}; - options[ name ] = value; + return access( this, name, value, true, function( elem, name, value ) { + if (value === undefined) { + return jQuery.css( elem, name ); } - } - - var isFunction = {}; - - // For each value, determine whether it's a Function so we don't - // need to determine it again for each element - for ( var prop in options ) { - isFunction[prop] = jQuery.isFunction( options[prop] ); - } - - // For each element... - for ( var i = 0, l = this.length; i < l; i++ ) { - var elem = this[i]; - - // Set all the styles - for ( var prop in options ) { - value = options[prop]; - - if ( isFunction[prop] ) { - value = value.call( elem, i ); - } - - if ( typeof value === "number" && !rexclude.test(prop) ) { - value = value + "px"; - } - - jQuery.style( elem, prop, value ); + + if ( typeof value === "number" && !rexclude.test(name) ) { + value += "px"; } - } - return this; + jQuery.style( elem, name, value ); + }); }; jQuery.extend({ diff --git a/test/unit/attributes.js b/test/unit/attributes.js index e9880df27..9b5411f08 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -2,7 +2,10 @@ module("attributes"); test("attr(String)", function() { expect(27); + + // This one sometimes fails randomally ?! equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' ); + equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' ); equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );