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