If no hook is provided, and a boolean property exists, use that to return an attribute-style value for boolean attributes. Fixes #9079.

This commit is contained in:
John Resig 2011-05-03 21:44:42 -04:00
parent bfad45fe45
commit a9d9f8c542
2 changed files with 44 additions and 12 deletions

View file

@ -285,8 +285,7 @@ jQuery.extend({
attrFix: { attrFix: {
// Always normalize to ensure hook usage // Always normalize to ensure hook usage
tabindex: "tabIndex", tabindex: "tabIndex"
readonly: "readOnly"
}, },
attr: function( elem, name, value, pass ) { attr: function( elem, name, value, pass ) {
@ -327,20 +326,25 @@ jQuery.extend({
// Set boolean attributes to the same name // Set boolean attributes to the same name
if ( value === true && !rspecial.test( name ) ) { if ( value === true && !rspecial.test( name ) ) {
value = name; value = name.toLowerCase();
} }
elem.setAttribute( name, "" + value ); elem.setAttribute( name, "" + value );
return value; return value;
} }
} else { } else if ( hooks && "get" in hooks && notxml ) {
return hooks.get( elem, name );
if ( hooks && "get" in hooks && notxml ) { } else {
return hooks.get( elem, name ); var boolProp = elem[ jQuery.propFix[ name ] || name ];
if ( typeof boolProp === "boolean" ) {
return boolProp ?
name.toLowerCase() :
undefined;
} else { } else {
ret = elem.getAttribute( name ); ret = elem.getAttribute( name );
// Non-existent attributes return null, we normalize to undefined // Non-existent attributes return null, we normalize to undefined
@ -399,7 +403,9 @@ jQuery.extend({
} }
}, },
propFix: {}, propFix: {
readonly: "readOnly"
},
prop: function( elem, name, value ) { prop: function( elem, name, value ) {
var nType = elem.nodeType; var nType = elem.nodeType;
@ -441,6 +447,7 @@ jQuery.extend({
// IE6/7 do not support getting/setting some attributes with get/setAttribute // IE6/7 do not support getting/setting some attributes with get/setAttribute
if ( !jQuery.support.getSetAttribute ) { if ( !jQuery.support.getSetAttribute ) {
jQuery.attrFix = jQuery.extend( jQuery.attrFix, { jQuery.attrFix = jQuery.extend( jQuery.attrFix, {
readonly: "readOnly",
"for": "htmlFor", "for": "htmlFor",
"class": "className", "class": "className",
maxlength: "maxLength", maxlength: "maxLength",

View file

@ -28,8 +28,7 @@ test("jQuery.attrFix integrity test", function() {
}; };
} else { } else {
propsShouldBe = { propsShouldBe = {
tabindex: "tabIndex", tabindex: "tabIndex"
readonly: "readOnly"
}; };
} }
@ -181,7 +180,7 @@ test("attr(Hash)", function() {
}); });
test("attr(String, Object)", function() { test("attr(String, Object)", function() {
expect(35); expect(55);
var div = jQuery("div").attr("foo", "bar"), var div = jQuery("div").attr("foo", "bar"),
fail = false; fail = false;
@ -204,12 +203,38 @@ test("attr(String, Object)", function() {
equals( jQuery("#name").attr("name"), undefined, "Remove name attribute" ); equals( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
jQuery("#check2").attr("checked", true); jQuery("#check2").attr("checked", true);
equals( document.getElementById("check2").checked, true, "Set checked attribute" ); equals( document.getElementById("check2").checked, true, "Set checked attribute" );
equals( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
equals( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
jQuery("#check2").attr("checked", false); jQuery("#check2").attr("checked", false);
equals( document.getElementById("check2").checked, false, "Set checked attribute" ); equals( document.getElementById("check2").checked, false, "Set checked attribute" );
equals( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
equals( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
jQuery("#text1").attr("readonly", true); jQuery("#text1").attr("readonly", true);
equals( document.getElementById("text1").readOnly, true, "Set readonly attribute" ); equals( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
equals( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
equals( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
jQuery("#text1").attr("readonly", false); jQuery("#text1").attr("readonly", false);
equals( document.getElementById("text1").readOnly, false, "Set readonly attribute" ); equals( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
equals( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
equals( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
jQuery("#check2").prop("checked", true);
equals( document.getElementById("check2").checked, true, "Set checked attribute" );
equals( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
equals( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
jQuery("#check2").prop("checked", false);
equals( document.getElementById("check2").checked, false, "Set checked attribute" );
equals( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
equals( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
jQuery("#text1").prop("readOnly", true);
equals( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
equals( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
equals( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
jQuery("#text1").prop("readOnly", false);
equals( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
equals( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
equals( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
jQuery("#name").attr("maxlength", "5"); jQuery("#name").attr("maxlength", "5");
equals( document.getElementById("name").maxLength, 5, "Set maxlength attribute" ); equals( document.getElementById("name").maxLength, 5, "Set maxlength attribute" );
jQuery("#name").attr("maxLength", "10"); jQuery("#name").attr("maxLength", "10");