Skip to content

Commit 7bce5b0

Browse files
committed
Attributes: revert returning null for non-existant attributes
Ref #2118
1 parent e38138a commit 7bce5b0

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

src/attributes/attr.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ jQuery.extend({
6666
return ret;
6767

6868
} else {
69-
return jQuery.find.attr( elem, name );
69+
ret = jQuery.find.attr( elem, name );
70+
71+
// Non-existent attributes return null, we normalize to undefined
72+
return ret == null ?
73+
undefined :
74+
ret;
7075
}
7176
},
7277

test/unit/attributes.js

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ test( "attr(String)", function() {
7171
equal( jQuery("#text1").attr("value", "").attr("value"), "", "Check setting the value attribute to empty string" );
7272
equal( jQuery("<div value='t'></div>").attr("value"), "t", "Check setting custom attr named 'value' on a div" );
7373
equal( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existent attribute on a form" );
74-
equal( jQuery("#foo").attr("height"), null, "Non existent height attribute should return null" );
74+
equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" );
7575

7676
// [7472] & [3113] (form contains an input with name="action" or name="id")
7777
extras = jQuery("<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />").appendTo("#testForm");
7878
equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" );
79-
equal( jQuery("#testForm").attr("target"), null, "Retrieving target does not equal the input with name=target" );
79+
equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" );
8080
equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" );
81-
equal( jQuery("#testForm").removeAttr("id").attr("id"), null, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
81+
equal( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
8282
// Bug #3685 (form contains input with name="name")
83-
equal( jQuery("#testForm").attr("name"), null, "Retrieving name does not retrieve input with name=name" );
83+
equal( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" );
8484
extras.remove();
8585

8686
equal( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" );
@@ -107,7 +107,7 @@ test( "attr(String)", function() {
107107
body = document.body;
108108
$body = jQuery( body );
109109

110-
strictEqual( $body.attr("foo"), null, "Make sure that a non existent attribute returns null" );
110+
strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" );
111111

112112
body.setAttribute( "foo", "baz" );
113113
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() {
139139

140140
// Check value on button element (#1954)
141141
$button = jQuery("<button>text</button>").insertAfter("#button");
142-
strictEqual( $button.attr("value"), null, "Absence of value attribute on a button" );
142+
strictEqual( $button.attr("value"), undefined, "Absence of value attribute on a button" );
143143
equal( $button.attr( "value", "foobar" ).attr("value"), "foobar", "Value attribute on a button does not return innerHTML" );
144144
equal( $button.attr("value", "baz").html(), "text", "Setting the value attribute does not change innerHTML" );
145145

146146
// Attributes with a colon on a table element (#1591)
147-
equal( jQuery("#table").attr("test:attrib"), null, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
147+
equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
148148
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." );
149149

150150
$form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture");
@@ -153,12 +153,12 @@ test( "attr(String)", function() {
153153
$a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture");
154154
equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
155155

156-
ok( jQuery("<div/>").attr("doesntexist") === null, "Make sure null is returned when no attribute is found." );
157-
ok( jQuery("<div/>").attr("title") === null, "Make sure null is returned when no attribute is found." );
156+
ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
157+
ok( jQuery("<div/>").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." );
158158
equal( jQuery("<div/>").attr( "title", "something" ).attr("title"), "something", "Set the title attribute." );
159159
ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
160-
equal( jQuery("<div/>").attr("value"), null, "An unset value on a div returns null." );
161-
strictEqual( jQuery("<select><option value='property'></option></select>").attr("value"), null, "An unset value on a select returns null." );
160+
equal( jQuery("<div/>").attr("value"), undefined, "An unset value on a div returns undefined." );
161+
strictEqual( jQuery("<select><option value='property'></option></select>").attr("value"), undefined, "An unset value on a select returns undefined." );
162162

163163
$form = jQuery("#form").attr( "enctype", "multipart/form-data" );
164164
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() {
280280
jQuery("#name").attr( "name", "something" );
281281
equal( jQuery("#name").attr("name"), "something", "Set name attribute" );
282282
jQuery("#name").attr( "name", null );
283-
equal( jQuery("#name").attr("name"), null, "Remove name attribute" );
283+
equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
284284

285285
$input = jQuery( "<input>", {
286286
name: "something",
@@ -301,17 +301,17 @@ test( "attr(String, Object)", function() {
301301
$input.prop( "checked", true ).prop( "checked", false ).attr( "checked", true );
302302
equal( $input.attr("checked"), "checked", "Set checked (verified by .attr)" );
303303
$input.prop( "checked", false ).prop( "checked", true ).attr( "checked", false );
304-
equal( $input.attr("checked"), null, "Remove checked (verified by .attr)" );
304+
equal( $input.attr("checked"), undefined, "Remove checked (verified by .attr)" );
305305

306306
$input = jQuery("#text1").prop( "readOnly", true ).prop( "readOnly", false ).attr( "readonly", true );
307307
equal( $input.attr("readonly"), "readonly", "Set readonly (verified by .attr)" );
308308
$input.prop( "readOnly", false ).prop( "readOnly", true ).attr( "readonly", false );
309-
equal( $input.attr("readonly"), null, "Remove readonly (verified by .attr)" );
309+
equal( $input.attr("readonly"), undefined, "Remove readonly (verified by .attr)" );
310310

311311
$input = jQuery("#check2").attr( "checked", true ).attr( "checked", false ).prop( "checked", true );
312312
equal( $input[0].checked, true, "Set checked property (verified by native property)" );
313313
equal( $input.prop("checked"), true, "Set checked property (verified by .prop)" );
314-
equal( $input.attr("checked"), null, "Setting checked property doesn't affect checked attribute" );
314+
equal( $input.attr("checked"), undefined, "Setting checked property doesn't affect checked attribute" );
315315
$input.attr( "checked", false ).attr( "checked", true ).prop( "checked", false );
316316
equal( $input[0].checked, false, "Clear checked property (verified by native property)" );
317317
equal( $input.prop("checked"), false, "Clear checked property (verified by .prop)" );
@@ -343,13 +343,13 @@ test( "attr(String, Object)", function() {
343343
"required": true
344344
});
345345
equal( $text.attr("autofocus"), "autofocus", "Reading autofocus attribute yields 'autofocus'" );
346-
equal( $text.attr( "autofocus", false ).attr("autofocus"), null, "Setting autofocus to false removes it" );
346+
equal( $text.attr( "autofocus", false ).attr("autofocus"), undefined, "Setting autofocus to false removes it" );
347347
equal( $text.attr("required"), "required", "Reading required attribute yields 'required'" );
348-
equal( $text.attr( "required", false ).attr("required"), null, "Setting required attribute to false removes it" );
348+
equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" );
349349

350350
$details = jQuery("<details open></details>").appendTo("#qunit-fixture");
351351
equal( $details.attr("open"), "open", "open attribute presence indicates true" );
352-
equal( $details.attr( "open", false ).attr("open"), null, "Setting open attribute to false removes it" );
352+
equal( $details.attr( "open", false ).attr("open"), undefined, "Setting open attribute to false removes it" );
353353

354354
$text.attr( "data-something", true );
355355
equal( $text.attr("data-something"), "true", "Set data attributes");
@@ -377,17 +377,8 @@ test( "attr(String, Object)", function() {
377377
jQuery.each( [ window, document, obj, "#firstp" ], function( i, elem ) {
378378
var oldVal = elem.nonexisting,
379379
$elem = jQuery( elem );
380-
// Falls back to prop, which returns undefined
381-
strictEqual(
382-
$elem.attr( "nonexisting" ),
383-
typeof $elem[0].getAttribute === "undefined" ? undefined : null,
384-
"attr works correctly for non existing attributes (bug #7500)."
385-
);
386-
equal(
387-
$elem.attr( "nonexisting", "foo" ).attr( "nonexisting" ),
388-
"foo",
389-
"attr falls back to prop on unsupported arguments"
390-
);
380+
strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
381+
equal( $elem.attr( "nonexisting", "foo" ).attr("nonexisting"), "foo", "attr falls back to prop on unsupported arguments" );
391382
elem.nonexisting = oldVal;
392383
});
393384

@@ -403,7 +394,7 @@ test( "attr(String, Object)", function() {
403394
table.attr("cellspacing", "2");
404395
equal( table[ 0 ]["cellSpacing"], "2", "Check cellspacing is correctly set" );
405396

406-
equal( jQuery("#area1").attr("value"), null, "Value attribute is distinct from value property." );
397+
equal( jQuery("#area1").attr("value"), undefined, "Value attribute is distinct from value property." );
407398

408399
// for #1070
409400
jQuery("#name").attr( "someAttr", "0" );
@@ -476,7 +467,7 @@ test( "attr(String, Object)", function() {
476467
jQuery("#name").attr( "maxlength", "5" ).removeAttr("nonexisting");
477468
equal( typeof jQuery("#name").attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (#5571)" );
478469
equal( jQuery("#name").attr( "maxlength", undefined ).attr("maxlength"), "5", ".attr('attribute', undefined) does not change value (#5571)" );
479-
equal( jQuery("#name").attr( "nonexisting", undefined ).attr("nonexisting"), null, ".attr('attribute', undefined) does not create attribute (#5571)" );
470+
equal( jQuery("#name").attr( "nonexisting", undefined ).attr("nonexisting"), undefined, ".attr('attribute', undefined) does not create attribute (#5571)" );
480471
});
481472

482473
test( "attr - extending the boolean attrHandle", function() {
@@ -512,23 +503,23 @@ test( "attr(String, Object) - Loaded via XML fragment", function() {
512503
$frag.attr( "test", "some value" );
513504
equal( $frag.attr("test"), "some value", "set attribute" );
514505
$frag.attr( "test", null );
515-
equal( $frag.attr("test"), null, "remove attribute" );
506+
equal( $frag.attr("test"), undefined, "remove attribute" );
516507
});
517508

518509
test( "attr('tabindex')", function() {
519510
expect( 8 );
520511

521512
// elements not natively tabbable
522513
equal( jQuery("#listWithTabIndex").attr("tabindex"), "5", "not natively tabbable, with tabindex set to 0" );
523-
equal( jQuery("#divWithNoTabIndex").attr("tabindex"), null, "not natively tabbable, no tabindex set" );
514+
equal( jQuery("#divWithNoTabIndex").attr("tabindex"), undefined, "not natively tabbable, no tabindex set" );
524515

525516
// anchor with href
526-
equal( jQuery("#linkWithNoTabIndex").attr("tabindex"), null, "anchor with href, no tabindex set" );
517+
equal( jQuery("#linkWithNoTabIndex").attr("tabindex"), undefined, "anchor with href, no tabindex set" );
527518
equal( jQuery("#linkWithTabIndex").attr("tabindex"), "2", "anchor with href, tabindex set to 2" );
528519
equal( jQuery("#linkWithNegativeTabIndex").attr("tabindex"), "-1", "anchor with href, tabindex set to -1" );
529520

530521
// anchor without href
531-
equal( jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), null, "anchor without href, no tabindex set" );
522+
equal( jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), undefined, "anchor without href, no tabindex set" );
532523
equal( jQuery("#linkWithNoHrefWithTabIndex").attr("tabindex"), "1", "anchor without href, tabindex set to 2" );
533524
equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").attr("tabindex"), "-1", "anchor without href, no tabindex set" );
534525
});
@@ -537,7 +528,7 @@ test( "attr('tabindex', value)", function() {
537528
expect( 9 );
538529

539530
var element = jQuery("#divWithNoTabIndex");
540-
equal( element.attr("tabindex"), null, "start with no tabindex" );
531+
equal( element.attr("tabindex"), undefined, "start with no tabindex" );
541532

542533
// set a positive string
543534
element.attr( "tabindex", "1" );
@@ -574,10 +565,10 @@ test( "removeAttr(String)", function() {
574565
expect( 12 );
575566
var $first;
576567

577-
equal( jQuery("#mark").removeAttr("class").attr("class"), null, "remove class" );
578-
equal( jQuery("#form").removeAttr("id").attr("id"), null, "Remove id" );
579-
equal( jQuery("#foo").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), null, "Check removing style attribute" );
580-
equal( jQuery("#form").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), null, "Check removing style attribute on a form" );
568+
equal( jQuery("#mark").removeAttr("class").attr("class"), undefined, "remove class" );
569+
equal( jQuery("#form").removeAttr("id").attr("id"), undefined, "Remove id" );
570+
equal( jQuery("#foo").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute" );
571+
equal( jQuery("#form").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute on a form" );
581572
equal( jQuery("<div style='position: absolute'></div>").appendTo("#foo").removeAttr("style").prop("style").cssText, "", "Check removing style attribute (#9699 Webkit)" );
582573
equal( jQuery("#fx-test-group").attr( "height", "3px" ).removeAttr("height").get( 0 ).style.height, "1px", "Removing height attribute has no effect on height set with style attribute" );
583574

@@ -591,15 +582,15 @@ test( "removeAttr(String)", function() {
591582

592583
try {
593584
$first = jQuery("#first").attr( "contenteditable", "true" ).removeAttr("contenteditable");
594-
equal( $first.attr("contenteditable"), null, "Remove the contenteditable attribute" );
585+
equal( $first.attr("contenteditable"), undefined, "Remove the contenteditable attribute" );
595586
} catch( e ) {
596587
ok( false, "Removing contenteditable threw an error (#10429)" );
597588
}
598589

599590
$first = jQuery("<div Case='mixed'></div>");
600591
equal( $first.attr("Case"), "mixed", "case of attribute doesn't matter" );
601592
$first.removeAttr("Case");
602-
equal( $first.attr( "Case" ), null, "mixed-case attribute was removed" );
593+
equal( $first.attr("Case"), undefined, "mixed-case attribute was removed" );
603594
});
604595

605596
test( "removeAttr(String) in XML", function() {
@@ -611,14 +602,14 @@ test( "removeAttr(String) in XML", function() {
611602
iwt.removeAttr("Normal");
612603
equal( iwt.attr("normal"), "ab", "Should still be there" );
613604
iwt.removeAttr("normal");
614-
equal( iwt.attr("normal"), null, "Removed" );
605+
equal( iwt.attr("normal"), undefined, "Removed" );
615606

616607
equal( iwt.attr("mixedCase"), "yes", "Check initial value" );
617-
equal( iwt.attr("mixedcase"), null, "toLowerCase not work good" );
608+
equal( iwt.attr("mixedcase"), undefined, "toLowerCase not work good" );
618609
iwt.removeAttr("mixedcase");
619610
equal( iwt.attr("mixedCase"), "yes", "Should still be there" );
620611
iwt.removeAttr("mixedCase");
621-
equal( iwt.attr("mixedCase"), null, "Removed" );
612+
equal( iwt.attr("mixedCase"), undefined, "Removed" );
622613
});
623614

624615
test( "removeAttr(Multi String, variable space width)", function() {
@@ -639,7 +630,7 @@ test( "removeAttr(Multi String, variable space width)", function() {
639630
div.removeAttr( "id alt title rel " );
640631

641632
jQuery.each( tests, function( key ) {
642-
equal( div.attr( key ), null, "Attribute `" + key + "` was removed" );
633+
equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
643634
});
644635
});
645636

@@ -1169,7 +1160,7 @@ var testRemoveClass = function(valueObj) {
11691160

11701161

11711162
jQuery( div ).removeClass( valueObj("foo") );
1172-
strictEqual( jQuery( div ).attr("class"), null, "removeClass doesn't create a class attribute" );
1163+
strictEqual( jQuery( div ).attr("class"), undefined, "removeClass doesn't create a class attribute" );
11731164

11741165
div.className = " test foo ";
11751166

0 commit comments

Comments
 (0)