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