jQuery.css() returns a string value - handle this properly in the animation code.
[jquery.git] / src / css.js
1 (function( jQuery ) {
2
3 var ralpha = /alpha\([^)]*\)/,
4         ropacity = /opacity=([^)]*)/,
5         rdashAlpha = /-([a-z])/ig,
6         rupper = /([A-Z])/g,
7         rnumpx = /^-?\d+(?:px)?$/i,
8         rnum = /^-?\d/,
9
10         cssShow = { position: "absolute", visibility: "hidden", display: "block" },
11         cssWidth = [ "Left", "Right" ],
12         cssHeight = [ "Top", "Bottom" ],
13         curCSS,
14
15         // cache check for defaultView.getComputedStyle
16         getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
17
18         fcamelCase = function( all, letter ) {
19                 return letter.toUpperCase();
20         };
21
22 jQuery.fn.css = function( name, value ) {
23         return jQuery.access( this, name, value, true, function( elem, name, value ) {
24                 return jQuery.css( elem, name, value );
25         });
26 };
27
28 jQuery.extend({
29         // Add in style property hooks for overriding the default
30         // behavior of getting and setting a style property
31         cssHooks: {
32                 opacity: {
33                         get: function( elem ) {
34                                 // We should always get a number back from opacity
35                                 var ret = curCSS( elem, "opacity", "opacity" );
36                                 return ret === "" ? "1" : ret;
37                         }
38                 }
39         },
40
41         // Exclude the following css properties to add px
42         cssNumber: {
43                 "zIndex": true,
44                 "fontWeight": true,
45                 "opacity": true,
46                 "zoom": true,
47                 "lineHeight": true
48         },
49
50         // Add in properties whose names you wish to fix before
51         // setting or getting the value
52         cssProps: {
53                 // normalize float css property
54                 "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat"
55         },
56
57         // Get and set the style property on a DOM Node
58         style: function( elem, name, value, extra ) {
59                 // Don't set styles on text and comment nodes
60                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
61                         return undefined;
62                 }
63
64                 // Make sure that we're working with the right name
65                 var ret, origName = name.replace( rdashAlpha, fcamelCase ),
66                         style = elem.style, hooks = jQuery.cssHooks[ origName ];
67
68                 name = jQuery.cssProps[ origName ] || origName;
69
70                 // Check if we're setting a value
71                 if ( value !== undefined ) {
72                         // If a number was passed in, add 'px' to the (except for certain CSS properties)
73                         if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
74                                 value += "px";
75                         }
76
77                         // If a hook was provided, use that value, otherwise just set the specified value
78                         if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) {
79                                 style[ name ] = value;
80                         }
81
82                 } else {
83                         // If a hook was provided get the non-computed value from there
84                         if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {
85                                 return ret;
86                         }
87
88                         // Otherwise just get the value from the style object
89                         return style[ name ];
90                 }
91         },
92
93         css: function( elem, name, value, extra ) {
94                 // Make sure that we're working with the right name
95                 var ret, origName = name.replace( rdashAlpha, fcamelCase ),
96                         hooks = jQuery.cssHooks[ origName ];
97
98                 name = jQuery.cssProps[ origName ] || origName;
99
100                 // Check if we're setting a value, just use jQuery.style (DEPRECATED)
101                 if ( value !== undefined ) {
102                         jQuery.style( elem, name, value );
103
104                 // If a hook was provided get the computed value from there
105                 } else if ( hooks && "get" in hooks && (ret = hooks.get( elem, true, extra )) !== undefined ) {
106                         return ret;
107
108                 // Otherwise, if a way to get the computed value exists, use that
109                 } else if ( curCSS ) {
110                         return curCSS( elem, name, origName );
111                 }
112         },
113
114         // A method for quickly swapping in/out CSS properties to get correct calculations
115         swap: function( elem, options, callback ) {
116                 var old = {};
117
118                 // Remember the old values, and insert the new ones
119                 for ( var name in options ) {
120                         old[ name ] = elem.style[ name ];
121                         elem.style[ name ] = options[ name ];
122                 }
123
124                 callback.call( elem );
125
126                 // Revert the old values
127                 for ( name in options ) {
128                         elem.style[ name ] = old[ name ];
129                 }
130         }
131 });
132
133 jQuery.each(["height", "width"], function( i, name ) {
134         jQuery.cssHooks[ name ] = {
135                 get: function( elem, computed, extra ) {
136                         var val;
137
138                         if ( computed ) {
139                                 if ( elem.offsetWidth !== 0 ) {
140                                         val = getWH( elem, name, extra );
141
142                                 } else {
143                                         jQuery.swap( elem, cssShow, function() {
144                                                 val = getWH( elem, name, extra );
145                                         });
146                                 }
147
148                                 return val + "px";
149                         }
150                 },
151
152                 set: function( elem, value ) {
153                         if ( value !== "" ) {
154                                 // ignore negative width and height values #1599
155                                 value = parseFloat(value);
156
157                                 if ( value >= 0 ) {
158                                         return value + "px";
159                                 }
160
161                         } else {
162                                 return value;
163                         }
164                 }
165         };
166 });
167
168 if ( !jQuery.support.opacity ) {
169         jQuery.cssHooks.opacity = {
170                 get: function( elem, computed ) {
171                         // IE uses filters for opacity
172                         return ropacity.test((computed ? elem.currentStyle.filter : elem.style.filter) || "") ?
173                                 (parseFloat(RegExp.$1) / 100) + "" :
174                                 "1";
175                 },
176
177                 set: function( elem, value ) {
178                         var style = elem.style;
179
180                         // IE has trouble with opacity if it does not have layout
181                         // Force it by setting the zoom level
182                         style.zoom = 1;
183
184                         // Set the alpha filter to set the opacity
185                         var opacity = parseInt( value, 10 ) + "" === "NaN" ?
186                                 "" :
187                                 "alpha(opacity=" + value * 100 + ")";
188
189                         var filter = style.filter || elem.currentStyle.filter || "";
190
191                         style.filter = ralpha.test(filter) ?
192                                 filter.replace(ralpha, opacity) :
193                                 opacity;
194                 }
195         };
196 }
197
198 if ( getComputedStyle ) {
199         curCSS = function( elem, newName, name ) {
200                 var ret, defaultView, computedStyle;
201
202                 name = name.replace( rupper, "-$1" ).toLowerCase();
203
204                 if ( !(defaultView = elem.ownerDocument.defaultView) ) {
205                         return undefined;
206                 }
207
208                 if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
209                         ret = computedStyle.getPropertyValue( name );
210                 }
211
212                 return ret;
213         };
214
215 } else if ( document.documentElement.currentStyle ) {
216         curCSS = function( elem, name ) {
217                 var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style;
218
219                 // From the awesome hack by Dean Edwards
220                 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
221
222                 // If we're not dealing with a regular pixel number
223                 // but a number that has a weird ending, we need to convert it to pixels
224                 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
225                         // Remember the original values
226                         left = style.left;
227                         rsLeft = elem.runtimeStyle.left;
228
229                         // Put in the new values to get a computed value out
230                         elem.runtimeStyle.left = elem.currentStyle.left;
231                         style.left = name === "fontSize" ? "1em" : (ret || 0);
232                         ret = style.pixelLeft + "px";
233
234                         // Revert the changed values
235                         style.left = left;
236                         elem.runtimeStyle.left = rsLeft;
237                 }
238
239                 return ret;
240         };
241 }
242
243 function getWH( elem, name, extra ) {
244         var which = name === "width" ? cssWidth : cssHeight,
245                 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
246
247         if ( extra === "border" ) {
248                 return val;
249         }
250
251         jQuery.each( which, function() {
252                 if ( !extra ) {
253                         val -= parseFloat(jQuery.css( elem, "padding" + this )) || 0;
254                 }
255
256                 if ( extra === "margin" ) {
257                         val += parseFloat(jQuery.css( elem, "margin" + this )) || 0;
258
259                 } else {
260                         val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0;
261                 }
262         });
263
264         return val;
265 }
266
267 if ( jQuery.expr && jQuery.expr.filters ) {
268         jQuery.expr.filters.hidden = function( elem ) {
269                 var width = elem.offsetWidth, height = elem.offsetHeight,
270                         skip = elem.nodeName.toLowerCase() === "tr";
271
272                 return width === 0 && height === 0 && !skip ?
273                         true :
274                         width > 0 && height > 0 && !skip ?
275                                 false :
276                                 (elem.style.display || jQuery.css( elem, "display" )) === "none";
277         };
278
279         jQuery.expr.filters.visible = function( elem ) {
280                 return !jQuery.expr.filters.hidden( elem );
281         };
282 }
283
284 })( jQuery );