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