Make sure that missing attributes return undefined in Blackberry 4.7. Fixes #6938.
[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                                         // attributes.value is undefined in Blackberry 4.7 but
143                                         // uses .value. See #6932
144                                         var val = elem.attributes.value;
145                                         return !val || val.specified ? elem.value : elem.text;
146                                 }
147
148                                 // We need to handle select boxes special
149                                 if ( jQuery.nodeName( elem, "select" ) ) {
150                                         var index = elem.selectedIndex,
151                                                 values = [],
152                                                 options = elem.options,
153                                                 one = elem.type === "select-one";
154
155                                         // Nothing was selected
156                                         if ( index < 0 ) {
157                                                 return null;
158                                         }
159
160                                         // Loop through all the selected options
161                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
162                                                 var option = options[ i ];
163
164                                                 if ( option.selected ) {
165                                                         // Get the specifc value for the option
166                                                         value = jQuery(option).val();
167
168                                                         // We don't need an array for one selects
169                                                         if ( one ) {
170                                                                 return value;
171                                                         }
172
173                                                         // Multi-Selects return an array
174                                                         values.push( value );
175                                                 }
176                                         }
177
178                                         return values;
179                                 }
180
181                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
182                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
183                                         return elem.getAttribute("value") === null ? "on" : elem.value;
184                                 }
185                                 
186
187                                 // Everything else, we just grab the value
188                                 return (elem.value || "").replace(rreturn, "");
189
190                         }
191
192                         return undefined;
193                 }
194
195                 var isFunction = jQuery.isFunction(value);
196
197                 return this.each(function(i) {
198                         var self = jQuery(this), val = value;
199
200                         if ( this.nodeType !== 1 ) {
201                                 return;
202                         }
203
204                         if ( isFunction ) {
205                                 val = value.call(this, i, self.val());
206                         }
207
208                         // Typecast each time if the value is a Function and the appended
209                         // value is therefore different each time.
210                         if ( typeof val === "number" ) {
211                                 val += "";
212                         }
213
214                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
215                                 this.checked = jQuery.inArray( self.val(), val ) >= 0;
216
217                         } else if ( jQuery.nodeName( this, "select" ) ) {
218                                 var values = jQuery.makeArray(val);
219
220                                 jQuery( "option", this ).each(function() {
221                                         this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
222                                 });
223
224                                 if ( !values.length ) {
225                                         this.selectedIndex = -1;
226                                 }
227
228                         } else {
229                                 this.value = val;
230                         }
231                 });
232         }
233 });
234
235 jQuery.extend({
236         attrFn: {
237                 val: true,
238                 css: true,
239                 html: true,
240                 text: true,
241                 data: true,
242                 width: true,
243                 height: true,
244                 offset: true
245         },
246                 
247         attr: function( elem, name, value, pass ) {
248                 // don't set attributes on text and comment nodes
249                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
250                         return undefined;
251                 }
252
253                 if ( pass && name in jQuery.attrFn ) {
254                         return jQuery(elem)[name](value);
255                 }
256
257                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
258                         // Whether we are setting (or getting)
259                         set = value !== undefined;
260
261                 // Try to normalize/fix the name
262                 name = notxml && jQuery.props[ name ] || name;
263
264                 // Only do all the following if this is a node (faster for style)
265                 if ( elem.nodeType === 1 ) {
266                         // These attributes require special treatment
267                         var special = rspecialurl.test( name );
268
269                         // Safari mis-reports the default selected property of an option
270                         // Accessing the parent's selectedIndex property fixes it
271                         if ( name === "selected" && !jQuery.support.optSelected ) {
272                                 var parent = elem.parentNode;
273                                 if ( parent ) {
274                                         parent.selectedIndex;
275         
276                                         // Make sure that it also works with optgroups, see #5701
277                                         if ( parent.parentNode ) {
278                                                 parent.parentNode.selectedIndex;
279                                         }
280                                 }
281                         }
282
283                         // If applicable, access the attribute via the DOM 0 way
284                         // 'in' checks fail in Blackberry 4.7 #6931
285                         if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
286                                 if ( set ) {
287                                         // We can't allow the type property to be changed (since it causes problems in IE)
288                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
289                                                 jQuery.error( "type property can't be changed" );
290                                         }
291
292                                         elem[ name ] = value;
293                                 }
294
295                                 // browsers index elements by id/name on forms, give priority to attributes.
296                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
297                                         return elem.getAttributeNode( name ).nodeValue;
298                                 }
299
300                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
301                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
302                                 if ( name === "tabIndex" ) {
303                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
304
305                                         return attributeNode && attributeNode.specified ?
306                                                 attributeNode.value :
307                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
308                                                         0 :
309                                                         undefined;
310                                 }
311
312                                 return elem[ name ];
313                         }
314
315                         if ( !jQuery.support.style && notxml && name === "style" ) {
316                                 if ( set ) {
317                                         elem.style.cssText = "" + value;
318                                 }
319
320                                 return elem.style.cssText;
321                         }
322
323                         if ( set ) {
324                                 // convert the value to a string (all browsers do this but IE) see #1070
325                                 elem.setAttribute( name, "" + value );
326                         }
327
328                         // Ensure that missing attributes return undefined
329                         // Blackberry 4.7 returns "" from getAttribute #6938
330                         if ( !elem.attributes[ name ] && !elem.hasAttribute( name ) ) {
331                                 return undefined;
332                         }
333
334                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
335                                         // Some attributes require a special call on IE
336                                         elem.getAttribute( name, 2 ) :
337                                         elem.getAttribute( name );
338
339                         // Non-existent attributes return null, we normalize to undefined
340                         return attr === null ? undefined : attr;
341                 }
342
343                 // elem is actually elem.style ... set the style
344                 // Using attr for specific style information is now deprecated. Use style instead.
345                 return jQuery.style( elem, name, value );
346         }
347 });