A first pass at making sure that all the setter function arguments receive the index...
[jquery.git] / src / attributes.js
1 var rclass = /[\n\t]/g,
2         rspace = /\s+/,
3         rreturn = /\r/g,
4         rspecialurl = /href|src|style/,
5         rtype = /(button|input)/i,
6         rfocusable = /(button|input|object|select|textarea)/i,
7         rclickable = /^(a|area)$/i,
8         rradiocheck = /radio|checkbox/;
9
10 jQuery.fn.extend({
11         attr: function( name, value ) {
12                 return access( this, name, value, true, jQuery.attr );
13         },
14
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
31         addClass: function( value ) {
32                 if ( jQuery.isFunction(value) ) {
33                         return this.each(function(i) {
34                                 var self = jQuery(this);
35                                 self.addClass( value.call(this, i, self.attr("class")) );
36                         });
37                 }
38
39                 if ( value && typeof value === "string" ) {
40                         var classNames = (value || "").split( rspace );
41
42                         for ( var i = 0, l = this.length; i < l; i++ ) {
43                                 var elem = this[i];
44
45                                 if ( elem.nodeType === 1 ) {
46                                         if ( !elem.className ) {
47                                                 elem.className = value;
48
49                                         } else {
50                                                 var className = " " + elem.className + " ";
51                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
52                                                         if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
53                                                                 elem.className += " " + classNames[c];
54                                                         }
55                                                 }
56                                         }
57                                 }
58                         }
59                 }
60
61                 return this;
62         },
63
64         removeClass: function( value ) {
65                 if ( jQuery.isFunction(value) ) {
66                         return this.each(function(i) {
67                                 var self = jQuery(this);
68                                 self.removeClass( value.call(this, i, self.attr("class")) );
69                         });
70                 }
71
72                 if ( (value && typeof value === "string") || value === undefined ) {
73                         var classNames = (value || "").split(rspace);
74
75                         for ( var i = 0, l = this.length; i < l; i++ ) {
76                                 var elem = this[i];
77
78                                 if ( elem.nodeType === 1 && elem.className ) {
79                                         if ( value ) {
80                                                 var className = (" " + elem.className + " ").replace(rclass, " ");
81                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
82                                                         className = className.replace(" " + classNames[c] + " ", " ");
83                                                 }
84                                                 elem.className = className.substring(1, className.length - 1);
85
86                                         } else {
87                                                 elem.className = "";
88                                         }
89                                 }
90                         }
91                 }
92
93                 return this;
94         },
95
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
130         hasClass: function( selector ) {
131                 var className = " " + selector + " ";
132                 for ( var i = 0, l = this.length; i < l; i++ ) {
133                         if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
134                                 return true;
135                         }
136                 }
137
138                 return false;
139         },
140
141         val: function( value ) {
142                 if ( value === undefined ) {
143                         var elem = this[0];
144
145                         if ( elem ) {
146                                 if ( jQuery.nodeName( elem, "option" ) ) {
147                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
148                                 }
149
150                                 // We need to handle select boxes special
151                                 if ( jQuery.nodeName( elem, "select" ) ) {
152                                         var index = elem.selectedIndex,
153                                                 values = [],
154                                                 options = elem.options,
155                                                 one = elem.type === "select-one";
156
157                                         // Nothing was selected
158                                         if ( index < 0 ) {
159                                                 return null;
160                                         }
161
162                                         // Loop through all the selected options
163                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
164                                                 var option = options[ i ];
165
166                                                 if ( option.selected ) {
167                                                         // Get the specifc value for the option
168                                                         value = jQuery(option).val();
169
170                                                         // We don't need an array for one selects
171                                                         if ( one ) {
172                                                                 return value;
173                                                         }
174
175                                                         // Multi-Selects return an array
176                                                         values.push( value );
177                                                 }
178                                         }
179
180                                         return values;
181                                 }
182
183                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
184                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
185                                         return elem.getAttribute("value") === null ? "on" : elem.value;
186                                 }
187                                 
188
189                                 // Everything else, we just grab the value
190                                 return (elem.value || "").replace(rreturn, "");
191
192                         }
193
194                         return undefined;
195                 }
196
197                 // Typecast once if the value is a number
198                 if ( typeof value === "number" ) {
199                         value += "";
200                 }
201
202                 var val = value;
203
204                 return this.each(function(i) {
205                         var self = jQuery(this);
206
207                         if ( jQuery.isFunction(value) ) {
208                                 val = value.call(this, i, self.val());
209
210                                 // Typecast each time if the value is a Function and the appended
211                                 // value is therefore different each time.
212                                 if ( typeof val === "number" ) {
213                                         val += "";
214                                 }
215                         }
216
217                         if ( this.nodeType !== 1 ) {
218                                 return;
219                         }
220
221                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
222                                 this.checked = jQuery.inArray( self.val(), val ) >= 0;
223
224                         } else if ( jQuery.nodeName( this, "select" ) ) {
225                                 var values = jQuery.makeArray(val);
226
227                                 jQuery( "option", this ).each(function() {
228                                         this.selected = jQuery.inArray( self.val(), values ) >= 0;
229                                 });
230
231                                 if ( !values.length ) {
232                                         this.selectedIndex = -1;
233                                 }
234
235                         } else {
236                                 this.value = val;
237                         }
238                 });
239         }
240 });
241
242 jQuery.extend({
243         attrFn: {
244                 val: true,
245                 css: true,
246                 html: true,
247                 text: true,
248                 data: true,
249                 width: true,
250                 height: true,
251                 offset: true
252         },
253                 
254         attr: function( elem, name, value, pass ) {
255                 // don't set attributes on text and comment nodes
256                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
257                         return undefined;
258                 }
259
260                 if ( pass && name in jQuery.attrFn ) {
261                         return jQuery(elem)[name](value);
262                 }
263
264                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
265                         // Whether we are setting (or getting)
266                         set = value !== undefined;
267
268                 // Try to normalize/fix the name
269                 name = notxml && jQuery.props[ name ] || name;
270
271                 // Only do all the following if this is a node (faster for style)
272                 if ( elem.nodeType === 1 ) {
273                         // These attributes require special treatment
274                         var special = rspecialurl.test( name );
275
276                         // Safari mis-reports the default selected property of an option
277                         // Accessing the parent's selectedIndex property fixes it
278                         if ( name === "selected" && !jQuery.support.optSelected ) {
279                                 var parent = elem.parentNode;
280                                 if ( parent ) {
281                                         parent.selectedIndex;
282         
283                                         // Make sure that it also works with optgroups, see #5701
284                                         if ( parent.parentNode ) {
285                                                 parent.parentNode.selectedIndex;
286                                         }
287                                 }
288                         }
289
290                         // If applicable, access the attribute via the DOM 0 way
291                         if ( name in elem && notxml && !special ) {
292                                 if ( set ) {
293                                         // We can't allow the type property to be changed (since it causes problems in IE)
294                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
295                                                 throw "type property can't be changed";
296                                         }
297
298                                         elem[ name ] = value;
299                                 }
300
301                                 // browsers index elements by id/name on forms, give priority to attributes.
302                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
303                                         return elem.getAttributeNode( name ).nodeValue;
304                                 }
305
306                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
307                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
308                                 if ( name === "tabIndex" ) {
309                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
310
311                                         return attributeNode && attributeNode.specified ?
312                                                 attributeNode.value :
313                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
314                                                         0 :
315                                                         undefined;
316                                 }
317
318                                 return elem[ name ];
319                         }
320
321                         if ( !jQuery.support.style && notxml && name === "style" ) {
322                                 if ( set ) {
323                                         elem.style.cssText = "" + value;
324                                 }
325
326                                 return elem.style.cssText;
327                         }
328
329                         if ( set ) {
330                                 // convert the value to a string (all browsers do this but IE) see #1070
331                                 elem.setAttribute( name, "" + value );
332                         }
333
334                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
335                                         // Some attributes require a special call on IE
336                                         elem.getAttribute( name, 2 ) :
337                                         elem.getAttribute( name );
338
339                         // Non-existent attributes return null, we normalize to undefined
340                         return attr === null ? undefined : attr;
341                 }
342
343                 // elem is actually elem.style ... set the style
344                 // Using attr for specific style information is now deprecated. Use style insead.
345                 return jQuery.style( elem, name, value );
346         }
347 });