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