Make a new jQuery.support.noCloneChecked - splitting apart the previous feature detec...
[jquery.git] / src / attributes.js
1 (function( jQuery ) {
2
3 var rclass = /[\n\t\r]/g,
4         rspaces = /\s+/,
5         rreturn = /\r/g,
6         rspecialurl = /^(?:href|src|style)$/,
7         rtype = /^(?:button|input)$/i,
8         rfocusable = /^(?:button|input|object|select|textarea)$/i,
9         rclickable = /^a(?:rea)?$/i,
10         rradiocheck = /^(?:radio|checkbox)$/i;
11
12 jQuery.props = {
13         "for": "htmlFor",
14         "class": "className",
15         readonly: "readOnly",
16         maxlength: "maxLength",
17         cellspacing: "cellSpacing",
18         rowspan: "rowSpan",
19         colspan: "colSpan",
20         tabindex: "tabIndex",
21         usemap: "useMap",
22         frameborder: "frameBorder"
23 };
24
25 jQuery.fn.extend({
26         attr: function( name, value ) {
27                 return jQuery.access( this, name, value, true, jQuery.attr );
28         },
29
30         removeAttr: function( name, fn ) {
31                 return this.each(function(){
32                         jQuery.attr( this, name, "" );
33                         if ( this.nodeType === 1 ) {
34                                 this.removeAttribute( name );
35                         }
36                 });
37         },
38
39         addClass: function( value ) {
40                 if ( jQuery.isFunction(value) ) {
41                         return this.each(function(i) {
42                                 var self = jQuery(this);
43                                 self.addClass( value.call(this, i, self.attr("class")) );
44                         });
45                 }
46
47                 if ( value && typeof value === "string" ) {
48                         var classNames = (value || "").split( rspaces );
49
50                         for ( var i = 0, l = this.length; i < l; i++ ) {
51                                 var elem = this[i];
52
53                                 if ( elem.nodeType === 1 ) {
54                                         if ( !elem.className ) {
55                                                 elem.className = value;
56
57                                         } else {
58                                                 var className = " " + elem.className + " ",
59                                                         setClass = elem.className;
60
61                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
62                                                         if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
63                                                                 setClass += " " + classNames[c];
64                                                         }
65                                                 }
66                                                 elem.className = jQuery.trim( setClass );
67                                         }
68                                 }
69                         }
70                 }
71
72                 return this;
73         },
74
75         removeClass: function( value ) {
76                 if ( jQuery.isFunction(value) ) {
77                         return this.each(function(i) {
78                                 var self = jQuery(this);
79                                 self.removeClass( value.call(this, i, self.attr("class")) );
80                         });
81                 }
82
83                 if ( (value && typeof value === "string") || value === undefined ) {
84                         var classNames = (value || "").split( rspaces );
85
86                         for ( var i = 0, l = this.length; i < l; i++ ) {
87                                 var elem = this[i];
88
89                                 if ( elem.nodeType === 1 && elem.className ) {
90                                         if ( value ) {
91                                                 var className = (" " + elem.className + " ").replace(rclass, " ");
92                                                 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
93                                                         className = className.replace(" " + classNames[c] + " ", " ");
94                                                 }
95                                                 elem.className = jQuery.trim( className );
96
97                                         } else {
98                                                 elem.className = "";
99                                         }
100                                 }
101                         }
102                 }
103
104                 return this;
105         },
106
107         toggleClass: function( value, stateVal ) {
108                 var type = typeof value,
109                         isBool = typeof stateVal === "boolean";
110
111                 if ( jQuery.isFunction( value ) ) {
112                         return this.each(function(i) {
113                                 var self = jQuery(this);
114                                 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
115                         });
116                 }
117
118                 return this.each(function() {
119                         if ( type === "string" ) {
120                                 // toggle individual class names
121                                 var className,
122                                         i = 0,
123                                         self = jQuery( this ),
124                                         state = stateVal,
125                                         classNames = value.split( rspaces );
126
127                                 while ( (className = classNames[ i++ ]) ) {
128                                         // check each className given, space seperated list
129                                         state = isBool ? state : !self.hasClass( className );
130                                         self[ state ? "addClass" : "removeClass" ]( className );
131                                 }
132
133                         } else if ( type === "undefined" || type === "boolean" ) {
134                                 if ( this.className ) {
135                                         // store className if set
136                                         jQuery._data( this, "__className__", this.className );
137                                 }
138
139                                 // toggle whole className
140                                 this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || "";
141                         }
142                 });
143         },
144
145         hasClass: function( selector ) {
146                 var className = " " + selector + " ";
147                 for ( var i = 0, l = this.length; i < l; i++ ) {
148                         if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
149                                 return true;
150                         }
151                 }
152
153                 return false;
154         },
155
156         val: function( value ) {
157                 if ( !arguments.length ) {
158                         var elem = this[0];
159
160                         if ( elem ) {
161                                 if ( jQuery.nodeName( elem, "option" ) ) {
162                                         // attributes.value is undefined in Blackberry 4.7 but
163                                         // uses .value. See #6932
164                                         var val = elem.attributes.value;
165                                         return !val || val.specified ? elem.value : elem.text;
166                                 }
167
168                                 // We need to handle select boxes special
169                                 if ( jQuery.nodeName( elem, "select" ) ) {
170                                         var index = elem.selectedIndex,
171                                                 values = [],
172                                                 options = elem.options,
173                                                 one = elem.type === "select-one";
174
175                                         // Nothing was selected
176                                         if ( index < 0 ) {
177                                                 return null;
178                                         }
179
180                                         // Loop through all the selected options
181                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
182                                                 var option = options[ i ];
183
184                                                 // Don't return options that are disabled or in a disabled optgroup
185                                                 if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
186                                                                 (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
187
188                                                         // Get the specific value for the option
189                                                         value = jQuery(option).val();
190
191                                                         // We don't need an array for one selects
192                                                         if ( one ) {
193                                                                 return value;
194                                                         }
195
196                                                         // Multi-Selects return an array
197                                                         values.push( value );
198                                                 }
199                                         }
200
201                                         // Fixes Bug #2551 -- select.val() broken in IE after form.reset()
202                                         if ( one && !values.length && options.length ) {
203                                                 return jQuery( options[ index ] ).val();
204                                         }
205
206                                         return values;
207                                 }
208
209                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
210                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
211                                         return elem.getAttribute("value") === null ? "on" : elem.value;
212                                 }
213
214                                 // Everything else, we just grab the value
215                                 return (elem.value || "").replace(rreturn, "");
216
217                         }
218
219                         return undefined;
220                 }
221
222                 var isFunction = jQuery.isFunction(value);
223
224                 return this.each(function(i) {
225                         var self = jQuery(this), val = value;
226
227                         if ( this.nodeType !== 1 ) {
228                                 return;
229                         }
230
231                         if ( isFunction ) {
232                                 val = value.call(this, i, self.val());
233                         }
234
235                         // Treat null/undefined as ""; convert numbers to string
236                         if ( val == null ) {
237                                 val = "";
238                         } else if ( typeof val === "number" ) {
239                                 val += "";
240                         } else if ( jQuery.isArray(val) ) {
241                                 val = jQuery.map(val, function (value) {
242                                         return value == null ? "" : value + "";
243                                 });
244                         }
245
246                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
247                                 this.checked = jQuery.inArray( self.val(), val ) >= 0;
248
249                         } else if ( jQuery.nodeName( this, "select" ) ) {
250                                 var values = jQuery.makeArray(val);
251
252                                 jQuery( "option", this ).each(function() {
253                                         this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
254                                 });
255
256                                 if ( !values.length ) {
257                                         this.selectedIndex = -1;
258                                 }
259
260                         } else {
261                                 this.value = val;
262                         }
263                 });
264         }
265 });
266
267 jQuery.extend({
268         attrFn: {
269                 val: true,
270                 css: true,
271                 html: true,
272                 text: true,
273                 data: true,
274                 width: true,
275                 height: true,
276                 offset: true
277         },
278
279         attr: function( elem, name, value, pass ) {
280                 // don't get/set attributes on text, comment and attribute nodes
281                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || elem.nodeType === 2 ) {
282                         return undefined;
283                 }
284
285                 if ( pass && name in jQuery.attrFn ) {
286                         return jQuery(elem)[name](value);
287                 }
288
289                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
290                         // Whether we are setting (or getting)
291                         set = value !== undefined;
292
293                 // Try to normalize/fix the name
294                 name = notxml && jQuery.props[ name ] || name;
295
296                 // Only do all the following if this is a node (faster for style)
297                 if ( elem.nodeType === 1 ) {
298                         // These attributes require special treatment
299                         var special = rspecialurl.test( name );
300
301                         // Safari mis-reports the default selected property of an option
302                         // Accessing the parent's selectedIndex property fixes it
303                         if ( name === "selected" && !jQuery.support.optSelected ) {
304                                 var parent = elem.parentNode;
305                                 if ( parent ) {
306                                         parent.selectedIndex;
307
308                                         // Make sure that it also works with optgroups, see #5701
309                                         if ( parent.parentNode ) {
310                                                 parent.parentNode.selectedIndex;
311                                         }
312                                 }
313                         }
314
315                         // If applicable, access the attribute via the DOM 0 way
316                         // 'in' checks fail in Blackberry 4.7 #6931
317                         if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
318                                 if ( set ) {
319                                         // We can't allow the type property to be changed (since it causes problems in IE)
320                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
321                                                 jQuery.error( "type property can't be changed" );
322                                         }
323
324                                         if ( value === null ) {
325                                                 if ( elem.nodeType === 1 ) {
326                                                         elem.removeAttribute( name );
327                                                 }
328
329                                         } else {
330                                                 elem[ name ] = value;
331                                         }
332                                 }
333
334                                 // browsers index elements by id/name on forms, give priority to attributes.
335                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
336                                         return elem.getAttributeNode( name ).nodeValue;
337                                 }
338
339                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
340                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
341                                 if ( name === "tabIndex" ) {
342                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
343
344                                         return attributeNode && attributeNode.specified ?
345                                                 attributeNode.value :
346                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
347                                                         0 :
348                                                         undefined;
349                                 }
350
351                                 return elem[ name ];
352                         }
353
354                         if ( !jQuery.support.style && notxml && name === "style" ) {
355                                 if ( set ) {
356                                         elem.style.cssText = "" + value;
357                                 }
358
359                                 return elem.style.cssText;
360                         }
361
362                         if ( set ) {
363                                 // convert the value to a string (all browsers do this but IE) see #1070
364                                 elem.setAttribute( name, "" + value );
365                         }
366
367                         // Ensure that missing attributes return undefined
368                         // Blackberry 4.7 returns "" from getAttribute #6938
369                         if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
370                                 return undefined;
371                         }
372
373                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
374                                         // Some attributes require a special call on IE
375                                         elem.getAttribute( name, 2 ) :
376                                         elem.getAttribute( name );
377
378                         // Non-existent attributes return null, we normalize to undefined
379                         return attr === null ? undefined : attr;
380                 }
381                 // Handle everything which isn't a DOM element node
382                 if ( set ) {
383                         elem[ name ] = value;
384                 }
385                 return elem[ name ];
386         }
387 });
388
389 })( jQuery );