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