aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortimmywil <timmywillisn@gmail.com>2011-07-09 14:41:58 -0400
committertimmywil <timmywillisn@gmail.com>2011-07-09 14:52:16 -0400
commit1886d7443453feab0b73f4a7c4b15fbd9401c4af (patch)
tree2a07bfeb95854fb10e77dfdc0b6f03fab5f26946
parente6f8951983b8ce1af8986651fd412f9e569504c6 (diff)
downloadjquery-1886d7443453feab0b73f4a7c4b15fbd9401c4af.tar.gz
jquery-1886d7443453feab0b73f4a7c4b15fbd9401c4af.zip
Check the attribute node value for false for HTML5 booleans when not supported. Fixes #9504.
-rw-r--r--src/attributes.js4
-rw-r--r--test/unit/attributes.js23
2 files changed, 18 insertions, 9 deletions
diff --git a/src/attributes.js b/src/attributes.js
index 1e0e79f4b..cbd9561eb 100644
--- a/src/attributes.js
+++ b/src/attributes.js
@@ -496,7 +496,9 @@ jQuery.extend({
boolHook = {
get: function( elem, name ) {
// Align boolean attributes with corresponding properties
- return jQuery.prop( elem, name ) ?
+ // Fall back to attribute presence where some booleans are not supported
+ var attrNode;
+ return jQuery.prop( elem, name ) === true || ( attrNode = elem.getAttributeNode( name ) ) && attrNode.nodeValue !== false ?
name.toLowerCase() :
undefined;
},
diff --git a/test/unit/attributes.js b/test/unit/attributes.js
index 4716e5b53..b0fe2b4aa 100644
--- a/test/unit/attributes.js
+++ b/test/unit/attributes.js
@@ -162,7 +162,7 @@ test("attr(Hash)", function() {
});
test("attr(String, Object)", function() {
- expect(69);
+ expect(73);
var div = jQuery("div").attr("foo", "bar"),
fail = false;
@@ -230,13 +230,20 @@ test("attr(String, Object)", function() {
jQuery("#name").attr("maxLength", "10");
equals( document.getElementById("name").maxLength, 10, "Set maxlength attribute" );
- var $text = jQuery("#text1").attr("autofocus", true);
- if ( "autofocus" in $text[0] ) {
- equals( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name");
- } else {
- equals( $text.attr("autofocus"), undefined, "autofocus stays undefined in browsers that do not support it(F<4)");
- }
- equals( $text.attr("autofocus", false).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it");
+ // HTML5 boolean attributes
+ var $text = jQuery("#text1").attr({
+ "autofocus": true,
+ "required": true
+ });
+ equal( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name" );
+ equal( $text.attr("autofocus", false).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it" );
+ equal( $text.attr("required"), "required", "Set boolean attributes to the same name" );
+ equal( $text.attr("required", false).attr("required"), undefined, "Setting required attribute to false removes it" );
+
+ var $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
+ equal( $details.attr("open"), "open", "open attribute presense indicates true" );
+ equal( $details.attr("open", false).attr("open"), undefined, "Setting open attribute to false removes it" );
+
equals( $text.attr("data-something", true).data("something"), true, "Setting data attributes are not affected by boolean settings");
equals( $text.attr("data-another", false).data("another"), false, "Setting data attributes are not affected by boolean settings" );
equals( $text.attr("aria-disabled", false).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings");