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