Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3faf450

Browse files
committed
feat(jqLite): don't remove a boolean attribute for .attr(attrName, '')
This change aligns jqLite with jQuery. Ref #15126 BREAKING CHANGE: Before, using the `attr` method with an empty string as a value would remove the boolean attribute. Now it sets it to its lowercase name as was happening for every non-empty string so far. The only two values that remove the boolean attribute are now null & false, just like in jQuery. To migrate the code follow the example below: Before: elem.attr(booleanAttrName, ''); After: elem.attr(booleanAttrName, false); or: elem.attr(booleanAttrName, null);
1 parent 4e36245 commit 3faf450

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/jqLite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ forEach({
645645
var lowercasedName = lowercase(name);
646646
if (BOOLEAN_ATTR[lowercasedName]) {
647647
if (isDefined(value)) {
648-
if (value) {
648+
if (value !== false && value !== null) {
649649
element.setAttribute(name, name);
650650
} else {
651651
element.removeAttribute(name);

test/jqLiteSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,24 @@ describe('jqLite', function() {
733733
elm.attr('attribute', '');
734734
expect(elm[0].getAttribute('attribute')).toBe('');
735735
});
736+
737+
it('should remove the boolean attribute for a false value', function() {
738+
var elm = jqLite('<select multiple>');
739+
elm.attr('multiple', false);
740+
expect(elm[0].hasAttribute('multiple')).toBe(false);
741+
});
742+
743+
it('should remove the boolean attribute for a null value', function() {
744+
var elm = jqLite('<select multiple>');
745+
elm.attr('multiple', null);
746+
expect(elm[0].hasAttribute('multiple')).toBe(false);
747+
});
748+
749+
it('should not remove the boolean attribute for an empty string as a value', function() {
750+
var elm = jqLite('<select multiple>');
751+
elm.attr('multiple', '');
752+
expect(elm[0].getAttribute('multiple')).toBe('multiple');
753+
});
736754
});
737755

738756

0 commit comments

Comments
 (0)