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