diff --git a/src/attributes/attr.js b/src/attributes/attr.js index 3bb05a5a3..caf0bc018 100644 --- a/src/attributes/attr.js +++ b/src/attributes/attr.js @@ -63,7 +63,12 @@ jQuery.extend({ return ret; } else { - return jQuery.find.attr( elem, name ); + ret = jQuery.find.attr( elem, name ); + + // Non-existent attributes return null, we normalize to undefined + return ret == null ? + undefined : + ret; } }, diff --git a/test/unit/attributes.js b/test/unit/attributes.js index ebc28a0e1..c1f773718 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -71,16 +71,16 @@ test( "attr(String)", function() { equal( jQuery("#text1").attr("value", "").attr("value"), "", "Check setting the value attribute to empty string" ); equal( jQuery("
").attr("value"), "t", "Check setting custom attr named 'value' on a div" ); equal( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existent attribute on a form" ); - equal( jQuery("#foo").attr("height"), null, "Non existent height attribute should return null" ); + equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" ); // [7472] & [3113] (form contains an input with name="action" or name="id") extras = jQuery("").appendTo("#testForm"); equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" ); - equal( jQuery("#testForm").attr("target"), null, "Retrieving target does not equal the input with name=target" ); + equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" ); equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" ); - equal( jQuery("#testForm").removeAttr("id").attr("id"), null, "Retrieving id does not equal the input with name=id after id is removed [#7472]" ); + equal( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" ); // Bug #3685 (form contains input with name="name") - equal( jQuery("#testForm").attr("name"), null, "Retrieving name does not retrieve input with name=name" ); + equal( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" ); extras.remove(); equal( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" ); @@ -107,7 +107,7 @@ test( "attr(String)", function() { body = document.body; $body = jQuery( body ); - strictEqual( $body.attr("foo"), null, "Make sure that a non existent attribute returns null" ); + strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" ); body.setAttribute( "foo", "baz" ); equal( $body.attr("foo"), "baz", "Make sure the dom attribute is retrieved when no expando is found" ); @@ -139,12 +139,12 @@ test( "attr(String)", function() { // Check value on button element (#1954) $button = jQuery("").insertAfter("#button"); - strictEqual( $button.attr("value"), null, "Absence of value attribute on a button" ); + strictEqual( $button.attr("value"), undefined, "Absence of value attribute on a button" ); equal( $button.attr( "value", "foobar" ).attr("value"), "foobar", "Value attribute on a button does not return innerHTML" ); equal( $button.attr("value", "baz").html(), "text", "Setting the value attribute does not change innerHTML" ); // Attributes with a colon on a table element (#1591) - equal( jQuery("#table").attr("test:attrib"), null, "Retrieving a non-existent attribute on a table with a colon does not throw an error." ); + equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." ); equal( jQuery("#table").attr( "test:attrib", "foobar" ).attr("test:attrib"), "foobar", "Setting an attribute on a table with a colon does not throw an error." ); $form = jQuery("").appendTo("#qunit-fixture"); @@ -153,12 +153,12 @@ test( "attr(String)", function() { $a = jQuery("Click").appendTo("#qunit-fixture"); equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." ); - ok( jQuery("").attr("doesntexist") === null, "Make sure null is returned when no attribute is found." ); - ok( jQuery("").attr("title") === null, "Make sure null is returned when no attribute is found." ); + ok( jQuery("").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." ); + ok( jQuery("").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." ); equal( jQuery("").attr( "title", "something" ).attr("title"), "something", "Set the title attribute." ); ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." ); - equal( jQuery("").attr("value"), null, "An unset value on a div returns null." ); - strictEqual( jQuery("").attr("value"), null, "An unset value on a select returns null." ); + equal( jQuery("").attr("value"), undefined, "An unset value on a div returns undefined." ); + strictEqual( jQuery("").attr("value"), undefined, "An unset value on a select returns undefined." ); $form = jQuery("#form").attr( "enctype", "multipart/form-data" ); equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" ); @@ -280,7 +280,7 @@ test( "attr(String, Object)", function() { jQuery("#name").attr( "name", "something" ); equal( jQuery("#name").attr("name"), "something", "Set name attribute" ); jQuery("#name").attr( "name", null ); - equal( jQuery("#name").attr("name"), null, "Remove name attribute" ); + equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" ); $input = jQuery( "", { name: "something", @@ -301,17 +301,17 @@ test( "attr(String, Object)", function() { $input.prop( "checked", true ).prop( "checked", false ).attr( "checked", true ); equal( $input.attr("checked"), "checked", "Set checked (verified by .attr)" ); $input.prop( "checked", false ).prop( "checked", true ).attr( "checked", false ); - equal( $input.attr("checked"), null, "Remove checked (verified by .attr)" ); + equal( $input.attr("checked"), undefined, "Remove checked (verified by .attr)" ); $input = jQuery("#text1").prop( "readOnly", true ).prop( "readOnly", false ).attr( "readonly", true ); equal( $input.attr("readonly"), "readonly", "Set readonly (verified by .attr)" ); $input.prop( "readOnly", false ).prop( "readOnly", true ).attr( "readonly", false ); - equal( $input.attr("readonly"), null, "Remove readonly (verified by .attr)" ); + equal( $input.attr("readonly"), undefined, "Remove readonly (verified by .attr)" ); $input = jQuery("#check2").attr( "checked", true ).attr( "checked", false ).prop( "checked", true ); equal( $input[0].checked, true, "Set checked property (verified by native property)" ); equal( $input.prop("checked"), true, "Set checked property (verified by .prop)" ); - equal( $input.attr("checked"), null, "Setting checked property doesn't affect checked attribute" ); + equal( $input.attr("checked"), undefined, "Setting checked property doesn't affect checked attribute" ); $input.attr( "checked", false ).attr( "checked", true ).prop( "checked", false ); equal( $input[0].checked, false, "Clear checked property (verified by native property)" ); equal( $input.prop("checked"), false, "Clear checked property (verified by .prop)" ); @@ -343,13 +343,13 @@ test( "attr(String, Object)", function() { "required": true }); equal( $text.attr("autofocus"), "autofocus", "Reading autofocus attribute yields 'autofocus'" ); - equal( $text.attr( "autofocus", false ).attr("autofocus"), null, "Setting autofocus to false removes it" ); + equal( $text.attr( "autofocus", false ).attr("autofocus"), undefined, "Setting autofocus to false removes it" ); equal( $text.attr("required"), "required", "Reading required attribute yields 'required'" ); - equal( $text.attr( "required", false ).attr("required"), null, "Setting required attribute to false removes it" ); + equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" ); $details = jQuery("