Skip to content

Commit 600d314

Browse files
committed
A first pass at making sure that all the setter function arguments receive the index of the element and a relevant value to work with. Fixes #5763.
1 parent 84dd82e commit 600d314

File tree

3 files changed

+72
-60
lines changed

3 files changed

+72
-60
lines changed

src/attributes.js

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@ jQuery.fn.extend({
1212
return access( this, name, value, true, jQuery.attr );
1313
},
1414

15+
removeAttr: function( name ) {
16+
if ( jQuery.isFunction( name ) ) {
17+
return this.each(function(i) {
18+
var self = jQuery(this);
19+
self.removeAttr( name.call(this, i, self.attr(name)) );
20+
});
21+
}
22+
23+
return this.each(function(){
24+
jQuery.attr( this, name, "" );
25+
if ( this.nodeType === 1 ) {
26+
this.removeAttribute( name );
27+
}
28+
});
29+
},
30+
1531
addClass: function( value ) {
1632
if ( jQuery.isFunction(value) ) {
17-
return this.each(function() {
18-
jQuery(this).addClass( value.call(this) );
33+
return this.each(function(i) {
34+
var self = jQuery(this);
35+
self.addClass( value.call(this, i, self.attr("class")) );
1936
});
2037
}
2138

@@ -46,8 +63,9 @@ jQuery.fn.extend({
4663

4764
removeClass: function( value ) {
4865
if ( jQuery.isFunction(value) ) {
49-
return this.each(function() {
50-
jQuery(this).removeClass( value.call(this) );
66+
return this.each(function(i) {
67+
var self = jQuery(this);
68+
self.removeClass( value.call(this, i, self.attr("class")) );
5169
});
5270
}
5371

@@ -75,6 +93,40 @@ jQuery.fn.extend({
7593
return this;
7694
},
7795

96+
toggleClass: function( classNames, state ) {
97+
var type = typeof classNames;
98+
99+
if ( jQuery.isFunction( classNames ) ) {
100+
return this.each(function(i) {
101+
var self = jQuery(this);
102+
self.toggleClass( classNames.call(this, i, self.attr("class")), state );
103+
});
104+
}
105+
106+
return this.each(function(){
107+
if ( type === "string" ) {
108+
// toggle individual class names
109+
var isBool = typeof state === "boolean", className, i = 0,
110+
classNames = classNames.split( rspace );
111+
112+
while ( (className = classNames[ i++ ]) ) {
113+
// check each className given, space seperated list
114+
state = isBool ? state : !jQuery(this).hasClass( className );
115+
jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
116+
}
117+
118+
} else if ( type === "undefined" || type === "boolean" ) {
119+
if ( this.className ) {
120+
// store className if set
121+
jQuery.data( this, "__className__", this.className );
122+
}
123+
124+
// toggle whole className
125+
this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
126+
}
127+
});
128+
},
129+
78130
hasClass: function( selector ) {
79131
var className = " " + selector + " ";
80132
for ( var i = 0, l = this.length; i < l; i++ ) {
@@ -149,9 +201,11 @@ jQuery.fn.extend({
149201

150202
var val = value;
151203

152-
return this.each(function() {
204+
return this.each(function(i) {
205+
var self = jQuery(this);
206+
153207
if ( jQuery.isFunction(value) ) {
154-
val = value.call(this);
208+
val = value.call(this, i, self.val());
155209

156210
// Typecast each time if the value is a Function and the appended
157211
// value is therefore different each time.
@@ -165,13 +219,13 @@ jQuery.fn.extend({
165219
}
166220

167221
if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
168-
this.checked = jQuery.inArray( jQuery(this).val(), val ) >= 0;
222+
this.checked = jQuery.inArray( self.val(), val ) >= 0;
169223

170224
} else if ( jQuery.nodeName( this, "select" ) ) {
171225
var values = jQuery.makeArray(val);
172226

173227
jQuery( "option", this ).each(function() {
174-
this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
228+
this.selected = jQuery.inArray( self.val(), values ) >= 0;
175229
});
176230

177231
if ( !values.length ) {
@@ -185,50 +239,6 @@ jQuery.fn.extend({
185239
}
186240
});
187241

188-
jQuery.each({
189-
removeAttr: function( name ) {
190-
jQuery.attr( this, name, "" );
191-
if ( this.nodeType === 1 ) {
192-
this.removeAttribute( name );
193-
}
194-
},
195-
196-
toggleClass: function( classNames, state ) {
197-
var type = typeof classNames;
198-
199-
if ( type === "string" ) {
200-
// toggle individual class names
201-
var isBool = typeof state === "boolean", className, i = 0,
202-
classNames = classNames.split( rspace );
203-
204-
while ( (className = classNames[ i++ ]) ) {
205-
// check each className given, space seperated list
206-
state = isBool ? state : !jQuery(this).hasClass( className );
207-
jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
208-
}
209-
210-
} else if ( type === "undefined" || type === "boolean" ) {
211-
if ( this.className ) {
212-
// store className if set
213-
jQuery.data( this, "__className__", this.className );
214-
}
215-
216-
// toggle whole className
217-
this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
218-
}
219-
}
220-
}, function( name, fn ) {
221-
jQuery.fn[ name ] = function( val, state ) {
222-
if ( jQuery.isFunction( val ) ) {
223-
return this.each(function() {
224-
jQuery(this)[ name ]( val.call(this), state );
225-
});
226-
}
227-
228-
return this.each( fn, arguments );
229-
};
230-
});
231-
232242
jQuery.extend({
233243
attrFn: {
234244
val: true,

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ function access( elems, key, value, exec, fn, pass ) {
775775
exec = exec && jQuery.isFunction(value);
776776

777777
for ( var i = 0; i < length; i++ ) {
778-
fn( elems[i], key, exec ? value.call( elems[i], i ) : value, pass );
778+
fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
779779
}
780780

781781
return elems;

src/manipulation.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ if ( !jQuery.support.htmlSerialize ) {
3333
jQuery.fn.extend({
3434
text: function( text ) {
3535
if ( jQuery.isFunction(text) ) {
36-
return this.each(function() {
37-
return jQuery(this).text( text.call(this) );
36+
return this.each(function(i) {
37+
var self = jQuery(this);
38+
return self.text( text.call(this, i, self.text()) );
3839
});
3940
}
4041

@@ -47,8 +48,8 @@ jQuery.fn.extend({
4748

4849
wrapAll: function( html ) {
4950
if ( jQuery.isFunction( html ) ) {
50-
return this.each(function() {
51-
jQuery(this).wrapAll( html.apply(this, arguments) );
51+
return this.each(function(i) {
52+
jQuery(this).wrapAll( html.call(this, i) );
5253
});
5354
}
5455

@@ -228,9 +229,10 @@ jQuery.fn.extend({
228229
var results, first, value = args[0], scripts = [];
229230

230231
if ( jQuery.isFunction(value) ) {
231-
return this.each(function() {
232-
args[0] = value.call(this);
233-
return jQuery(this).domManip( args, table, callback );
232+
return this.each(function(i) {
233+
var self = jQuery(this);
234+
args[0] = value.call(this, i, table ? self.html() : undefined);
235+
return self.domManip( args, table, callback );
234236
});
235237
}
236238

0 commit comments

Comments
 (0)