5b6b7b9b6bf8cf07936ac1668060083f7035a984
[jquery.git] / 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.curCSS( 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                                 var 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 defaultView = elem.ownerDocument.defaultView;
145
146                         if ( !defaultView ) {
147                                 return null;
148                         }
149
150                         var computedStyle = defaultView.getComputedStyle( elem, null );
151
152                         if ( computedStyle ) {
153                                 ret = computedStyle.getPropertyValue( name );
154                         }
155
156                         // We should always get a number back from opacity
157                         if ( name === "opacity" && ret === "" ) {
158                                 ret = "1";
159                         }
160
161                 } else if ( elem.currentStyle ) {
162                         var camelCase = name.replace(rdashAlpha, fcamelCase);
163
164                         ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
165
166                         // From the awesome hack by Dean Edwards
167                         // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
168
169                         // If we're not dealing with a regular pixel number
170                         // but a number that has a weird ending, we need to convert it to pixels
171                         if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
172                                 // Remember the original values
173                                 var left = style.left, rsLeft = elem.runtimeStyle.left;
174
175                                 // Put in the new values to get a computed value out
176                                 elem.runtimeStyle.left = elem.currentStyle.left;
177                                 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
178                                 ret = style.pixelLeft + "px";
179
180                                 // Revert the changed values
181                                 style.left = left;
182                                 elem.runtimeStyle.left = rsLeft;
183                         }
184                 }
185
186                 return ret;
187         },
188
189         // A method for quickly swapping in/out CSS properties to get correct calculations
190         swap: function( elem, options, callback ) {
191                 var old = {};
192
193                 // Remember the old values, and insert the new ones
194                 for ( var name in options ) {
195                         old[ name ] = elem.style[ name ];
196                         elem.style[ name ] = options[ name ];
197                 }
198
199                 callback.call( elem );
200
201                 // Revert the old values
202                 for ( var name in options ) {
203                         elem.style[ name ] = old[ name ];
204                 }
205         }
206 });
207
208 if ( jQuery.expr && jQuery.expr.filters ) {
209         jQuery.expr.filters.hidden = function( elem ) {
210                 var width = elem.offsetWidth, height = elem.offsetHeight,
211                         skip = elem.nodeName.toLowerCase() === "tr";
212
213                 return width === 0 && height === 0 && !skip ?
214                         true :
215                         width > 0 && height > 0 && !skip ?
216                                 false :
217                                 jQuery.curCSS(elem, "display") === "none";
218         };
219
220         jQuery.expr.filters.visible = function( elem ) {
221                 return !jQuery.expr.filters.hidden( elem );
222         };
223 }