Skip to content

Commit

Permalink
fix(replaceWith): Fix replacing element with itself (#1581)
Browse files Browse the repository at this point in the history
Fixes #962
  • Loading branch information
fb55 authored Dec 22, 2020
1 parent 3b35ae4 commit 88ae636
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,10 @@ exports.replaceWith = function (content) {

// Completely remove old element
uniqueSplice(siblings, index, 1, dom, parent);
el.parent = el.prev = el.next = null;

if (dom.indexOf(el) < 0) {
el.parent = el.prev = el.next = null;
}
});

return this;
Expand Down
10 changes: 10 additions & 0 deletions test/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,16 @@ describe('$(...)', function () {
expect($.html($src)).to.equal('<h2>hi <div>here</div></h2>');
});

it('(self) : should be replaced after replacing it with itself', function () {
var $ = cheerio.load('<a>foo</a>', null, false);
var replacement = '<a>bar</a>';
$('a').replaceWith(function (i, el) {
return el;
});
$('a').replaceWith(replacement);
expect($.html()).to.be.equal(replacement);
});

it('(str) : should accept strings', function () {
var $src = $('<h2>hi <span>there</span></h2>');
var newStr = '<div>here</div>';
Expand Down

0 comments on commit 88ae636

Please sign in to comment.