Skip to content

Commit

Permalink
Merge pull request jquery#416 from gnarf37/ticket_6652
Browse files Browse the repository at this point in the history
CSS: Remove filter from style when setting opacity to 1 - Fixes #6652 - R
  • Loading branch information
dmethvin committed Aug 4, 2011
2 parents b5a16ea + 5b0c306 commit 8d5c874
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,29 @@ if ( !jQuery.support.opacity ) {

set: function( elem, value ) {
var style = elem.style,
currentStyle = elem.currentStyle;
currentStyle = elem.currentStyle,
opacity = jQuery.isNaN( value ) ? "" : "alpha(opacity=" + value * 100 + ")",
filter = currentStyle && currentStyle.filter || style.filter || "";

// IE has trouble with opacity if it does not have layout
// Force it by setting the zoom level
style.zoom = 1;

// Set the alpha filter to set the opacity
var opacity = jQuery.isNaN( value ) ?
"" :
"alpha(opacity=" + value * 100 + ")",
filter = currentStyle && currentStyle.filter || style.filter || "";
// if setting opacity to 1, and no other filters exist - attempt to remove filter attribute #6652
if ( value >= 1 && jQuery.trim( filter.replace( ralpha, "" ) ) === "" ) {

// Setting style.filter to null, "" & " " still leave "filter:" in the cssText
// if "filter:" is present at all, clearType is disabled, we want to avoid this
// style.removeAttribute is IE Only, but so apparently is this code path...
style.removeAttribute( "filter" );

// if there there is no filter style applied in a css rule, we are done
if ( currentStyle && !currentStyle.filter ) {
return;
}
}

// otherwise, set new filter values
style.filter = ralpha.test( filter ) ?
filter.replace( ralpha, opacity ) :
filter + " " + opacity;
Expand Down
2 changes: 2 additions & 0 deletions test/data/testsuite.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ dfn { display: none; }
/* #9239 Attach a background to the body( avoid crashes in removing the test element in support ) */
body, div { background: url(http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif) no-repeat -1000px 0; }

/* #6652 REMOVE FILTER:ALPHA(OPACITY=100) AFTER ANIMATION */
#t6652 div { filter: alpha(opacity=50); }
3 changes: 3 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ <h2 id="qunit-userAgent"></h2>
<div id="t2037">
<div><div class="hidden">hidden</div></div>
</div>
<div id="t6652">
<div></div>
</div>
</div>

<div id="tabindex-tests">
Expand Down
17 changes: 17 additions & 0 deletions test/unit/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ if ( !jQuery.support.opacity ) {
jQuery("#foo").css("filter", filterVal3).css("opacity", 1);
ok( jQuery("#foo").css("filter").indexOf(filterVal3) !== -1, "Setting opacity in IE doesn't clobber other filters" );
});

test( "Setting opacity to 1 properly removes filter: style (#6652)", function() {
var rfilter = /filter:[^;]*/i,
test = jQuery( "#t6652" ).css( "opacity", 1 ),
test2 = test.find( "div" ).css( "opacity", 1 );

function hasFilter( elem ) {
var match = rfilter.exec( elem[0].style.cssText );
if ( match ) {
return true;
}
return false;
}
expect( 2 );
ok( !hasFilter( test ), "Removed filter attribute on element without filter in stylesheet" );
ok( hasFilter( test2 ), "Filter attribute remains on element that had filter in stylesheet" );
});
}

test("css(String, Function)", function() {
Expand Down

0 comments on commit 8d5c874

Please sign in to comment.