Brought the attributes module more inline with the jQuery style guidelines.
[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 || this.text, 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() { jQuery(this)[ name ]( val.call(this), state ); });
217                 }
218
219                 return this.each( fn, arguments );
220         };
221 });
222
223 jQuery.extend({
224         attrFn: {
225                 val: true,
226                 css: true,
227                 html: true,
228                 text: true,
229                 data: true,
230                 width: true,
231                 height: true,
232                 offset: true
233         },
234                 
235         attr: function( elem, name, value ) {
236                 // don't set attributes on text and comment nodes
237                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
238                         return undefined;
239                 }
240
241                 if ( name in jQuery.attrFn && value !== undefined ) {
242                         return jQuery(elem)[name](value);
243                 }
244
245                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
246                         // Whether we are setting (or getting)
247                         set = value !== undefined;
248
249                 // Try to normalize/fix the name
250                 name = notxml && jQuery.props[ name ] || name;
251
252                 // Only do all the following if this is a node (faster for style)
253                 if ( elem.nodeType === 1 ) {
254                         // These attributes require special treatment
255                         var special = rspecialurl.test( name );
256
257                         // Safari mis-reports the default selected property of a hidden option
258                         // Accessing the parent's selectedIndex property fixes it
259                         if ( name === "selected" && elem.parentNode ) {
260                                 elem.parentNode.selectedIndex;
261                         }
262
263                         // If applicable, access the attribute via the DOM 0 way
264                         if ( name in elem && notxml && !special ) {
265                                 if ( set ) {
266                                         // We can't allow the type property to be changed (since it causes problems in IE)
267                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
268                                                 throw "type property can't be changed";
269                                         }
270
271                                         // browsers index elements by id/name on forms, give priority to attributes.
272                                         if( jQuery.nodeName( elem, "form" ) ) {
273                                                 // convert the value to a string (all browsers do this but IE) see #1070
274                                                 elem.setAttribute( name, "" + value );
275
276                                         } else {
277                                                 elem[ name ] = value;
278                                         }
279                                 }
280
281                                 // browsers index elements by id/name on forms, give priority to attributes.
282                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
283                                         return elem.getAttributeNode( name ).nodeValue;
284                                 }
285
286                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
287                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
288                                 if ( name === "tabIndex" ) {
289                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
290
291                                         return attributeNode && attributeNode.specified ?
292                                                 attributeNode.value :
293                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
294                                                         0 :
295                                                         undefined;
296                                 }
297
298                                 return elem[ name ];
299                         }
300
301                         if ( !jQuery.support.style && notxml && name === "style" ) {
302                                 if ( set ) {
303                                         elem.style.cssText = "" + value;
304                                 }
305
306                                 return elem.style.cssText;
307                         }
308
309                         if ( set ) {
310                                 // convert the value to a string (all browsers do this but IE) see #1070
311                                 elem.setAttribute( name, "" + value );
312                         }
313
314                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
315                                         // Some attributes require a special call on IE
316                                         elem.getAttribute( name, 2 ) :
317                                         elem.getAttribute( name );
318
319                         // Non-existent attributes return null, we normalize to undefined
320                         return attr === null ? undefined : attr;
321                 }
322
323                 // elem is actually elem.style ... set the style
324                 // Using attr for specific style information is now deprecated. Use style insead.
325                 return jQuery.style( elem, name, value );
326         }
327 });