X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcss.js;h=99cb73581cd4430aeeb8bd2696b364514e0f849d;hb=2ca36598954759c5b5dce569a39c52b981ed4ab2;hp=87f9abd70733903e81b1bd78dc536e6be738c327;hpb=37b607d2815b893d13de4ac3461090d0dd46535e;p=jquery.git diff --git a/src/css.js b/src/css.js index 87f9abd..99cb735 100644 --- a/src/css.js +++ b/src/css.js @@ -1,6 +1,6 @@ (function( jQuery ) { -var ralpha = /alpha\([^)]*\)/, +var ralpha = /alpha\([^)]*\)/i, ropacity = /opacity=([^)]*)/, rdashAlpha = /-([a-z])/ig, rupper = /([A-Z])/g, @@ -20,8 +20,15 @@ var ralpha = /alpha\([^)]*\)/, }; jQuery.fn.css = function( name, value ) { + // Setting 'undefined' is a no-op + if ( arguments.length === 2 && value === undefined ) { + return this; + } + return jQuery.access( this, name, value, true, function( elem, name, value ) { - return jQuery.css( elem, name, value ); + return value !== undefined ? + jQuery.style( elem, name, value ) : + jQuery.css( elem, name ); }); }; @@ -30,10 +37,15 @@ jQuery.extend({ // behavior of getting and setting a style property cssHooks: { opacity: { - get: function( elem ) { - // We should always get a number back from opacity - var ret = curCSS( elem, "opacity", "opacity" ); - return ret === "" ? "1" : ret; + get: function( elem, computed ) { + if ( computed ) { + // We should always get a number back from opacity + var ret = curCSS( elem, "opacity", "opacity" ); + return ret === "" ? "1" : ret; + + } else { + return elem.style.opacity; + } } } }, @@ -62,7 +74,7 @@ jQuery.extend({ } // Make sure that we're working with the right name - var ret, origName = name.replace( rdashAlpha, fcamelCase ), + var ret, origName = jQuery.camelCase( name ), style = elem.style, hooks = jQuery.cssHooks[ origName ]; name = jQuery.cssProps[ origName ] || origName; @@ -76,7 +88,11 @@ jQuery.extend({ // If a hook was provided, use that value, otherwise just set the specified value if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) { - style[ name ] = value; + // Wrapped to prevent IE from throwing errors when 'invalid' values are provided + // Fixes bug #5509 + try { + style[ name ] = value; + } catch(e) {} } } else { @@ -90,19 +106,15 @@ jQuery.extend({ } }, - css: function( elem, name, value, extra ) { + css: function( elem, name, extra ) { // Make sure that we're working with the right name - var ret, origName = name.replace( rdashAlpha, fcamelCase ), + var ret, origName = jQuery.camelCase( name ), hooks = jQuery.cssHooks[ origName ]; name = jQuery.cssProps[ origName ] || origName; - // Check if we're setting a value, just use jQuery.style (DEPRECATED) - if ( value !== undefined ) { - jQuery.style( elem, name, value ); - // If a hook was provided get the computed value from there - } else if ( hooks && "get" in hooks && (ret = hooks.get( elem, true, extra )) !== undefined ) { + if ( hooks && "get" in hooks && (ret = hooks.get( elem, true, extra )) !== undefined ) { return ret; // Otherwise, if a way to get the computed value exists, use that @@ -127,9 +139,16 @@ jQuery.extend({ for ( name in options ) { elem.style[ name ] = old[ name ]; } + }, + + camelCase: function( string ) { + return string.replace( rdashAlpha, fcamelCase ); } }); +// DEPRECATED, Use jQuery.css() instead +jQuery.curCSS = jQuery.css; + jQuery.each(["height", "width"], function( i, name ) { jQuery.cssHooks[ name ] = { get: function( elem, computed, extra ) { @@ -150,11 +169,16 @@ jQuery.each(["height", "width"], function( i, name ) { }, set: function( elem, value ) { - // ignore negative width and height values #1599 - value = parseFloat(value); + if ( rnumpx.test( value ) ) { + // ignore negative width and height values #1599 + value = parseFloat(value); - if ( value >= 0 ) { - return value + "px"; + if ( value >= 0 ) { + return value + "px"; + } + + } else { + return value; } } }; @@ -164,9 +188,9 @@ if ( !jQuery.support.opacity ) { jQuery.cssHooks.opacity = { get: function( elem, computed ) { // IE uses filters for opacity - return ropacity.test((computed ? elem.currentStyle.filter : elem.style.filter) || "") ? + return ropacity.test((computed && elem.currentStyle ? elem.currentStyle.filter : elem.style.filter) || "") ? (parseFloat(RegExp.$1) / 100) + "" : - "1"; + computed ? "1" : ""; }, set: function( elem, value ) { @@ -177,15 +201,14 @@ if ( !jQuery.support.opacity ) { style.zoom = 1; // Set the alpha filter to set the opacity - var opacity = parseInt( value, 10 ) + "" === "NaN" ? + var opacity = jQuery.isNaN(value) ? "" : - "alpha(opacity=" + value * 100 + ")"; - - var filter = style.filter || elem.currentStyle.filter || ""; + "alpha(opacity=" + value * 100 + ")", + filter = style.filter || ""; style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : - opacity; + style.filter + ' ' + opacity; } }; } @@ -209,7 +232,7 @@ if ( getComputedStyle ) { } else if ( document.documentElement.currentStyle ) { curCSS = function( elem, name ) { - var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style; + var left, rsLeft, ret = elem.currentStyle && 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