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