Skip to content

Commit

Permalink
fix #667: attr({foo: null}) removes attribute foo, like attr('foo', n…
Browse files Browse the repository at this point in the history
…ull)
  • Loading branch information
rwaldin authored and jugglinmike committed Jan 1, 2016
1 parent 70c5608 commit b27bed6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/api/attributes.js
Expand Up @@ -61,8 +61,8 @@ exports.attr = function(name, value) {
if (!isTag(el)) return;

if (typeof name === 'object') {
_.each(name, function(name, key) {
el.attribs[key] = name+'';
_.each(name, function(value, name) {
setAttr(el, name, value);
});
} else {
setAttr(el, name, value);
Expand Down
16 changes: 16 additions & 0 deletions test/api/attributes.js
Expand Up @@ -111,6 +111,22 @@ describe('$(...)', function() {
$apple.removeAttr('data-autofocus');
expect($apple.attr('data-autofocus')).to.be(undefined);
});

it('(key, value) : should remove attributes when called with null value', function() {
var $pear = $('.pear').attr('autofocus', 'autofocus');
expect($pear.attr('autofocus')).to.equal('autofocus');
$pear.attr('autofocus', null);
expect($pear.attr('autofocus')).to.be(undefined);
});

it('(map) : should remove attributes with null values', function() {
var $pear = $('.pear').attr({'autofocus': 'autofocus', 'style': 'color:red'});
expect($pear.attr('autofocus')).to.equal('autofocus');
expect($pear.attr('style')).to.equal('color:red');
$pear.attr({'autofocus': null, 'style': 'color:blue'});
expect($pear.attr('autofocus')).to.be(undefined);
expect($pear.attr('style')).to.equal('color:blue');
});
});

describe('.data', function() {
Expand Down

0 comments on commit b27bed6

Please sign in to comment.