Skip to content

Commit

Permalink
Merge branch 'setterargs'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Jan 7, 2010
2 parents a9665bb + 0e5370b commit d6d5ed4
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 263 deletions.
127 changes: 63 additions & 64 deletions src/attributes.js
Expand Up @@ -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")) );
});
}

Expand Down Expand Up @@ -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")) );
});
}

Expand Down Expand Up @@ -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++ ) {
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Expand Up @@ -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;
Expand Down
26 changes: 18 additions & 8 deletions src/manipulation.js
Expand Up @@ -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()) );
});
}

Expand All @@ -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) );
});
}

Expand Down Expand Up @@ -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;

Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 );
});
}

Expand Down
25 changes: 15 additions & 10 deletions src/offset.js
Expand Up @@ -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 );
});
}

Expand All @@ -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 );
});
}

Expand Down Expand Up @@ -137,19 +137,24 @@ 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";
}
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 );
Expand Down

0 comments on commit d6d5ed4

Please sign in to comment.