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