Skip to content

Commit 1886d74

Browse files
committed
Check the attribute node value for false for HTML5 booleans when not supported. Fixes #9504.
1 parent e6f8951 commit 1886d74

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ jQuery.extend({
496496
boolHook = {
497497
get: function( elem, name ) {
498498
// Align boolean attributes with corresponding properties
499-
return jQuery.prop( elem, name ) ?
499+
// Fall back to attribute presence where some booleans are not supported
500+
var attrNode;
501+
return jQuery.prop( elem, name ) === true || ( attrNode = elem.getAttributeNode( name ) ) && attrNode.nodeValue !== false ?
500502
name.toLowerCase() :
501503
undefined;
502504
},

test/unit/attributes.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ test("attr(Hash)", function() {
162162
});
163163

164164
test("attr(String, Object)", function() {
165-
expect(69);
165+
expect(73);
166166

167167
var div = jQuery("div").attr("foo", "bar"),
168168
fail = false;
@@ -230,13 +230,20 @@ test("attr(String, Object)", function() {
230230
jQuery("#name").attr("maxLength", "10");
231231
equals( document.getElementById("name").maxLength, 10, "Set maxlength attribute" );
232232

233-
var $text = jQuery("#text1").attr("autofocus", true);
234-
if ( "autofocus" in $text[0] ) {
235-
equals( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name");
236-
} else {
237-
equals( $text.attr("autofocus"), undefined, "autofocus stays undefined in browsers that do not support it(F<4)");
238-
}
239-
equals( $text.attr("autofocus", false).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it");
233+
// HTML5 boolean attributes
234+
var $text = jQuery("#text1").attr({
235+
"autofocus": true,
236+
"required": true
237+
});
238+
equal( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name" );
239+
equal( $text.attr("autofocus", false).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it" );
240+
equal( $text.attr("required"), "required", "Set boolean attributes to the same name" );
241+
equal( $text.attr("required", false).attr("required"), undefined, "Setting required attribute to false removes it" );
242+
243+
var $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
244+
equal( $details.attr("open"), "open", "open attribute presense indicates true" );
245+
equal( $details.attr("open", false).attr("open"), undefined, "Setting open attribute to false removes it" );
246+
240247
equals( $text.attr("data-something", true).data("something"), true, "Setting data attributes are not affected by boolean settings");
241248
equals( $text.attr("data-another", false).data("another"), false, "Setting data attributes are not affected by boolean settings" );
242249
equals( $text.attr("aria-disabled", false).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings");

0 commit comments

Comments
 (0)