Skip to content

Commit

Permalink
fix(traversing): Fix .add modifying previous selections (#1656)
Browse files Browse the repository at this point in the history
Fixes #834
  • Loading branch information
5saviahv authored Jan 9, 2021
1 parent 5aa4272 commit 9f9b493
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 2 additions & 8 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,14 +902,8 @@ exports.end = function () {
*/
exports.add = function (other, context) {
var selection = this._make(other, context);
var contents = uniqueSort(selection.get().concat(this.get()));

for (var i = 0; i < contents.length; ++i) {
selection[i] = contents[i];
}
selection.length = contents.length;

return selection;
var contents = uniqueSort(this.get().concat(selection.get()));
return this._make(contents);
};

/**
Expand Down
15 changes: 15 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,21 @@ describe('$(...)', function () {
expect($selection[3]).toBe($pear[0]);
});
});

it('modifying nested selections should not impact the parent [#834]', function () {
var apple_pear = $apple.add($pear);

// applies red to apple and pear
apple_pear.addClass('red');

expect($apple.hasClass('red')).toBe(true); // this is true
expect($pear.hasClass('red')).toBe(true); // this is true

// applies green to pear... AND should not affect apple
$pear.addClass('green');
expect($pear.hasClass('green')).toBe(true); //currently this is true
expect($apple.hasClass('green')).toBe(false); // and this should be false!
});
});
});

Expand Down

0 comments on commit 9f9b493

Please sign in to comment.