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