Skip to content

Commit 9f9b493

Browse files
authored
fix(traversing): Fix .add modifying previous selections (#1656)
Fixes #834
1 parent 5aa4272 commit 9f9b493

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/api/traversing.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -902,14 +902,8 @@ exports.end = function () {
902902
*/
903903
exports.add = function (other, context) {
904904
var selection = this._make(other, context);
905-
var contents = uniqueSort(selection.get().concat(this.get()));
906-
907-
for (var i = 0; i < contents.length; ++i) {
908-
selection[i] = contents[i];
909-
}
910-
selection.length = contents.length;
911-
912-
return selection;
905+
var contents = uniqueSort(this.get().concat(selection.get()));
906+
return this._make(contents);
913907
};
914908

915909
/**

test/api/traversing.js

+15
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,21 @@ describe('$(...)', function () {
13831383
expect($selection[3]).toBe($pear[0]);
13841384
});
13851385
});
1386+
1387+
it('modifying nested selections should not impact the parent [#834]', function () {
1388+
var apple_pear = $apple.add($pear);
1389+
1390+
// applies red to apple and pear
1391+
apple_pear.addClass('red');
1392+
1393+
expect($apple.hasClass('red')).toBe(true); // this is true
1394+
expect($pear.hasClass('red')).toBe(true); // this is true
1395+
1396+
// applies green to pear... AND should not affect apple
1397+
$pear.addClass('green');
1398+
expect($pear.hasClass('green')).toBe(true); //currently this is true
1399+
expect($apple.hasClass('green')).toBe(false); // and this should be false!
1400+
});
13861401
});
13871402
});
13881403

0 commit comments

Comments
 (0)