The logic for specified option values was already handled by .val(), removed the...
[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         addClass: function( value ) {
16                 if ( jQuery.isFunction(value) ) {
17                         return this.each(function() {
18                                 jQuery(this).addClass( value.call(this) );
19                         });
20                 }
21
22                 if ( value && typeof value === "string" ) {
23                         var classNames = (value || "").split( rspace );
24
25                         for ( var i = 0, l = this.length; i < l; i++ ) {
26                                 var elem = this[i];
27
28                                 if ( elem.nodeType === 1 ) {
29                                         if ( !elem.className ) {
30                                                 elem.className = value;
31
32                                         } else {
33                                                 var className = " " + elem.className + " ";
34                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
35                                                         if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
36                                                                 elem.className += " " + classNames[c];
37                                                         }
38                                                 }
39                                         }
40                                 }
41                         }
42                 }
43
44                 return this;
45         },
46
47         removeClass: function( value ) {
48                 if ( jQuery.isFunction(value) ) {
49                         return this.each(function() {
50                                 jQuery(this).removeClass( value.call(this) );
51                         });
52                 }
53
54                 if ( (value && typeof value === "string") || value === undefined ) {
55                         var classNames = (value || "").split(rspace);
56
57                         for ( var i = 0, l = this.length; i < l; i++ ) {
58                                 var elem = this[i];
59
60                                 if ( elem.nodeType === 1 && elem.className ) {
61                                         if ( value ) {
62                                                 var className = (" " + elem.className + " ").replace(rclass, " ");
63                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
64                                                         className = className.replace(" " + classNames[c] + " ", " ");
65                                                 }
66                                                 elem.className = className.substring(1, className.length - 1);
67
68                                         } else {
69                                                 elem.className = "";
70                                         }
71                                 }
72                         }
73                 }
74
75                 return this;
76         },
77
78         hasClass: function( selector ) {
79                 var className = " " + selector + " ";
80                 for ( var i = 0, l = this.length; i < l; i++ ) {
81                         if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
82                                 return true;
83                         }
84                 }
85
86                 return false;
87         },
88
89         val: function( value ) {
90                 if ( value === undefined ) {
91                         var elem = this[0];
92
93                         if ( elem ) {
94                                 if ( jQuery.nodeName( elem, "option" ) ) {
95                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
96                                 }
97
98                                 // We need to handle select boxes special
99                                 if ( jQuery.nodeName( elem, "select" ) ) {
100                                         var index = elem.selectedIndex,
101                                                 values = [],
102                                                 options = elem.options,
103                                                 one = elem.type === "select-one";
104
105                                         // Nothing was selected
106                                         if ( index < 0 ) {
107                                                 return null;
108                                         }
109
110                                         // Loop through all the selected options
111                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
112                                                 var option = options[ i ];
113
114                                                 if ( option.selected ) {
115                                                         // Get the specifc value for the option
116                                                         value = jQuery(option).val();
117
118                                                         // We don't need an array for one selects
119                                                         if ( one ) {
120                                                                 return value;
121                                                         }
122
123                                                         // Multi-Selects return an array
124                                                         values.push( value );
125                                                 }
126                                         }
127
128                                         return values;
129                                 }
130
131                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
132                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
133                                         return elem.getAttribute("value") === null ? "on" : elem.value;
134                                 }
135                                 
136
137                                 // Everything else, we just grab the value
138                                 return (elem.value || "").replace(rreturn, "");
139
140                         }
141
142                         return undefined;
143                 }
144
145                 // Typecast once if the value is a number
146                 if ( typeof value === "number" ) {
147                         value += "";
148                 }
149
150                 var val = value;
151
152                 return this.each(function() {
153                         if ( jQuery.isFunction(value) ) {
154                                 val = value.call(this);
155
156                                 // Typecast each time if the value is a Function and the appended
157                                 // value is therefore different each time.
158                                 if ( typeof val === "number" ) {
159                                         val += "";
160                                 }
161                         }
162
163                         if ( this.nodeType !== 1 ) {
164                                 return;
165                         }
166
167                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
168                                 this.checked = jQuery.inArray( jQuery(this).val(), val ) >= 0;
169
170                         } else if ( jQuery.nodeName( this, "select" ) ) {
171                                 var values = jQuery.makeArray(val);
172
173                                 jQuery( "option", this ).each(function() {
174                                         this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
175                                 });
176
177                                 if ( !values.length ) {
178                                         this.selectedIndex = -1;
179                                 }
180
181                         } else {
182                                 this.value = val;
183                         }
184                 });
185         }
186 });
187
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
232 jQuery.extend({
233         attrFn: {
234                 val: true,
235                 css: true,
236                 html: true,
237                 text: true,
238                 data: true,
239                 width: true,
240                 height: true,
241                 offset: true
242         },
243                 
244         attr: function( elem, name, value, pass ) {
245                 // don't set attributes on text and comment nodes
246                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
247                         return undefined;
248                 }
249
250                 if ( pass && name in jQuery.attrFn ) {
251                         return jQuery(elem)[name](value);
252                 }
253
254                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
255                         // Whether we are setting (or getting)
256                         set = value !== undefined;
257
258                 // Try to normalize/fix the name
259                 name = notxml && jQuery.props[ name ] || name;
260
261                 // Only do all the following if this is a node (faster for style)
262                 if ( elem.nodeType === 1 ) {
263                         // These attributes require special treatment
264                         var special = rspecialurl.test( name );
265
266                         // Safari mis-reports the default selected property of a hidden option
267                         // Accessing the parent's selectedIndex property fixes it
268                         if ( name === "selected" && elem.parentNode ) {
269                                 elem.parentNode.selectedIndex;
270                         }
271
272                         // If applicable, access the attribute via the DOM 0 way
273                         if ( name in elem && notxml && !special ) {
274                                 if ( set ) {
275                                         // We can't allow the type property to be changed (since it causes problems in IE)
276                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
277                                                 throw "type property can't be changed";
278                                         }
279
280                                         elem[ name ] = value;
281                                 }
282
283                                 // browsers index elements by id/name on forms, give priority to attributes.
284                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
285                                         return elem.getAttributeNode( name ).nodeValue;
286                                 }
287
288                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
289                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
290                                 if ( name === "tabIndex" ) {
291                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
292
293                                         return attributeNode && attributeNode.specified ?
294                                                 attributeNode.value :
295                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
296                                                         0 :
297                                                         undefined;
298                                 }
299
300                                 return elem[ name ];
301                         }
302
303                         if ( !jQuery.support.style && notxml && name === "style" ) {
304                                 if ( set ) {
305                                         elem.style.cssText = "" + value;
306                                 }
307
308                                 return elem.style.cssText;
309                         }
310
311                         if ( set ) {
312                                 // convert the value to a string (all browsers do this but IE) see #1070
313                                 elem.setAttribute( name, "" + value );
314                         }
315
316                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
317                                         // Some attributes require a special call on IE
318                                         elem.getAttribute( name, 2 ) :
319                                         elem.getAttribute( name );
320
321                         // Non-existent attributes return null, we normalize to undefined
322                         return attr === null ? undefined : attr;
323                 }
324
325                 // elem is actually elem.style ... set the style
326                 // Using attr for specific style information is now deprecated. Use style insead.
327                 return jQuery.style( elem, name, value );
328         }
329 });