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