Another logic bug caught by furf in ad950c8c5992937640a1e1aca8d63bb476b001f6.
[jquery.git] / src / css.js
index cd77ec1..dd0ca4e 100644 (file)
@@ -1,34 +1,54 @@
-// exclude the following css properties to add px
-var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
-       ralpha = /alpha\([^)]*\)/,
+(function( jQuery ) {
+
+var ralpha = /alpha\([^)]*\)/,
        ropacity = /opacity=([^)]*)/,
-       rfloat = /float/i,
        rdashAlpha = /-([a-z])/ig,
        rupper = /([A-Z])/g,
        rnumpx = /^-?\d+(?:px)?$/i,
        rnum = /^-?\d/,
 
-       cssShow = { position: "absolute", visibility: "hidden", display:"block" },
+       cssShow = { position: "absolute", visibility: "hidden", display: "block" },
        cssWidth = [ "Left", "Right" ],
        cssHeight = [ "Top", "Bottom" ],
        curCSS,
 
        // cache check for defaultView.getComputedStyle
        getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
-       // normalize float css property
-       styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat",
+
        fcamelCase = function( all, letter ) {
                return letter.toUpperCase();
        };
 
 jQuery.fn.css = function( name, value ) {
        return jQuery.access( this, name, value, true, function( elem, name, value ) {
-               jQuery.css( elem, name, value );
+               return jQuery.css( elem, name, value );
        });
 };
 
 jQuery.extend({
-       cssHooks: {},
+       cssHooks: {
+               opacity: {
+                       get: function( elem ) {
+                               // We should always get a number back from opacity
+                               var ret = curCSS( elem, "opacity", "opacity" );
+                               return ret === "" ? "1" : ret;
+                       }
+               }
+       },
+
+       // exclude the following css properties to add px
+       cssNumber: {
+               "zIndex": true,
+               "fontWeight": true,
+               "opacity": true,
+               "zoom": true,
+               "lineHeight": true
+       },
+
+       cssProps: {
+               // normalize float css property
+               "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat"
+       },
 
        css: function( elem, name, value, force, extra ) {
                // don't set styles on text and comment nodes
@@ -36,34 +56,29 @@ jQuery.extend({
                        return undefined;
                }
 
-               // Make sure we're using the right name for getting the float value
-               if ( rfloat.test( name ) ) {
-                       name = styleFloat;
-               }
-
-               name = name.replace( rdashAlpha, fcamelCase );
+               var ret, origName = name.replace( rdashAlpha, fcamelCase ),
+                       style = elem.style || {}, hooks = jQuery.cssHooks[ origName ] || {};
 
-               var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {};
+               name = jQuery.cssProps[ origName ] || origName;
 
                if ( value !== undefined ) {
-                       if ( typeof value === "number" && !rexclude.test(name) ) {
+                       if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
                                value += "px";
                        }
 
-                       if ( !("set" in hooks) || (value = hooks.set( elem, value )) === false ) {
+                       if ( !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) {
                                style[ name ] = value;
                        }
 
                } else {
-                       if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== false ) {
+                       if ( !force && "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) {
                                return ret;
-                       }
 
-                       if ( !force && name in style ) {
+                       } else if ( !force && style[ name ] ) {
                                ret = style[ name ];
 
                        } else if ( curCSS ) {
-                               ret = curCSS( elem, name );
+                               ret = curCSS( elem, name, origName );
                        }
 
                        return ret;
@@ -92,6 +107,8 @@ jQuery.extend({
 jQuery.each(["height", "width"], function( i, name ) {
        jQuery.cssHooks[ name ] = {
                get: function( elem, force, extra ) {
+                       var val;
+
                        if ( elem.offsetWidth !== 0 ) {
                                val = getWH( elem, name, extra );
 
@@ -100,11 +117,13 @@ jQuery.each(["height", "width"], function( i, name ) {
                                        val = getWH( elem, name, extra );
                                });
                        }
+
+                       return val + "px";
                },
 
                set: function( elem, value ) {
                        // ignore negative width and height values #1599
-                       elem.style[ name ] = Math.max( parseFloat(value), 0 );
+                       return Math.max( parseFloat(value), 0 ) + "px";
                }
        };
 });
@@ -140,35 +159,25 @@ if ( !jQuery.support.opacity ) {
 }
 
 if ( getComputedStyle ) {
-       curCSS = function( elem, name ) {
+       curCSS = function( elem, newName, name ) {
                var ret, defaultView, computedStyle;
 
-               // Only "float" is needed here
-               if ( rfloat.test( name ) ) {
-                       name = "float";
-               }
-
                name = name.replace( rupper, "-$1" ).toLowerCase();
 
                if ( !(defaultView = elem.ownerDocument.defaultView) ) {
-                       return null;
+                       return undefined;
                }
 
                if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) {
                        ret = computedStyle.getPropertyValue( name );
                }
 
-               // We should always get a number back from opacity
-               if ( name === "opacity" && ret === "" ) {
-                       ret = "1";
-               }
-
                return ret;
        };
 
 } else if ( document.documentElement.currentStyle ) {
        curCSS = function( elem, name ) {
-               var left, rsLeft, ret = elem.currentStyle[ name ];
+               var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style;
 
                // From the awesome hack by Dean Edwards
                // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
@@ -204,14 +213,14 @@ function getWH( elem, name, extra ) {
 
        jQuery.each( which, function() {
                if ( !extra ) {
-                       val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true)) || 0;
+                       val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true )) || 0;
                }
 
                if ( extra === "margin" ) {
-                       val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true)) || 0;
+                       val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true )) || 0;
 
                } else {
-                       val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true)) || 0;
+                       val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true )) || 0;
                }
        });
 
@@ -234,3 +243,5 @@ if ( jQuery.expr && jQuery.expr.filters ) {
                return !jQuery.expr.filters.hidden( elem );
        };
 }
+
+})( jQuery );