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