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