Skip to content

Commit d332046

Browse files
committed
Do not set boolean attributes to empty string on removal. Fixes #10870. +0 bytes compressed
1 parent 8013163 commit d332046

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/attributes.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ jQuery.extend({
352352
},
353353

354354
removeAttr: function( elem, value ) {
355-
var propName, attrNames, name, l,
355+
var propName, attrNames, name, l, isBool,
356356
i = 0;
357357

358358
if ( value && elem.nodeType === 1 ) {
@@ -364,13 +364,17 @@ jQuery.extend({
364364

365365
if ( name ) {
366366
propName = jQuery.propFix[ name ] || name;
367+
isBool = rboolean.test( name );
367368

368369
// See #9699 for explanation of this approach (setting first, then removal)
369-
jQuery.attr( elem, name, "" );
370+
// Do not do this for boolean attributes (see #10870)
371+
if ( !isBool ) {
372+
jQuery.attr( elem, name, "" );
373+
}
370374
elem.removeAttribute( getSetAttribute ? name : propName );
371375

372376
// Set corresponding property to false for boolean attributes
373-
if ( rboolean.test( name ) && propName in elem ) {
377+
if ( isBool && propName in elem ) {
374378
elem[ propName ] = false;
375379
}
376380
}

test/unit/attributes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ test("attr('tabindex', value)", function() {
464464
});
465465

466466
test("removeAttr(String)", function() {
467-
expect(9);
467+
expect( 10 );
468468
var $first;
469469

470470
equal( jQuery("#mark").removeAttr( "class" ).attr("class"), undefined, "remove class" );
@@ -479,6 +479,9 @@ test("removeAttr(String)", function() {
479479
jQuery("#text1").prop("readOnly", true).removeAttr("readonly");
480480
equal( document.getElementById("text1").readOnly, false, "removeAttr sets boolean properties to false" );
481481

482+
jQuery("#option2c").removeAttr("selected");
483+
equal( jQuery("#option2d").attr("selected"), "selected", "Removing `selected` from an option that is not selected does not remove selected from the currently selected option (#10870)");
484+
482485
try {
483486
$first = jQuery("#first").attr("contenteditable", "true").removeAttr("contenteditable");
484487
equal( $first.attr('contenteditable'), undefined, "Remove the contenteditable attribute" );

0 commit comments

Comments
 (0)