Broke more of the property-specific CSS logic out of the jQuery.css() function.
[jquery.git] / src / css.js
index 5d50d32..2da19b9 100644 (file)
-// exclude the following css properties to add px
-var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
-       ralpha = /alpha\([^)]*\)/,
+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 ) {
-               if ( value === undefined ) {
-                       return jQuery.curCSS( elem, name );
-               }
-
-               if ( typeof value === "number" && !rexclude.test(name) ) {
-                       value += "px";
-               }
-
-               jQuery.style( elem, name, value );
+               jQuery.css( elem, name, value );
        });
 };
 
 jQuery.extend({
-       cssHooks: {},
-
-       style: function( elem, name, value ) {
-               // don't set styles on text and comment nodes
-               if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
-                       return undefined;
+       cssHooks: {
+               opacity: {
+                       get: function( elem ) {
+                               // We should always get a number back from opacity
+                               var ret = curCSS( elem, "opacity", "opacity" );
+                               return ret === "" ? "1" : ret;
+                       }
                }
+       },
 
-               // ignore negative width and height values #1599
-               if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) {
-                       value = undefined;
-               }
+       // exclude the following css properties to add px
+       cssNumber: {
+               "zIndex": true,
+               "fontWeight": true,
+               "opacity": true,
+               "zoom": true,
+               "lineHeight": true
+       },
 
-               var style = elem.style || elem, set = value !== undefined;
+       cssProps: {
+               // normalize float css property
+               "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat"
+       },
 
-               // Make sure we're using the right name for getting the float value
-               if ( rfloat.test( name ) ) {
-                       name = styleFloat;
+       css: function( elem, name, value, force, extra ) {
+               // don't set styles on text and comment nodes
+               if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
+                       return undefined;
                }
 
-               name = name.replace(rdashAlpha, fcamelCase);
-
-               var hooks = jQuery.cssHooks[name] || {};
+               var ret, origName = name.replace( rdashAlpha, fcamelCase ),
+                       style = elem.style || {}, hooks = jQuery.cssHooks[ origName ] || {};
 
-               if ( set && (!("set" in hooks) || hooks.set( elem, value ) === false) ) {
-                       style[ name ] = value;
-               }
+               name = jQuery.cssProps[ origName ] || origName;
 
-               if ( "get" in hooks ) {
-                       var cssHookReturn = hooks.get( elem, false );
-                       if ( cssHookReturn !== false ) {
-                               return cssHookReturn;
+               if ( value !== undefined ) {
+                       if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
+                               value += "px";
                        }
-               }
 
-               return style[ name ];
-       },
-
-       css: function( elem, name, force, extra ) {
-               if ( name === "width" || name === "height" ) {
-                       if ( elem.offsetWidth !== 0 ) {
-                               val = getWH( elem, name, extra );
-
-                       } else {
-                               jQuery.swap( elem, cssShow, function() {
-                                       val = getWH( elem, name, extra );
-                               });
+                       if ( !("set" in hooks) || (value = hooks.set( elem, value )) === false ) {
+                               style[ name ] = value;
                        }
 
-                       return Math.max(0, Math.round(val));
-               }
-
-               return jQuery.curCSS( elem, name, force );
-       },
+               } else {
+                       if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== false ) {
+                               return ret;
+                       }
 
-       curCSS: function( elem, name, force ) {
-               var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {};
+                       if ( !force && name in style ) {
+                               ret = style[ name ];
 
-               // Make sure we're using the right name for getting the float value
-               if ( rfloat.test( name ) ) {
-                       name = styleFloat;
-               }
+                       } else if ( curCSS ) {
+                               ret = curCSS( elem, name, origName );
+                       }
 
-               if ( "get" in hooks && (ret = hooks.get( elem, force )) !== false ) {
                        return ret;
                }
-
-               if ( !force && name in style ) {
-                       ret = style[ name ];
-
-               } else if ( curCSS ) {
-                       ret = curCSS( elem, name );
-               }
-
-               return ret;
        },
 
        // A method for quickly swapping in/out CSS properties to get correct calculations
@@ -132,6 +103,26 @@ jQuery.extend({
        }
 });
 
+jQuery.each(["height", "width"], function( i, name ) {
+       jQuery.cssHooks[ name ] = {
+               get: function( elem, force, extra ) {
+                       if ( elem.offsetWidth !== 0 ) {
+                               val = getWH( elem, name, extra );
+
+                       } else {
+                               jQuery.swap( elem, cssShow, function() {
+                                       val = getWH( elem, name, extra );
+                               });
+                       }
+               },
+
+               set: function( elem, value ) {
+                       // ignore negative width and height values #1599
+                       elem.style[ name ] = Math.max( parseFloat(value), 0 ) + "px";
+               }
+       };
+});
+
 if ( !jQuery.support.opacity ) {
        jQuery.cssHooks.opacity = {
                get: function( elem, force ) {
@@ -153,7 +144,7 @@ if ( !jQuery.support.opacity ) {
                                "" :
                                "alpha(opacity=" + value * 100 + ")";
 
-                       var filter = style.filter || jQuery.curCSS( elem, "filter" ) || "";
+                       var filter = style.filter || jQuery.css( elem, "filter" ) || "";
 
                        style.filter = ralpha.test(filter) ?
                                filter.replace(ralpha, opacity) :
@@ -163,36 +154,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, camelCase = name.replace(rdashAlpha, fcamelCase),
-                       ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ];
+               var left, rsLeft, ret = elem.currentStyle[ name ];
 
                // From the awesome hack by Dean Edwards
                // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
@@ -206,7 +186,7 @@ if ( getComputedStyle ) {
 
                        // Put in the new values to get a computed value out
                        elem.runtimeStyle.left = elem.currentStyle.left;
-                       style.left = camelCase === "fontSize" ? "1em" : (ret || 0);
+                       style.left = name === "fontSize" ? "1em" : (ret || 0);
                        ret = style.pixelLeft + "px";
 
                        // Revert the changed values
@@ -228,14 +208,14 @@ function getWH( elem, name, extra ) {
 
        jQuery.each( which, function() {
                if ( !extra ) {
-                       val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
+                       val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true)) || 0;
                }
 
                if ( extra === "margin" ) {
-                       val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
+                       val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true)) || 0;
 
                } else {
-                       val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
+                       val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true)) || 0;
                }
        });
 
@@ -251,7 +231,7 @@ if ( jQuery.expr && jQuery.expr.filters ) {
                        true :
                        width > 0 && height > 0 && !skip ?
                                false :
-                               jQuery.curCSS(elem, "display") === "none";
+                               jQuery.css(elem, "display") === "none";
        };
 
        jQuery.expr.filters.visible = function( elem ) {