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