diff --git a/lib/api/traversing.js b/lib/api/traversing.js index ef12a8e885..089c6d53a2 100644 --- a/lib/api/traversing.js +++ b/lib/api/traversing.js @@ -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); }; /** diff --git a/test/api/traversing.js b/test/api/traversing.js index eea17f020a..09d34d7087 100644 --- a/test/api/traversing.js +++ b/test/api/traversing.js @@ -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! + }); }); });