Make sure null/NaN values aren't set in .css(). Fixes #7116.
[jquery.git] / src / css.js
index 07ff686..caf4985 100644 (file)
@@ -1,6 +1,6 @@
 (function( jQuery ) {
 
-var ralpha = /alpha\([^)]*\)/,
+var ralpha = /alpha\([^)]*\)/i,
        ropacity = /opacity=([^)]*)/,
        rdashAlpha = /-([a-z])/ig,
        rupper = /([A-Z])/g,
@@ -20,6 +20,11 @@ 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 value !== undefined ?
                        jQuery.style( elem, name, value ) :
@@ -65,7 +70,7 @@ jQuery.extend({
        style: function( elem, name, value, extra ) {
                // Don't set styles on text and comment nodes
                if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
-                       return undefined;
+                       return;
                }
 
                // Make sure that we're working with the right name
@@ -76,6 +81,11 @@ jQuery.extend({
 
                // Check if we're setting a value
                if ( value !== undefined ) {
+                       // Make sure that NaN and null values aren't set. See: #7116
+                       if ( typeof value === "number" && isNaN( value ) || value == null ) {
+                               return;
+                       }
+
                        // If a number was passed in, add 'px' to the (except for certain CSS properties)
                        if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) {
                                value += "px";
@@ -83,7 +93,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 {
@@ -199,7 +213,7 @@ if ( !jQuery.support.opacity ) {
 
                        style.filter = ralpha.test(filter) ?
                                filter.replace(ralpha, opacity) :
-                               opacity;
+                               style.filter + ' ' + opacity;
                }
        };
 }