Skip to content

Commit

Permalink
Don't try to parse things that aren't parseable in the cssHook - Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarf committed Jan 4, 2012
1 parent 6fc340c commit 2f7b194
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 54 deletions.
121 changes: 67 additions & 54 deletions jquery.color.js
Expand Up @@ -171,6 +171,50 @@
return type.min > value ? type.min : type.max < value ? type.max : value;
}

function stringParse( string ) {
var inst = color(),
rgba = inst._rgba = [];

string = string.toLowerCase();

each( stringParsers, function( i, parser ) {
var match = parser.re.exec( string ),
values = match && parser.parse( match ),
parsed,
spaceName = parser.space || "rgba",
cache = spaces[ spaceName ].cache;


if ( values ) {
parsed = inst[ spaceName ]( values );

// if this was an rgba parse the assignment might happen twice
// oh well....
inst[ cache ] = parsed[ cache ];
rgba = inst._rgba = parsed._rgba;

// exit each( stringParsers ) here because we matched
return false;
}
});

// Found a stringParser that handled it
if ( rgba.length !== 0 ) {

// if this came from a parsed string, force "transparent" when alpha is 0
// chrome, (and maybe others) return "transparent" as rgba(0,0,0,0)
if ( Math.max.apply( Math, rgba ) === 0 ) {
jQuery.extend( rgba, colors.transparent );
}
return inst;
}

// named colors / default - filter back through parse function
if ( string = colors[ string ] ) {
return string;
}
}

color.fn = color.prototype = {
constructor: color,
parse: function( red, green, blue, alpha ) {
Expand All @@ -195,42 +239,7 @@
}

if ( type === "string" ) {
red = red.toLowerCase();
each( stringParsers, function( i, parser ) {
var match = parser.re.exec( red ),
values = match && parser.parse( match ),
parsed,
spaceName = parser.space || "rgba",
cache = spaces[ spaceName ].cache;


if ( values ) {
parsed = inst[ spaceName ]( values );

// if this was an rgba parse the assignment might happen twice
// oh well....
inst[ cache ] = parsed[ cache ];
rgba = inst._rgba = parsed._rgba;

// exit each( stringParsers ) here because we matched
return false;
}
});

// Found a stringParser that handled it
if ( rgba.length !== 0 ) {

// if this came from a parsed string, force "transparent" when alpha is 0
// chrome, (and maybe others) return "transparent" as rgba(0,0,0,0)
if ( Math.max.apply( Math, rgba ) === 0 ) {
jQuery.extend( rgba, colors.transparent );
}
return this;
}

// named colors / default - filter back through parse function
red = colors[ red ] || colors._default;
return this.parse( red );
return this.parse( stringParse( red ) || colors._default );
}

if ( type === "array" ) {
Expand Down Expand Up @@ -557,25 +566,29 @@
each( stepHooks, function( i, hook ) {
jQuery.cssHooks[ hook ] = {
set: function( elem, value ) {
value = color( value );
if ( !support.rgba && value._rgba[ 3 ] !== 1 ) {
var backgroundColor,
curElem = hook === "backgroundColor" ? elem.parentNode : elem;
do {
backgroundColor = jQuery.curCSS( curElem, "backgroundColor" );
} while (
( backgroundColor === "" || backgroundColor === "transparent" ) &&
( curElem = curElem.parentNode ) &&
curElem.style
);

value = value.blend( backgroundColor && backgroundColor !== "transparent" ?
backgroundColor :
"_default" );
}

value = value.toRgbaString();
var parsed;

if ( jQuery.type( value ) !== 'string' || ( parsed = stringParse( value ) ) )
{
value = color( parsed || value );
if ( !support.rgba && value._rgba[ 3 ] !== 1 ) {
var backgroundColor,
curElem = hook === "backgroundColor" ? elem.parentNode : elem;
do {
backgroundColor = jQuery.curCSS( curElem, "backgroundColor" );
} while (
( backgroundColor === "" || backgroundColor === "transparent" ) &&
( curElem = curElem.parentNode ) &&
curElem.style
);

value = value.blend( backgroundColor && backgroundColor !== "transparent" ?
backgroundColor :
"_default" );
}

value = value.toRgbaString();
}
elem.style[ hook ] = value;
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/unit/color.js
Expand Up @@ -550,4 +550,16 @@ asyncTest( "animated documentFragment", function() {
ok( true, "Animation of color on documentFragment did not fail" );
start();
});
});

test( "Setting CSS to empty string / inherit", function() {
var el = jQuery( "<div></div>" ).appendTo( "body" ).css({ color: "#fff" });
expect( 2 );

el.css( "color", "" );
equal( el[0].style.color, "", "CSS was set to empty string" );

el.css( "color", "inherit" );
equal( el[0].style.color, "inherit", "CSS was set to inherit" );

});

0 comments on commit 2f7b194

Please sign in to comment.