Skip to content

Commit

Permalink
Merge pull request #411 from xavi-/multi-replaceWith
Browse files Browse the repository at this point in the history
.replaceWith now replaces all selected elements.
  • Loading branch information
davidchambers committed Mar 9, 2014
2 parents 6ce5473 + f699862 commit e74025e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,12 @@ var remove = exports.remove = function(selector) {
};

var replaceWith = exports.replaceWith = function(content) {
var dom = makeDomArray(content);

domEach(this, function(i, el) {
var parent = el.parent || el.root,
siblings = parent.children,
dom = makeDomArray(_.isFunction(content) ? content.call(el, i, el) : content),
index;

if (_.isFunction(content)) {
dom = makeDomArray(content.call(el, i, el));
}

// In the case that `dom` contains nodes that already exist in other
// structures, ensure those nodes are properly removed.
updateDOM(dom, null);
Expand Down
7 changes: 7 additions & 0 deletions test/api.manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,13 @@ describe('$(...)', function() {
expect($.html($src)).to.equal('<h2>hi <div>here</div></h2>');
});

it('(str) : should replace all selected elements', function() {
var $src = $('<b>a<br>b<br>c<br>d</b>');
var $replaced = $src.find('br').replaceWith(' ');
expect($replaced[0].parent).to.equal(null);
expect($.html($src)).to.equal('<b>a b c d</b>');
});

it('(fn) : should invoke the callback with the correct argument and context', function() {
var $fruits = $(fruits);
var origChildren = $fruits.children().toArray();
Expand Down

0 comments on commit e74025e

Please sign in to comment.