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