Skip to content

Commit

Permalink
fix(attributes): Fix handling of undefined as value in .attr() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Feb 27, 2021
1 parent d706976 commit 98186e8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/api/attributes.js
Expand Up @@ -107,7 +107,7 @@ function setAttr(el, name, value) {
*/
exports.attr = function (name, value) {
// Set the value (with attr map support)
if (typeof name === 'object' || arguments.length > 1) {
if (typeof name === 'object' || value !== undefined) {
if (typeof value === 'function') {
return domEach(this, function (i, el) {
setAttr(el, name, value.call(el, i, el.attribs[name]));
Expand All @@ -127,7 +127,7 @@ exports.attr = function (name, value) {
});
}

return getAttr(this[0], name);
return arguments.length > 1 ? this : getAttr(this[0], name);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/api/attributes.js
Expand Up @@ -149,7 +149,7 @@ describe('$(...)', function () {
it('(chaining) setting attr to undefined returns a $', function () {
var $pear = $('.pear').attr('foo', undefined);
expect($('.pear')).toHaveLength(1);
expect($('.pear').attr('foo')).toBe('undefined'); // TODO this is stringified undefined
expect($('.pear').attr('foo')).toBeUndefined();
expect($pear).toBeInstanceOf($);
});
});
Expand Down

0 comments on commit 98186e8

Please sign in to comment.