Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/jquery/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ var oldRemoveAttr = jQuery.fn.removeAttr,
rmatchNonSpace = /\S+/g;

migratePatchFunc( jQuery.fn, "removeAttr", function( name ) {
var self = this;
var self = this,
patchNeeded = false;

jQuery.each( name.match( rmatchNonSpace ), function( _i, attr ) {
if ( jQuery.expr.match.bool.test( attr ) ) {

// Only warn if at least a single node had the property set to
// something else than `false`. Otherwise, this Migrate patch
// doesn't influence the behavior and there's no need to set or warn.
self.each( function() {
if ( jQuery( this ).prop( attr ) !== false ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use this.attr !== false here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was playing safe to not omit any possible prop hooks. We almost don't have any for boolean props... except for selected in IE:
https://github.com/jquery/jquery/blob/3.6.3/src/attributes/prop.js#L91-L125
It seems we should use the jQuery API then, shouldn't we?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I suppose we might add a prop hook so this is the safest way. 👍

patchNeeded = true;
return false;
}
} );
}

if ( patchNeeded ) {
migrateWarn( "removeAttr-bool",
"jQuery.fn.removeAttr no longer sets boolean properties: " + attr );
self.prop( attr, false );
Expand Down
22 changes: 21 additions & 1 deletion test/unit/jquery/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
QUnit.module( "attributes" );

QUnit.test( ".removeAttr( boolean attribute )", function( assert ) {
assert.expect( 8 );
assert.expect( 14 );

expectNoWarning( assert, "non-boolean attr", function() {
var $div = jQuery( "<div />" )
Expand Down Expand Up @@ -40,6 +40,26 @@ QUnit.test( ".removeAttr( boolean attribute )", function( assert ) {
.removeAttr( "size" );
} );

expectNoWarning( assert, "boolean attr when prop false", function() {
var $inp = jQuery( "<input type=checkbox/>" )
.attr( "checked", "checked" )
.prop( "checked", false )
.removeAttr( "checked" );

assert.equal( $inp.attr( "checked" ), null, "boolean attribute was removed" );
assert.equal( $inp.prop( "checked" ), false, "property was not changed" );
} );

expectWarning( assert, "boolean attr when only some props false", 1, function() {
var $inp = jQuery( "<input type=checkbox/><input type=checkbox/><input type=checkbox/>" )
.attr( "checked", "checked" )
.prop( "checked", false )
.eq( 1 ).prop( "checked", true ).end()
.removeAttr( "checked" );

assert.equal( $inp.attr( "checked" ), null, "boolean attribute was removed" );
assert.equal( $inp.eq( 1 ).prop( "checked" ), false, "property was changed" );
} );
} );

QUnit.test( ".toggleClass( boolean )", function( assert ) {
Expand Down