Merge branch 'bug7524' of https://github.com/csnover/jquery into csnover-bug7524
[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                                         return values;
202                                 }
203
204                                 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
205                                 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
206                                         return elem.getAttribute("value") === null ? "on" : elem.value;
207                                 }
208
209                                 // Everything else, we just grab the value
210                                 return (elem.value || "").replace(rreturn, "");
211
212                         }
213
214                         return undefined;
215                 }
216
217                 var isFunction = jQuery.isFunction(value);
218
219                 return this.each(function(i) {
220                         var self = jQuery(this), val = value;
221
222                         if ( this.nodeType !== 1 ) {
223                                 return;
224                         }
225
226                         if ( isFunction ) {
227                                 val = value.call(this, i, self.val());
228                         }
229
230                         // Treat null/undefined as ""; convert numbers to string
231                         if ( val == null ) {
232                                 val = "";
233                         } else if ( typeof val === "number" ) {
234                                 val += "";
235                         } else if ( jQuery.isArray(val) ) {
236                                 val = jQuery.map(val, function (value) {
237                                         return value == null ? "" : value + "";
238                                 });
239                         }
240
241                         if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
242                                 this.checked = jQuery.inArray( self.val(), val ) >= 0;
243
244                         } else if ( jQuery.nodeName( this, "select" ) ) {
245                                 var values = jQuery.makeArray(val);
246
247                                 jQuery( "option", this ).each(function() {
248                                         this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
249                                 });
250
251                                 if ( !values.length ) {
252                                         this.selectedIndex = -1;
253                                 }
254
255                         } else {
256                                 this.value = val;
257                         }
258                 });
259         }
260 });
261
262 jQuery.extend({
263         attrFn: {
264                 val: true,
265                 css: true,
266                 html: true,
267                 text: true,
268                 data: true,
269                 width: true,
270                 height: true,
271                 offset: true
272         },
273
274         attr: function( elem, name, value, pass ) {
275                 // don't set attributes on text and comment nodes
276                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
277                         return undefined;
278                 }
279
280                 if ( pass && name in jQuery.attrFn ) {
281                         return jQuery(elem)[name](value);
282                 }
283
284                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
285                         // Whether we are setting (or getting)
286                         set = value !== undefined;
287
288                 // Try to normalize/fix the name
289                 name = notxml && jQuery.props[ name ] || name;
290
291                 // Only do all the following if this is a node (faster for style)
292                 if ( elem.nodeType === 1 ) {
293                         // These attributes require special treatment
294                         var special = rspecialurl.test( name );
295
296                         // Safari mis-reports the default selected property of an option
297                         // Accessing the parent's selectedIndex property fixes it
298                         if ( name === "selected" && !jQuery.support.optSelected ) {
299                                 var parent = elem.parentNode;
300                                 if ( parent ) {
301                                         parent.selectedIndex;
302
303                                         // Make sure that it also works with optgroups, see #5701
304                                         if ( parent.parentNode ) {
305                                                 parent.parentNode.selectedIndex;
306                                         }
307                                 }
308                         }
309
310                         // If applicable, access the attribute via the DOM 0 way
311                         // 'in' checks fail in Blackberry 4.7 #6931
312                         if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
313                                 if ( set ) {
314                                         // We can't allow the type property to be changed (since it causes problems in IE)
315                                         if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
316                                                 jQuery.error( "type property can't be changed" );
317                                         }
318
319                                         if ( value === null ) {
320                                                 if ( elem.nodeType === 1 ) {
321                                                         elem.removeAttribute( name );
322                                                 }
323
324                                         } else {
325                                                 elem[ name ] = value;
326                                         }
327                                 }
328
329                                 // browsers index elements by id/name on forms, give priority to attributes.
330                                 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
331                                         return elem.getAttributeNode( name ).nodeValue;
332                                 }
333
334                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
335                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
336                                 if ( name === "tabIndex" ) {
337                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
338
339                                         return attributeNode && attributeNode.specified ?
340                                                 attributeNode.value :
341                                                 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
342                                                         0 :
343                                                         undefined;
344                                 }
345
346                                 return elem[ name ];
347                         }
348
349                         if ( !jQuery.support.style && notxml && name === "style" ) {
350                                 if ( set ) {
351                                         elem.style.cssText = "" + value;
352                                 }
353
354                                 return elem.style.cssText;
355                         }
356
357                         if ( set ) {
358                                 // convert the value to a string (all browsers do this but IE) see #1070
359                                 elem.setAttribute( name, "" + value );
360                         }
361
362                         // Ensure that missing attributes return undefined
363                         // Blackberry 4.7 returns "" from getAttribute #6938
364                         if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
365                                 return undefined;
366                         }
367
368                         var attr = !jQuery.support.hrefNormalized && notxml && special ?
369                                         // Some attributes require a special call on IE
370                                         elem.getAttribute( name, 2 ) :
371                                         elem.getAttribute( name );
372
373                         // Non-existent attributes return null, we normalize to undefined
374                         return attr === null ? undefined : attr;
375                 }
376                 // Handle everything which isn't a DOM element node
377                 if ( set ) {
378                         elem[ name ] = value;
379                 }
380                 return elem[ name ];
381         }
382 });
383
384 })( jQuery );