diff options
author | jeresig <jeresig@gmail.com> | 2010-01-07 14:26:06 -0500 |
---|---|---|
committer | jeresig <jeresig@gmail.com> | 2010-01-07 14:26:06 -0500 |
commit | d6d5ed4c683ceee4d3c64ce75e7349dde8205d50 (patch) | |
tree | e377719ab472a02cd24a6db629e83fbd25744ad9 /src | |
parent | a9665bbe9bb50b060b3f8f2f5facf2e1d1115f37 (diff) | |
parent | 0e5370b89c0dfe56bf2f02fdd34fd820ecd48254 (diff) | |
download | jquery-d6d5ed4c683ceee4d3c64ce75e7349dde8205d50.tar.gz jquery-d6d5ed4c683ceee4d3c64ce75e7349dde8205d50.zip |
Merge branch 'setterargs'
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes.js | 127 | ||||
-rw-r--r-- | src/core.js | 2 | ||||
-rw-r--r-- | src/manipulation.js | 26 | ||||
-rw-r--r-- | src/offset.js | 25 |
4 files changed, 97 insertions, 83 deletions
diff --git a/src/attributes.js b/src/attributes.js index a2108feb8..004c6b30c 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -12,10 +12,20 @@ jQuery.fn.extend({ return access( this, name, value, true, jQuery.attr ); }, + removeAttr: function( name, fn ) { + return this.each(function(){ + jQuery.attr( this, name, "" ); + if ( this.nodeType === 1 ) { + this.removeAttribute( name ); + } + }); + }, + addClass: function( value ) { if ( jQuery.isFunction(value) ) { - return this.each(function() { - jQuery(this).addClass( value.call(this) ); + return this.each(function(i) { + var self = jQuery(this); + self.addClass( value.call(this, i, self.attr("class")) ); }); } @@ -46,8 +56,9 @@ jQuery.fn.extend({ removeClass: function( value ) { if ( jQuery.isFunction(value) ) { - return this.each(function() { - jQuery(this).removeClass( value.call(this) ); + return this.each(function(i) { + var self = jQuery(this); + self.removeClass( value.call(this, i, self.attr("class")) ); }); } @@ -75,6 +86,41 @@ jQuery.fn.extend({ return this; }, + toggleClass: function( value, stateVal ) { + var type = typeof value, isBool = typeof stateVal === "boolean"; + + if ( jQuery.isFunction( value ) ) { + return this.each(function(i) { + var self = jQuery(this); + self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal ); + }); + } + + return this.each(function() { + if ( type === "string" ) { + // toggle individual class names + var className, i = 0, self = jQuery(this), + state = stateVal, + classNames = value.split( rspace ); + + while ( (className = classNames[ i++ ]) ) { + // check each className given, space seperated list + state = isBool ? state : !self.hasClass( className ); + self[ state ? "addClass" : "removeClass" ]( className ); + } + + } else if ( type === "undefined" || type === "boolean" ) { + if ( this.className ) { + // store className if set + jQuery.data( this, "__className__", this.className ); + } + + // toggle whole className + this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || ""; + } + }); + }, + hasClass: function( selector ) { var className = " " + selector + " "; for ( var i = 0, l = this.length; i < l; i++ ) { @@ -142,30 +188,27 @@ jQuery.fn.extend({ return undefined; } - // Typecast once if the value is a number - if ( typeof value === "number" ) { - value += ""; - } + var isFunction = jQuery.isFunction(value); - var val = value; + return this.each(function(i) { + var self = jQuery(this), val = value; - return this.each(function() { - if ( jQuery.isFunction(value) ) { - val = value.call(this); + if ( this.nodeType !== 1 ) { + return; + } - // Typecast each time if the value is a Function and the appended - // value is therefore different each time. - if ( typeof val === "number" ) { - val += ""; - } + if ( isFunction ) { + val = value.call(this, i, self.val()); } - if ( this.nodeType !== 1 ) { - return; + // Typecast each time if the value is a Function and the appended + // value is therefore different each time. + if ( typeof val === "number" ) { + val += ""; } if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) { - this.checked = jQuery.inArray( jQuery(this).val(), val ) >= 0; + this.checked = jQuery.inArray( self.val(), val ) >= 0; } else if ( jQuery.nodeName( this, "select" ) ) { var values = jQuery.makeArray(val); @@ -185,50 +228,6 @@ jQuery.fn.extend({ } }); -jQuery.each({ - removeAttr: function( name ) { - jQuery.attr( this, name, "" ); - if ( this.nodeType === 1 ) { - this.removeAttribute( name ); - } - }, - - toggleClass: function( classNames, state ) { - var type = typeof classNames; - - if ( type === "string" ) { - // toggle individual class names - var isBool = typeof state === "boolean", className, i = 0, - classNames = classNames.split( rspace ); - - while ( (className = classNames[ i++ ]) ) { - // check each className given, space seperated list - state = isBool ? state : !jQuery(this).hasClass( className ); - jQuery(this)[ state ? "addClass" : "removeClass" ]( className ); - } - - } else if ( type === "undefined" || type === "boolean" ) { - if ( this.className ) { - // store className if set - jQuery.data( this, "__className__", this.className ); - } - - // toggle whole className - this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || ""; - } - } -}, function( name, fn ) { - jQuery.fn[ name ] = function( val, state ) { - if ( jQuery.isFunction( val ) ) { - return this.each(function() { - jQuery(this)[ name ]( val.call(this), state ); - }); - } - - return this.each( fn, arguments ); - }; -}); - jQuery.extend({ attrFn: { val: true, diff --git a/src/core.js b/src/core.js index 58cbbc7e5..831570ed3 100644 --- a/src/core.js +++ b/src/core.js @@ -775,7 +775,7 @@ function access( elems, key, value, exec, fn, pass ) { exec = exec && jQuery.isFunction(value); for ( var i = 0; i < length; i++ ) { - fn( elems[i], key, exec ? value.call( elems[i], i ) : value, pass ); + fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass ); } return elems; diff --git a/src/manipulation.js b/src/manipulation.js index 2af2d7e7d..9ed22baaf 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -33,8 +33,9 @@ if ( !jQuery.support.htmlSerialize ) { jQuery.fn.extend({ text: function( text ) { if ( jQuery.isFunction(text) ) { - return this.each(function() { - return jQuery(this).text( text.call(this) ); + return this.each(function(i) { + var self = jQuery(this); + return self.text( text.call(this, i, self.text()) ); }); } @@ -47,8 +48,8 @@ jQuery.fn.extend({ wrapAll: function( html ) { if ( jQuery.isFunction( html ) ) { - return this.each(function() { - jQuery(this).wrapAll( html.apply(this, arguments) ); + return this.each(function(i) { + jQuery(this).wrapAll( html.call(this, i) ); }); } @@ -172,7 +173,7 @@ jQuery.fn.extend({ html: function( value ) { if ( value === undefined ) { - return this[0] ? + return this[0] && this[0].nodeType === 1 ? this[0].innerHTML.replace(rinlinejQuery, "") : null; @@ -195,6 +196,14 @@ jQuery.fn.extend({ this.empty().append( value ); } + } else if ( jQuery.isFunction( value ) ) { + this.each(function(i){ + var self = jQuery(this), old = self.html(); + self.empty().append(function(){ + return value.call( this, i, old ); + }); + }); + } else { this.empty().append( value ); } @@ -228,9 +237,10 @@ jQuery.fn.extend({ var results, first, value = args[0], scripts = []; if ( jQuery.isFunction(value) ) { - return this.each(function() { - args[0] = value.call(this); - return jQuery(this).domManip( args, table, callback ); + return this.each(function(i) { + var self = jQuery(this); + args[0] = value.call(this, i, table ? self.html() : undefined); + return self.domManip( args, table, callback ); }); } diff --git a/src/offset.js b/src/offset.js index d12921dff..c3183743b 100644 --- a/src/offset.js +++ b/src/offset.js @@ -7,8 +7,8 @@ if ( "getBoundingClientRect" in document.documentElement ) { } if ( options ) { - return this.each(function() { - jQuery.offset.setOffset( this, options ); + return this.each(function( i ) { + jQuery.offset.setOffset( this, options, i ); }); } @@ -33,8 +33,8 @@ if ( "getBoundingClientRect" in document.documentElement ) { } if ( options ) { - return this.each(function() { - jQuery.offset.setOffset( this, options ); + return this.each(function( i ) { + jQuery.offset.setOffset( this, options, i ); }); } @@ -137,7 +137,7 @@ jQuery.offset = { return { top: top, left: left }; }, - setOffset: function( elem, options ) { + setOffset: function( elem, options, i ) { // set position first, in-case top/left are set even on static elem if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) { elem.style.position = "relative"; @@ -145,11 +145,16 @@ jQuery.offset = { var curElem = jQuery( elem ), curOffset = curElem.offset(), curTop = parseInt( jQuery.curCSS( elem, "top", true ), 10 ) || 0, - curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10) || 0, - props = { - top: (options.top - curOffset.top) + curTop, - left: (options.left - curOffset.left) + curLeft - }; + curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0; + + if ( jQuery.isFunction( options ) ) { + options = options.call( elem, i, curOffset ); + } + + var props = { + top: (options.top - curOffset.top) + curTop, + left: (options.left - curOffset.left) + curLeft + }; if ( "using" in options ) { options.using.call( elem, props ); |