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