Another logic bug caught by furf in ad950c8c5992937640a1e1aca8d63bb476b001f6.
[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         cssHooks: {
30                 opacity: {
31                         get: function( elem ) {
32                                 // We should always get a number back from opacity
33                                 var ret = curCSS( elem, "opacity", "opacity" );
34                                 return ret === "" ? "1" : ret;
35                         }
36                 }
37         },
38
39         // exclude the following css properties to add px
40         cssNumber: {
41                 "zIndex": true,
42                 "fontWeight": true,
43                 "opacity": true,
44                 "zoom": true,
45                 "lineHeight": true
46         },
47
48         cssProps: {
49                 // normalize float css property
50                 "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat"
51         },
52
53         css: function( elem, name, value, force, extra ) {
54                 // don't set styles on text and comment nodes
55                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
56                         return undefined;
57                 }
58
59                 var ret, origName = name.replace( rdashAlpha, fcamelCase ),
60                         style = elem.style || {}, hooks = jQuery.cssHooks[ origName ] || {};
61
62                 name = jQuery.cssProps[ origName ] || origName;
63
64                 if ( value !== undefined ) {
65                         if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
66                                 value += "px";
67                         }
68
69                         if ( !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) {
70                                 style[ name ] = value;
71                         }
72
73                 } else {
74                         if ( !force && "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) {
75                                 return ret;
76
77                         } else if ( !force && style[ name ] ) {
78                                 ret = style[ name ];
79
80                         } else if ( curCSS ) {
81                                 ret = curCSS( elem, name, origName );
82                         }
83
84                         return ret;
85                 }
86         },
87
88         // A method for quickly swapping in/out CSS properties to get correct calculations
89         swap: function( elem, options, callback ) {
90                 var old = {};
91
92                 // Remember the old values, and insert the new ones
93                 for ( var name in options ) {
94                         old[ name ] = elem.style[ name ];
95                         elem.style[ name ] = options[ name ];
96                 }
97
98                 callback.call( elem );
99
100                 // Revert the old values
101                 for ( name in options ) {
102                         elem.style[ name ] = old[ name ];
103                 }
104         }
105 });
106
107 jQuery.each(["height", "width"], function( i, name ) {
108         jQuery.cssHooks[ name ] = {
109                 get: function( elem, force, extra ) {
110                         var val;
111
112                         if ( elem.offsetWidth !== 0 ) {
113                                 val = getWH( elem, name, extra );
114
115                         } else {
116                                 jQuery.swap( elem, cssShow, function() {
117                                         val = getWH( elem, name, extra );
118                                 });
119                         }
120
121                         return val + "px";
122                 },
123
124                 set: function( elem, value ) {
125                         // ignore negative width and height values #1599
126                         return Math.max( parseFloat(value), 0 ) + "px";
127                 }
128         };
129 });
130
131 if ( !jQuery.support.opacity ) {
132         jQuery.cssHooks.opacity = {
133                 get: function( elem, force ) {
134                         // IE uses filters for opacity
135                         return ropacity.test(elem.currentStyle.filter || "") ?
136                                 (parseFloat(RegExp.$1) / 100) + "" :
137                                 "1";
138                 },
139
140                 set: function( elem, value ) {
141                         var style = elem.style;
142
143                         // IE has trouble with opacity if it does not have layout
144                         // Force it by setting the zoom level
145                         style.zoom = 1;
146
147                         // Set the alpha filter to set the opacity
148                         var opacity = parseInt( value, 10 ) + "" === "NaN" ?
149                                 "" :
150                                 "alpha(opacity=" + value * 100 + ")";
151
152                         var filter = style.filter || jQuery.css( elem, "filter" ) || "";
153
154                         style.filter = ralpha.test(filter) ?
155                                 filter.replace(ralpha, opacity) :
156                                 opacity;
157                 }
158         };
159 }
160
161 if ( getComputedStyle ) {
162         curCSS = function( elem, newName, name ) {
163                 var ret, defaultView, computedStyle;
164
165                 name = name.replace( rupper, "-$1" ).toLowerCase();
166
167                 if ( !(defaultView = elem.ownerDocument.defaultView) ) {
168                         return undefined;
169                 }
170
171                 if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
172                         ret = computedStyle.getPropertyValue( name );
173                 }
174
175                 return ret;
176         };
177
178 } else if ( document.documentElement.currentStyle ) {
179         curCSS = function( elem, name ) {
180                 var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style;
181
182                 // From the awesome hack by Dean Edwards
183                 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
184
185                 // If we're not dealing with a regular pixel number
186                 // but a number that has a weird ending, we need to convert it to pixels
187                 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
188                         // Remember the original values
189                         left = style.left;
190                         rsLeft = elem.runtimeStyle.left;
191
192                         // Put in the new values to get a computed value out
193                         elem.runtimeStyle.left = elem.currentStyle.left;
194                         style.left = name === "fontSize" ? "1em" : (ret || 0);
195                         ret = style.pixelLeft + "px";
196
197                         // Revert the changed values
198                         style.left = left;
199                         elem.runtimeStyle.left = rsLeft;
200                 }
201
202                 return ret;
203         };
204 }
205
206 function getWH( elem, name, extra ) {
207         var which = name === "width" ? cssWidth : cssHeight,
208                 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
209
210         if ( extra === "border" ) {
211                 return val;
212         }
213
214         jQuery.each( which, function() {
215                 if ( !extra ) {
216                         val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true )) || 0;
217                 }
218
219                 if ( extra === "margin" ) {
220                         val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true )) || 0;
221
222                 } else {
223                         val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true )) || 0;
224                 }
225         });
226
227         return val;
228 }
229
230 if ( jQuery.expr && jQuery.expr.filters ) {
231         jQuery.expr.filters.hidden = function( elem ) {
232                 var width = elem.offsetWidth, height = elem.offsetHeight,
233                         skip = elem.nodeName.toLowerCase() === "tr";
234
235                 return width === 0 && height === 0 && !skip ?
236                         true :
237                         width > 0 && height > 0 && !skip ?
238                                 false :
239                                 jQuery.css(elem, "display") === "none";
240         };
241
242         jQuery.expr.filters.visible = function( elem ) {
243                 return !jQuery.expr.filters.hidden( elem );
244         };
245 }
246
247 })( jQuery );