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