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