feat(jqLite): remove the attribute for .attr(attribute, null)

This change aligns jqLite with jQuery.

Also, the extra `2` second parameter to `setAttribute` has been removed;
it was only needed for IE<9 and latest jQuery doesn't pass it either.

Ref #15126

BREAKING CHANGE: Invoking `elem.attr(attributeName, null)` would set the
`attributeName` atribute value to a string `"null"`, now it removes the
attribute instead.

To migrate the code follow the example below:

Before:

elem.attr(attributeName, null);

After:

elem.attr(attributeName, "null");
This commit is contained in:
Michał Gołębiowski
2016-09-23 12:26:52 +02:00
parent 7ceb5f6fcc
commit 4e36245522
2 changed files with 18 additions and 4 deletions
+6 -4
View File
@@ -654,11 +654,13 @@ forEach({
return element.getAttribute(name) != null ? lowercasedName : undefined;
}
} else if (isDefined(value)) {
element.setAttribute(name, value);
if (value === null) {
element.removeAttribute(name);
} else {
element.setAttribute(name, value);
}
} else if (element.getAttribute) {
// the extra argument "2" is to get the right thing for a.href in IE, see jQuery code
// some elements (e.g. Document) don't have get attribute, so return undefined
var ret = element.getAttribute(name, 2);
var ret = element.getAttribute(name);
// normalize non-existing attributes to undefined (as jQuery)
return ret === null ? undefined : ret;
}
+12
View File
@@ -721,6 +721,18 @@ describe('jqLite', function() {
expect(comment.attr('some-attribute','somevalue')).toEqual(comment);
expect(comment.attr('some-attribute')).toBeUndefined();
});
it('should remove the attribute for a null value', function() {
var elm = jqLite('<div attribute="value">a</div>');
elm.attr('attribute', null);
expect(elm[0].hasAttribute('attribute')).toBe(false);
});
it('should not remove the attribute for an empty string as a value', function() {
var elm = jqLite('<div attribute="value">a</div>');
elm.attr('attribute', '');
expect(elm[0].getAttribute('attribute')).toBe('');
});
});