Skip to content

Commit 072e44d

Browse files
authored
Attributes: Don't warn against removeAttr if property false
Some frameworks, like AngularJS, invoke `removeAttr` on boolean attributes but only after setting the respective property to `false`. Such usage is not dangerous and would continue to work even after Migrate is removed. Therefore, only warn for `removeAttr(booleanAttribute)` if the property wasn't `false` already. Fixes gh-471 Closes gh-484
1 parent 0d082f9 commit 072e44d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/jquery/attributes.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ var oldRemoveAttr = jQuery.fn.removeAttr,
55
rmatchNonSpace = /\S+/g;
66

77
migratePatchFunc( jQuery.fn, "removeAttr", function( name ) {
8-
var self = this;
8+
var self = this,
9+
patchNeeded = false;
910

1011
jQuery.each( name.match( rmatchNonSpace ), function( _i, attr ) {
1112
if ( jQuery.expr.match.bool.test( attr ) ) {
13+
14+
// Only warn if at least a single node had the property set to
15+
// something else than `false`. Otherwise, this Migrate patch
16+
// doesn't influence the behavior and there's no need to set or warn.
17+
self.each( function() {
18+
if ( jQuery( this ).prop( attr ) !== false ) {
19+
patchNeeded = true;
20+
return false;
21+
}
22+
} );
23+
}
24+
25+
if ( patchNeeded ) {
1226
migrateWarn( "removeAttr-bool",
1327
"jQuery.fn.removeAttr no longer sets boolean properties: " + attr );
1428
self.prop( attr, false );

test/unit/jquery/attributes.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
QUnit.module( "attributes" );
33

44
QUnit.test( ".removeAttr( boolean attribute )", function( assert ) {
5-
assert.expect( 8 );
5+
assert.expect( 14 );
66

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

43+
expectNoWarning( assert, "boolean attr when prop false", function() {
44+
var $inp = jQuery( "<input type=checkbox/>" )
45+
.attr( "checked", "checked" )
46+
.prop( "checked", false )
47+
.removeAttr( "checked" );
48+
49+
assert.equal( $inp.attr( "checked" ), null, "boolean attribute was removed" );
50+
assert.equal( $inp.prop( "checked" ), false, "property was not changed" );
51+
} );
52+
53+
expectWarning( assert, "boolean attr when only some props false", 1, function() {
54+
var $inp = jQuery( "<input type=checkbox/><input type=checkbox/><input type=checkbox/>" )
55+
.attr( "checked", "checked" )
56+
.prop( "checked", false )
57+
.eq( 1 ).prop( "checked", true ).end()
58+
.removeAttr( "checked" );
59+
60+
assert.equal( $inp.attr( "checked" ), null, "boolean attribute was removed" );
61+
assert.equal( $inp.eq( 1 ).prop( "checked" ), false, "property was changed" );
62+
} );
4363
} );
4464

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

0 commit comments

Comments
 (0)