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