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