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);
This commit is contained in:
+1
-1
@@ -645,7 +645,7 @@ forEach({
|
||||
var lowercasedName = lowercase(name);
|
||||
if (BOOLEAN_ATTR[lowercasedName]) {
|
||||
if (isDefined(value)) {
|
||||
if (value) {
|
||||
if (value !== false && value !== null) {
|
||||
element.setAttribute(name, name);
|
||||
} else {
|
||||
element.removeAttribute(name);
|
||||
|
||||
@@ -733,6 +733,24 @@ describe('jqLite', function() {
|
||||
elm.attr('attribute', '');
|
||||
expect(elm[0].getAttribute('attribute')).toBe('');
|
||||
});
|
||||
|
||||
it('should remove the boolean attribute for a false value', function() {
|
||||
var elm = jqLite('<select multiple>');
|
||||
elm.attr('multiple', false);
|
||||
expect(elm[0].hasAttribute('multiple')).toBe(false);
|
||||
});
|
||||
|
||||
it('should remove the boolean attribute for a null value', function() {
|
||||
var elm = jqLite('<select multiple>');
|
||||
elm.attr('multiple', null);
|
||||
expect(elm[0].hasAttribute('multiple')).toBe(false);
|
||||
});
|
||||
|
||||
it('should not remove the boolean attribute for an empty string as a value', function() {
|
||||
var elm = jqLite('<select multiple>');
|
||||
elm.attr('multiple', '');
|
||||
expect(elm[0].getAttribute('multiple')).toBe('multiple');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user