Skip to content

Commit

Permalink
Css: Restore the previous style if the new one is rejected
Browse files Browse the repository at this point in the history
The workaround to be able to change !important styles broke the browser
keeping the old CSS value if the new one was rejected. Revert to the old
value explicitely in such a case.

Fixes #14836
Closes jquerygh-1532
  • Loading branch information
mgol committed Mar 10, 2014
1 parent a96d5be commit 28aa986
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/css.js
Expand Up @@ -247,7 +247,7 @@ jQuery.extend({
}

// Make sure that we're working with the right name
var ret, type, hooks,
var ret, type, hooks, oldValue,
origName = jQuery.camelCase( name ),
style = elem.style;

Expand Down Expand Up @@ -286,10 +286,19 @@ 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, extra )) !== undefined ) {
// Support: Chrome, Safari
oldValue = style[ name ];

// Support: Chrome, Safari, IE
// Setting style to blank string required to delete "style: x !important;"
style[ name ] = "";
style[ name ] = value;

// Revert to the old value if the browser didn't accept the new rule to
// not break the cascade.
// Fixes #14836
if ( value && !style[ name ] ) {
style[ name ] = oldValue;
}
}

} else {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/css.js
Expand Up @@ -929,6 +929,20 @@ test( "Override !important when changing styles (#14394)", function() {
equal( el.css( "display" ), "none", "New style replaced !important" );
});

test( "Keep the last style if the new one isn't recognized by the browser (#14836)", function() {
expect( 3 );

var el;

el = jQuery( "<div></div>" ).css( "color", "black" ).css( "color", "fake value" );
equal( el.css( "color" ), "black", "The old style is kept when setting an unrecognized value" );
el = jQuery( "<div></div>" ).css( "color", "black" ).css( "color", " " );
equal( el.css( "color" ), "black", "The old style is kept when setting to a space" );

el = jQuery( "<div></div>" ).css( "color", "black" ).css( "color", "" );
equal( el.css( "color" ), "", "The style can be reset by setting to an empty string" );
});

asyncTest( "Clearing a Cloned Element's Style Shouldn't Clear the Original Element's Style (#8908)", 24, function() {
var baseUrl = document.location.href.replace( /([^\/]*)$/, "" ),
styles = [{
Expand Down

0 comments on commit 28aa986

Please sign in to comment.