breaking jquery out into smaller modules. added attributes.js, manipulation.js, and...
[jquery.git] / src / attributes.js
1 jQuery.fn.extend({
2         attr: function( name, value, type ) {
3                 var options = name, isFunction = jQuery.isFunction( value );
4
5                 // Look for the case where we're accessing a style value
6                 if ( typeof name === "string" ) {
7                         if ( value === undefined ) {
8                                 return this.length ?
9                                         jQuery[ type || "attr" ]( this[0], name ) :
10                                         null;
11
12                         } else {
13                                 options = {};
14                                 options[ name ] = value;
15                         }
16                 }
17
18                 // Check to see if we're setting style values
19                 for ( var i = 0, l = this.length; i < l; i++ ) {
20                         var elem = this[i];
21
22                         // Set all the styles
23                         for ( var prop in options ) {
24                                 value = options[prop];
25
26                                 if ( isFunction ) {
27                                         value = value.call( elem, i );
28                                 }
29
30                                 if ( typeof value === "number" && type === "curCSS" && !exclude.test(prop) ) {
31                                         value = value + "px";
32                                 }
33
34                                 jQuery.attr( type ? elem.style : elem, prop, value );
35                         }
36                 }
37
38                 return this;
39         },
40
41         css: function( key, value ) {
42                 // ignore negative width and height values
43                 if ( (key == 'width' || key == 'height') && parseFloat(value) < 0 )
44                         value = undefined;
45                 return this.attr( key, value, "curCSS" );
46         },
47
48         hasClass: function( selector ) {
49                 return !!selector && this.is( "." + selector );
50         },
51
52         val: function( value ) {
53                 if ( value === undefined ) {
54                         var elem = this[0];
55
56                         if ( elem ) {
57                                 if( jQuery.nodeName( elem, 'option' ) )
58                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
59                                 
60                                 // We need to handle select boxes special
61                                 if ( jQuery.nodeName( elem, "select" ) ) {
62                                         var index = elem.selectedIndex,
63                                                 values = [],
64                                                 options = elem.options,
65                                                 one = elem.type == "select-one";
66
67                                         // Nothing was selected
68                                         if ( index < 0 )
69                                                 return null;
70
71                                         // Loop through all the selected options
72                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
73                                                 var option = options[ i ];
74
75                                                 if ( option.selected ) {
76                                                         // Get the specifc value for the option
77                                                         value = jQuery(option).val();
78
79                                                         // We don't need an array for one selects
80                                                         if ( one )
81                                                                 return value;
82
83                                                         // Multi-Selects return an array
84                                                         values.push( value );
85                                                 }
86                                         }
87
88                                         return values;                          
89                                 }
90
91                                 // Everything else, we just grab the value
92                                 return (elem.value || "").replace(/\r/g, "");
93
94                         }
95
96                         return undefined;
97                 }
98
99                 if ( typeof value === "number" )
100                         value += '';
101
102                 return this.each(function(){
103                         if ( this.nodeType != 1 )
104                                 return;
105
106                         if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
107                                 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
108                                         jQuery.inArray(this.name, value) >= 0);
109
110                         else if ( jQuery.nodeName( this, "select" ) ) {
111                                 var values = jQuery.makeArray(value);
112
113                                 jQuery( "option", this ).each(function(){
114                                         this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
115                                                 jQuery.inArray( this.text, values ) >= 0);
116                                 });
117
118                                 if ( !values.length )
119                                         this.selectedIndex = -1;
120
121                         } else
122                                 this.value = value;
123                 });
124         }
125 });
126
127 jQuery.each({
128         removeAttr: function( name ) {
129                 jQuery.attr( this, name, "" );
130                 if (this.nodeType == 1)
131                         this.removeAttribute( name );
132         },
133
134         addClass: function( classNames ) {
135                 jQuery.className.add( this, classNames );
136         },
137
138         removeClass: function( classNames ) {
139                 jQuery.className.remove( this, classNames );
140         },
141
142         toggleClass: function( classNames, state ) {
143                 if( typeof state !== "boolean" )
144                         state = !jQuery.className.has( this, classNames );
145                 jQuery.className[ state ? "add" : "remove" ]( this, classNames );
146         }
147 }, function(name, fn){
148         jQuery.fn[ name ] = function(){
149                 return this.each( fn, arguments );
150         };
151 });
152
153 jQuery.extend({
154         className: {
155                 // internal only, use addClass("class")
156                 add: function( elem, classNames ) {
157                         jQuery.each((classNames || "").split(/\s+/), function(i, className){
158                                 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
159                                         elem.className += (elem.className ? " " : "") + className;
160                         });
161                 },
162
163                 // internal only, use removeClass("class")
164                 remove: function( elem, classNames ) {
165                         if (elem.nodeType == 1)
166                                 elem.className = classNames !== undefined ?
167                                         jQuery.grep(elem.className.split(/\s+/), function(className){
168                                                 return !jQuery.className.has( classNames, className );
169                                         }).join(" ") :
170                                         "";
171                 },
172
173                 // internal only, use hasClass("class")
174                 has: function( elem, className ) {
175                         return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
176                 }
177         },
178
179         // A method for quickly swapping in/out CSS properties to get correct calculations
180         swap: function( elem, options, callback ) {
181                 var old = {};
182                 // Remember the old values, and insert the new ones
183                 for ( var name in options ) {
184                         old[ name ] = elem.style[ name ];
185                         elem.style[ name ] = options[ name ];
186                 }
187
188                 callback.call( elem );
189
190                 // Revert the old values
191                 for ( var name in options )
192                         elem.style[ name ] = old[ name ];
193         },
194
195         css: function( elem, name, force, extra ) {
196                 if ( name == "width" || name == "height" ) {
197                         var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
198
199                         function getWH() {
200                                 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
201
202                                 if ( extra === "border" )
203                                         return;
204
205                                 jQuery.each( which, function() {
206                                         if ( !extra )
207                                                 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
208                                         if ( extra === "margin" )
209                                                 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
210                                         else
211                                                 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
212                                 });
213                         }
214
215                         if ( elem.offsetWidth !== 0 )
216                                 getWH();
217                         else
218                                 jQuery.swap( elem, props, getWH );
219
220                         return Math.max(0, Math.round(val));
221                 }
222
223                 return jQuery.curCSS( elem, name, force );
224         },
225
226         curCSS: function( elem, name, force ) {
227                 var ret, style = elem.style;
228
229                 // We need to handle opacity special in IE
230                 if ( name == "opacity" && !jQuery.support.opacity ) {
231                         ret = jQuery.attr( style, "opacity" );
232
233                         return ret == "" ?
234                                 "1" :
235                                 ret;
236                 }
237
238                 // Make sure we're using the right name for getting the float value
239                 if ( name.match( /float/i ) )
240                         name = styleFloat;
241
242                 if ( !force && style && style[ name ] )
243                         ret = style[ name ];
244
245                 else if ( defaultView.getComputedStyle ) {
246
247                         // Only "float" is needed here
248                         if ( name.match( /float/i ) )
249                                 name = "float";
250
251                         name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
252
253                         var computedStyle = defaultView.getComputedStyle( elem, null );
254
255                         if ( computedStyle )
256                                 ret = computedStyle.getPropertyValue( name );
257
258                         // We should always get a number back from opacity
259                         if ( name == "opacity" && ret == "" )
260                                 ret = "1";
261
262                 } else if ( elem.currentStyle ) {
263                         var camelCase = name.replace(/\-(\w)/g, function(all, letter){
264                                 return letter.toUpperCase();
265                         });
266
267                         ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
268
269                         // From the awesome hack by Dean Edwards
270                         // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
271
272                         // If we're not dealing with a regular pixel number
273                         // but a number that has a weird ending, we need to convert it to pixels
274                         if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) {
275                                 // Remember the original values
276                                 var left = style.left, rsLeft = elem.runtimeStyle.left;
277
278                                 // Put in the new values to get a computed value out
279                                 elem.runtimeStyle.left = elem.currentStyle.left;
280                                 style.left = ret || 0;
281                                 ret = style.pixelLeft + "px";
282
283                                 // Revert the changed values
284                                 style.left = left;
285                                 elem.runtimeStyle.left = rsLeft;
286                         }
287                 }
288
289                 return ret;
290         },
291
292         attr: function( elem, name, value ) {
293                 // don't set attributes on text and comment nodes
294                 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
295                         return undefined;
296
297                 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
298                         // Whether we are setting (or getting)
299                         set = value !== undefined;
300
301                 // Try to normalize/fix the name
302                 name = notxml && jQuery.props[ name ] || name;
303
304                 // Only do all the following if this is a node (faster for style)
305                 // IE elem.getAttribute passes even for style
306                 if ( elem.tagName ) {
307
308                         // These attributes require special treatment
309                         var special = /href|src|style/.test( name );
310
311                         // Safari mis-reports the default selected property of a hidden option
312                         // Accessing the parent's selectedIndex property fixes it
313                         if ( name == "selected" && elem.parentNode )
314                                 elem.parentNode.selectedIndex;
315
316                         // If applicable, access the attribute via the DOM 0 way
317                         if ( name in elem && notxml && !special ) {
318                                 if ( set ){
319                                         // We can't allow the type property to be changed (since it causes problems in IE)
320                                         if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
321                                                 throw "type property can't be changed";
322
323                                         elem[ name ] = value;
324                                 }
325
326                                 // browsers index elements by id/name on forms, give priority to attributes.
327                                 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
328                                         return elem.getAttributeNode( name ).nodeValue;
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                                         return attributeNode && attributeNode.specified
335                                                 ? attributeNode.value
336                                                 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
337                                                         ? 0
338                                                         : elem.nodeName.match(/^(a|area)$/i) && elem.href
339                                                                 ? 0
340                                                                 : undefined;
341                                 }
342
343                                 return elem[ name ];
344                         }
345
346                         if ( !jQuery.support.style && notxml &&  name == "style" )
347                                 return jQuery.attr( elem.style, "cssText", value );
348
349                         if ( set )
350                                 // convert the value to a string (all browsers do this but IE) see #1070
351                                 elem.setAttribute( name, "" + value );
352
353                         var attr = !jQuery.support.hrefNormalized && notxml && special
354                                         // Some attributes require a special call on IE
355                                         ? elem.getAttribute( name, 2 )
356                                         : elem.getAttribute( name );
357
358                         // Non-existent attributes return null, we normalize to undefined
359                         return attr === null ? undefined : attr;
360                 }
361
362                 // elem is actually elem.style ... set the style
363
364                 // IE uses filters for opacity
365                 if ( !jQuery.support.opacity && name == "opacity" ) {
366                         if ( set ) {
367                                 // IE has trouble with opacity if it does not have layout
368                                 // Force it by setting the zoom level
369                                 elem.zoom = 1;
370
371                                 // Set the alpha filter to set the opacity
372                                 elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
373                                         (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
374                         }
375
376                         return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
377                                 (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
378                                 "";
379                 }
380
381                 name = name.replace(/-([a-z])/ig, function(all, letter){
382                         return letter.toUpperCase();
383                 });
384
385                 if ( set )
386                         elem[ name ] = value;
387
388                 return elem[ name ];
389         }
390 });