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