Standardizing on .test() and .exec() - moving away from using .match() for RegExp...
[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
8 jQuery.fn.css = function( name, value ) {
9         var options = name, isFunction = jQuery.isFunction( value );
10
11         if ( typeof name === "string" ) {
12                 // Are we setting the style?
13                 if ( value === undefined ) {
14                         return this.length ?
15                                 jQuery.css( this[0], name ) :
16                                 null;
17
18                 // Convert name, value params to options hash format
19                 } else {
20                         options = {};
21                         options[ name ] = value;
22                 }
23         }
24         
25         var isFunction = {};
26         
27         // For each value, determine whether it's a Function so we don't
28         // need to determine it again for each element
29         for ( var prop in options )
30                 isFunction[prop] = jQuery.isFunction( options[prop] );
31
32         // For each element...
33         for ( var i = 0, l = this.length; i < l; i++ ) {
34                 var elem = this[i];
35
36                 // Set all the styles
37                 for ( var prop in options ) {
38                         value = options[prop];
39
40                         if ( isFunction[prop] ) value = value.call( elem, i );
41
42                         if ( typeof value === "number" && !exclude.test(prop) ) {
43                                 value = value + "px";
44                         }
45
46                         jQuery.style( elem, prop, value );
47                 }
48         }
49
50         return this;
51 };
52
53 jQuery.extend({
54         style: function( elem, name, value ) {
55                 // don't set styles on text and comment nodes
56                 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
57                         return undefined;
58
59                 // ignore negative width and height values #1599
60                 if ( (name == 'width' || name == 'height') && parseFloat(value) < 0 )
61                         value = undefined;
62
63                 var style = elem.style || elem, set = value !== undefined;
64
65                 // IE uses filters for opacity
66                 if ( !jQuery.support.opacity && name == "opacity" ) {
67                         if ( set ) {
68                                 // IE has trouble with opacity if it does not have layout
69                                 // Force it by setting the zoom level
70                                 style.zoom = 1;
71
72                                 // Set the alpha filter to set the opacity
73                                 style.filter = (style.filter || "").replace( /alpha\([^)]*\)/, "" ) +
74                                         (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
75                         }
76
77                         return style.filter && style.filter.indexOf("opacity=") >= 0 ?
78                                 (parseFloat( /opacity=([^)]*)/.exec(style.filter)[1] ) / 100) + '':
79                                 "";
80                 }
81
82                 // Make sure we're using the right name for getting the float value
83                 if ( /float/i.test( name ) )
84                         name = styleFloat;
85
86                 name = name.replace(/-([a-z])/ig, function(all, letter){
87                         return letter.toUpperCase();
88                 });
89
90                 if ( set )
91                         style[ name ] = value;
92
93                 return style[ name ];
94         },
95
96         css: function( elem, name, force, extra ) {
97                 if ( name == "width" || name == "height" ) {
98                         var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
99
100                         function getWH() {
101                                 val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
102
103                                 if ( extra === "border" )
104                                         return;
105
106                                 jQuery.each( which, function() {
107                                         if ( !extra )
108                                                 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
109                                         if ( extra === "margin" )
110                                                 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
111                                         else
112                                                 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
113                                 });
114                         }
115
116                         if ( elem.offsetWidth !== 0 )
117                                 getWH();
118                         else
119                                 jQuery.swap( elem, props, getWH );
120
121                         return Math.max(0, Math.round(val));
122                 }
123
124                 return jQuery.curCSS( elem, name, force );
125         },
126
127         curCSS: function( elem, name, force ) {
128                 var ret, style = elem.style, filter;
129
130                 // IE uses filters for opacity
131                 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
132                         ret = /opacity=([^)]*)/.test(elem.currentStyle.filter || "") ?
133                                 (parseFloat(RegExp.$1) / 100) + "" :
134                                 "";
135
136                         return ret === "" ?
137                                 "1" :
138                                 ret;
139                 }
140
141                 // Make sure we're using the right name for getting the float value
142                 if ( /float/i.test( name ) )
143                         name = styleFloat;
144
145                 if ( !force && style && style[ name ] ) {
146                         ret = style[ name ];
147
148                 } else if ( getComputedStyle ) {
149
150                         // Only "float" is needed here
151                         if ( /float/i.test( name ) )
152                                 name = "float";
153
154                         name = name.replace( /([A-Z])/g, "-$1" ).toLowerCase();
155
156                         var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
157
158                         if ( computedStyle )
159                                 ret = computedStyle.getPropertyValue( name );
160
161                         // We should always get a number back from opacity
162                         if ( name == "opacity" && ret == "" )
163                                 ret = "1";
164
165                 } else if ( elem.currentStyle ) {
166                         var camelCase = name.replace(/\-(\w)/g, function(all, letter){
167                                 return letter.toUpperCase();
168                         });
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 ( !/^\d+(px)?$/i.test( ret ) && /^\d/.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 = 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                 // Remember the old values, and insert the new ones
199                 for ( var name in options ) {
200                         old[ name ] = elem.style[ name ];
201                         elem.style[ name ] = options[ name ];
202                 }
203
204                 callback.call( elem );
205
206                 // Revert the old values
207                 for ( var name in options )
208                         elem.style[ name ] = old[ name ];
209         }
210 });