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:
Michał Gołębiowski
2016-09-23 12:51:22 +02:00
parent 4e36245522
commit 3faf450573
2 changed files with 19 additions and 1 deletions
+1 -1
View File
@@ -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);
+18
View File
@@ -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');
});
});