Fixed some bugs relating to the setter arg change in val and html. Also optimized...
[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( value, stateVal ) {
97                 var type = typeof value, isBool = typeof stateVal === "boolean";
98
99                 if ( jQuery.isFunction( value ) ) {
100                         return this.each(function(i) {
101                                 var self = jQuery(this);
102                                 self.toggleClass( value.call(this, i, self.attr("class")), stateVal );
103                         });
104                 }
105
106                 return this.each(function() {
107                         if ( type === "string" ) {
108                                 // toggle individual class names
109                                 var className, i = 0, self = jQuery(this),
110                                         state = stateVal,
111                                         classNames = value.split( rspace );
112
113                                 while ( (className = classNames[ i++ ]) ) {
114                                         // check each className given, space seperated list
115                                         state = isBool ? state : !self.hasClass( className );
116                                         self[ state ? "addClass" : "removeClass" ]( className );
117                                 }
118
119                         } else if ( type === "undefined" || type === "boolean" ) {
120                                 if ( this.className ) {
121                                         // store className if set
122                                         jQuery.data( this, "__className__", this.className );
123                                 }
124
125                                 // toggle whole className
126                                 this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
127                         }
128                 });
129         },
130
131         hasClass: function( selector ) {
132                 var className = " " + selector + " ";
133                 for ( var i = 0, l = this.length; i < l; i++ ) {
134                         if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
135                                 return true;
136                         }
137                 }
138
139                 return false;
140         },
141
142         val: function( value ) {
143                 if ( value === undefined ) {
144                         var elem = this[0];
145
146                         if ( elem ) {
147                                 if ( jQuery.nodeName( elem, "option" ) ) {
148                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
149                                 }
150
151                                 // We need to handle select boxes special
152                                 if ( jQuery.nodeName( elem, "select" ) ) {
153                                         var index = elem.selectedIndex,
154                                                 values = [],
155                                                 options = elem.options,
156                                                 one = elem.type === "select-one";
157
158                                         // Nothing was selected
159                                         if ( index < 0 ) {
160                                                 return null;
161                                         }
162
163                                         // Loop through all the selected options
164                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
165                                                 var option = options[ i ];
166
167                                                 if ( option.selected ) {
168                                                         // Get the specifc value for the option
169                                                         value = jQuery(option).val();
170
171                                                         // We don't need an array for one selects
172                                                         if ( one ) {
173                                                                 return value;
174                                                         }
175
176                                                         // Multi-Selects return an array
177                                                         values.push( value );
178                                                 }
179                                         }
180
181                                         return values;
182                                 }
183
184                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
185                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
186                                         return elem.getAttribute("value") === null ? "on" : elem.value;
187                                 }
188                                 
189
190                                 // Everything else, we just grab the value
191                                 return (elem.value || "").replace(rreturn, "");
192
193                         }
194
195                         return undefined;
196                 }
197
198                 var isFunction = jQuery.isFunction(value);
199
200                 return this.each(function(i) {
201                         var self = jQuery(this), val = value;
202
203                         if ( this.nodeType !== 1 ) {
204                                 return;
205                         }
206
207                         if ( isFunction ) {
208                                 val = value.call(this, i, self.val());
209                         }
210
211                         // Typecast each time if the value is a Function and the appended
212                         // value is therefore different each time.
213                         if ( typeof val === "number" ) {
214                                 val += "";
215                         }
216
217                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
218                                 this.checked = jQuery.inArray( self.val(), val ) >= 0;
219
220                         } else if ( jQuery.nodeName( this, "select" ) ) {
221                                 var values = jQuery.makeArray(val);
222
223                                 jQuery( "option", this ).each(function() {
224                                         this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
225                                 });
226
227                                 if ( !values.length ) {
228                                         this.selectedIndex = -1;
229                                 }
230
231                         } else {
232                                 this.value = val;
233                         }
234                 });
235         }
236 });
237
238 jQuery.extend({
239         attrFn: {
240                 val: true,
241                 css: true,
242                 html: true,
243                 text: true,
244                 data: true,
245                 width: true,
246                 height: true,
247                 offset: true
248         },
249                 
250         attr: function( elem, name, value, pass ) {
251                 // don't set attributes on text and comment nodes
252                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
253                         return undefined;
254                 }
255
256                 if ( pass && name in jQuery.attrFn ) {
257                         return jQuery(elem)[name](value);
258                 }
259
260                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
261                         // Whether we are setting (or getting)
262                         set = value !== undefined;
263
264                 // Try to normalize/fix the name
265                 name = notxml && jQuery.props[ name ] || name;
266
267                 // Only do all the following if this is a node (faster for style)
268                 if ( elem.nodeType === 1 ) {
269                         // These attributes require special treatment
270                         var special = rspecialurl.test( name );
271
272                         // Safari mis-reports the default selected property of an option
273                         // Accessing the parent's selectedIndex property fixes it
274                         if ( name === "selected" && !jQuery.support.optSelected ) {
275                                 var parent = elem.parentNode;
276                                 if ( parent ) {
277                                         parent.selectedIndex;
278         
279                                         // Make sure that it also works with optgroups, see #5701
280                                         if ( parent.parentNode ) {
281                                                 parent.parentNode.selectedIndex;
282                                         }
283                                 }
284                         }
285
286                         // If applicable, access the attribute via the DOM 0 way
287                         if ( name in elem && notxml && !special ) {
288                                 if ( set ) {
289                                         // We can't allow the type property to be changed (since it causes problems in IE)
290                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
291                                                 throw "type property can't be changed";
292                                         }
293
294                                         elem[ name ] = value;
295                                 }
296
297                                 // browsers index elements by id/name on forms, give priority to attributes.
298                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
299                                         return elem.getAttributeNode( name ).nodeValue;
300                                 }
301
302                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
303                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
304                                 if ( name === "tabIndex" ) {
305                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
306
307                                         return attributeNode && attributeNode.specified ?
308                                                 attributeNode.value :
309                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
310                                                         0 :
311                                                         undefined;
312                                 }
313
314                                 return elem[ name ];
315                         }
316
317                         if ( !jQuery.support.style && notxml && name === "style" ) {
318                                 if ( set ) {
319                                         elem.style.cssText = "" + value;
320                                 }
321
322                                 return elem.style.cssText;
323                         }
324
325                         if ( set ) {
326                                 // convert the value to a string (all browsers do this but IE) see #1070
327                                 elem.setAttribute( name, "" + value );
328                         }
329
330                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
331                                         // Some attributes require a special call on IE
332                                         elem.getAttribute( name, 2 ) :
333                                         elem.getAttribute( name );
334
335                         // Non-existent attributes return null, we normalize to undefined
336                         return attr === null ? undefined : attr;
337                 }
338
339                 // elem is actually elem.style ... set the style
340                 // Using attr for specific style information is now deprecated. Use style insead.
341                 return jQuery.style( elem, name, value );
342         }
343 });