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