Made isObjectLiteral to work correctly with custom objects with empty prototypes.
[jquery.git] / src / css.js
blobddd42b4b2c526f0a75f2f325d6229bbbebbdb0ee
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/,
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         };
19 jQuery.fn.css = function( name, value ) {
20         return access( this, name, value, true, function( elem, name, value ) {
21                 if (value === undefined) {
22                         return jQuery.css( elem, name );
23                 }
24                 
25                 if ( typeof value === "number" && !rexclude.test(name) ) {
26                         value += "px";
27                 }
29                 jQuery.style( elem, name, value );
30         });
33 jQuery.extend({
34         style: function( elem, name, value ) {
35                 // don't set styles on text and comment nodes
36                 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
37                         return undefined;
38                 }
40                 // ignore negative width and height values #1599
41                 if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
42                         value = undefined;
43                 }
45                 var style = elem.style || elem, set = value !== undefined;
47                 // IE uses filters for opacity
48                 if ( !jQuery.support.opacity && name === "opacity" ) {
49                         if ( set ) {
50                                 // IE has trouble with opacity if it does not have layout
51                                 // Force it by setting the zoom level
52                                 style.zoom = 1;
54                                 // Set the alpha filter to set the opacity
55                                 style.filter = (style.filter || "").replace( ralpha, "" ) +
56                                         (parseInt( value ) + '' === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
57                         }
59                         return style.filter && style.filter.indexOf("opacity=") >= 0 ?
60                                 (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + '':
61                                 "";
62                 }
64                 // Make sure we're using the right name for getting the float value
65                 if ( rfloat.test( name ) ) {
66                         name = styleFloat;
67                 }
69                 name = name.replace(rdashAlpha, fcamelCase);
71                 if ( set ) {
72                         style[ name ] = value;
73                 }
75                 return style[ name ];
76         },
78         css: function( elem, name, force, extra ) {
79                 if ( name === "width" || name === "height" ) {
80                         var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name === "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
82                         function getWH() {
83                                 val = name === "width" ? elem.offsetWidth : elem.offsetHeight;
85                                 if ( extra === "border" ) { return; }
87                                 jQuery.each( which, function() {
88                                         if ( !extra ) {
89                                                 val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
90                                         }
92                                         if ( extra === "margin" ) {
93                                                 val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
94                                         } else {
95                                                 val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
96                                         }
97                                 });
98                         }
100                         if ( elem.offsetWidth !== 0 ) {
101                                 getWH();
102                         } else {
103                                 jQuery.swap( elem, props, getWH );
104                         }
106                         return Math.max(0, Math.round(val));
107                 }
109                 return jQuery.curCSS( elem, name, force );
110         },
112         curCSS: function( elem, name, force ) {
113                 var ret, style = elem.style, filter;
115                 // IE uses filters for opacity
116                 if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) {
117                         ret = ropacity.test(elem.currentStyle.filter || "") ?
118                                 (parseFloat(RegExp.$1) / 100) + "" :
119                                 "";
121                         return ret === "" ?
122                                 "1" :
123                                 ret;
124                 }
126                 // Make sure we're using the right name for getting the float value
127                 if ( rfloat.test( name ) ) {
128                         name = styleFloat;
129                 }
131                 if ( !force && style && style[ name ] ) {
132                         ret = style[ name ];
134                 } else if ( getComputedStyle ) {
136                         // Only "float" is needed here
137                         if ( rfloat.test( name ) ) {
138                                 name = "float";
139                         }
141                         name = name.replace( rupper, "-$1" ).toLowerCase();
143                         var computedStyle = elem.ownerDocument.defaultView.getComputedStyle( elem, null );
145                         if ( computedStyle ) {
146                                 ret = computedStyle.getPropertyValue( name );
147                         }
149                         // We should always get a number back from opacity
150                         if ( name === "opacity" && ret === "" ) {
151                                 ret = "1";
152                         }
154                 } else if ( elem.currentStyle ) {
155                         var camelCase = name.replace(rdashAlpha, fcamelCase);
157                         ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
159                         // From the awesome hack by Dean Edwards
160                         // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
162                         // If we're not dealing with a regular pixel number
163                         // but a number that has a weird ending, we need to convert it to pixels
164                         if ( !rnumpx.test( ret ) && rnum.test( ret ) ) {
165                                 // Remember the original values
166                                 var left = style.left, rsLeft = elem.runtimeStyle.left;
168                                 // Put in the new values to get a computed value out
169                                 elem.runtimeStyle.left = elem.currentStyle.left;
170                                 style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
171                                 ret = style.pixelLeft + "px";
173                                 // Revert the changed values
174                                 style.left = left;
175                                 elem.runtimeStyle.left = rsLeft;
176                         }
177                 }
179                 return ret;
180         },
182         // A method for quickly swapping in/out CSS properties to get correct calculations
183         swap: function( elem, options, callback ) {
184                 var old = {};
186                 // Remember the old values, and insert the new ones
187                 for ( var name in options ) {
188                         old[ name ] = elem.style[ name ];
189                         elem.style[ name ] = options[ name ];
190                 }
192                 callback.call( elem );
194                 // Revert the old values
195                 for ( var name in options ) {
196                         elem.style[ name ] = old[ name ];
197                 }
198         }
201 if ( jQuery.expr && jQuery.expr.filters ) {
202         jQuery.expr.filters.hidden = function(elem){
203                 var width = elem.offsetWidth, height = elem.offsetHeight,
204                          force = /^tr$/i.test( elem.nodeName ); // ticket #4512
205                 return ( width === 0 && height === 0 && !force ) ?
206                         true :
207                                 ( width !== 0 && height !== 0 && !force ) ?
208                                         false :
209                                                 !!( jQuery.curCSS(elem, "display") === "none" );
210         };
212         jQuery.expr.filters.visible = function(elem){
213                 return !jQuery.expr.filters.hidden(elem);
214         };