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