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