Skip to content

Commit 773d749

Browse files
committed
feat(jqLite): don't remove a boolean attribute for .attr(attrName, '')
This change aligns jqLite with jQuery. Ref angular#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 52b1d3e commit 773d749

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/jqLite.js

+1-1
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

+18
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,24 @@ describe('jqLite', function() {
743743
elm.attr('attribute', '');
744744
expect(elm[0].getAttribute('attribute')).toBe('');
745745
});
746+
747+
it('should remove the boolean attribute for a false value', function() {
748+
var elm = jqLite('<select multiple>');
749+
elm.attr('multiple', false);
750+
expect(elm[0].hasAttribute('multiple')).toBe(false);
751+
});
752+
753+
it('should remove the boolean attribute for a null value', function() {
754+
var elm = jqLite('<select multiple>');
755+
elm.attr('multiple', null);
756+
expect(elm[0].hasAttribute('multiple')).toBe(false);
757+
});
758+
759+
it('should not remove the boolean attribute for an empty string as a value', function() {
760+
var elm = jqLite('<select multiple>');
761+
elm.attr('multiple', '');
762+
expect(elm[0].getAttribute('multiple')).toBe('multiple');
763+
});
746764
});
747765

748766

0 commit comments

Comments
 (0)