8ea815c8e5ccc30eef8637ac713dc047e72ff12e
[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 = name.replace( rdashAlpha, fcamelCase ),
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 = name.replace( rdashAlpha, fcamelCase ),
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
131 // DEPRECATED, Use jQuery.css() instead
132 jQuery.curCSS = jQuery.css;
133
134 jQuery.each(["height", "width"], function( i, name ) {
135         jQuery.cssHooks[ name ] = {
136                 get: function( elem, computed, extra ) {
137                         var val;
138
139                         if ( computed ) {
140                                 if ( elem.offsetWidth !== 0 ) {
141                                         val = getWH( elem, name, extra );
142
143                                 } else {
144                                         jQuery.swap( elem, cssShow, function() {
145                                                 val = getWH( elem, name, extra );
146                                         });
147                                 }
148
149                                 return val + "px";
150                         }
151                 },
152
153                 set: function( elem, value ) {
154                         if ( value !== "" ) {
155                                 // ignore negative width and height values #1599
156                                 value = parseFloat(value);
157
158                                 if ( value >= 0 ) {
159                                         return value + "px";
160                                 }
161
162                         } else {
163                                 return value;
164                         }
165                 }
166         };
167 });
168
169 if ( !jQuery.support.opacity ) {
170         jQuery.cssHooks.opacity = {
171                 get: function( elem, computed ) {
172                         // IE uses filters for opacity
173                         return ropacity.test((computed ? elem.currentStyle.filter : elem.style.filter) || "") ?
174                                 (parseFloat(RegExp.$1) / 100) + "" :
175                                 "1";
176                 },
177
178                 set: function( elem, value ) {
179                         var style = elem.style;
180
181                         // IE has trouble with opacity if it does not have layout
182                         // Force it by setting the zoom level
183                         style.zoom = 1;
184
185                         // Set the alpha filter to set the opacity
186                         var opacity = parseInt( value, 10 ) + "" === "NaN" ?
187                                 "" :
188                                 "alpha(opacity=" + value * 100 + ")";
189
190                         var filter = style.filter || elem.currentStyle.filter || "";
191
192                         style.filter = ralpha.test(filter) ?
193                                 filter.replace(ralpha, opacity) :
194                                 opacity;
195                 }
196         };
197 }
198
199 if ( getComputedStyle ) {
200         curCSS = function( elem, newName, name ) {
201                 var ret, defaultView, computedStyle;
202
203                 name = name.replace( rupper, "-$1" ).toLowerCase();
204
205                 if ( !(defaultView = elem.ownerDocument.defaultView) ) {
206                         return undefined;
207                 }
208
209                 if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
210                         ret = computedStyle.getPropertyValue( name );
211                 }
212
213                 return ret;
214         };
215
216 } else if ( document.documentElement.currentStyle ) {
217         curCSS = function( elem, name ) {
218                 var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style;
219
220                 // From the awesome hack by Dean Edwards
221                 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
222
223                 // If we're not dealing with a regular pixel number
224                 // but a number that has a weird ending, we need to convert it to pixels
225                 if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
226                         // Remember the original values
227                         left = style.left;
228                         rsLeft = elem.runtimeStyle.left;
229
230                         // Put in the new values to get a computed value out
231                         elem.runtimeStyle.left = elem.currentStyle.left;
232                         style.left = name === "fontSize" ? "1em" : (ret || 0);
233                         ret = style.pixelLeft + "px";
234
235                         // Revert the changed values
236                         style.left = left;
237                         elem.runtimeStyle.left = rsLeft;
238                 }
239
240                 return ret;
241         };
242 }
243
244 function getWH( elem, name, extra ) {
245         var which = name === "width" ? cssWidth : cssHeight,
246                 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
247
248         if ( extra === "border" ) {
249                 return val;
250         }
251
252         jQuery.each( which, function() {
253                 if ( !extra ) {
254                         val -= parseFloat(jQuery.css( elem, "padding" + this )) || 0;
255                 }
256
257                 if ( extra === "margin" ) {
258                         val += parseFloat(jQuery.css( elem, "margin" + this )) || 0;
259
260                 } else {
261                         val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0;
262                 }
263         });
264
265         return val;
266 }
267
268 if ( jQuery.expr && jQuery.expr.filters ) {
269         jQuery.expr.filters.hidden = function( elem ) {
270                 var width = elem.offsetWidth, height = elem.offsetHeight,
271                         skip = elem.nodeName.toLowerCase() === "tr";
272
273                 return width === 0 && height === 0 && !skip ?
274                         true :
275                         width > 0 && height > 0 && !skip ?
276                                 false :
277                                 (elem.style.display || jQuery.css( elem, "display" )) === "none";
278         };
279
280         jQuery.expr.filters.visible = function( elem ) {
281                 return !jQuery.expr.filters.hidden( elem );
282         };
283 }
284
285 })( jQuery );