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