From f6998624d40112f1e8b3f462a2faf81020ae753d Mon Sep 17 00:00:00 2001 From: Xavi Date: Sat, 8 Mar 2014 22:55:16 -0800 Subject: [PATCH] .replaceWith now replaces all selected elements. --- lib/api/manipulation.js | 7 +------ test/api.manipulation.js | 7 +++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 4ae89db05f..bbab180fa8 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -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); diff --git a/test/api.manipulation.js b/test/api.manipulation.js index 1e5cce9fc5..7affc37c98 100644 --- a/test/api.manipulation.js +++ b/test/api.manipulation.js @@ -583,6 +583,13 @@ describe('$(...)', function() { expect($.html($src)).to.equal('

hi
here

'); }); + it('(str) : should replace all selected elements', function() { + var $src = $('a
b
c
d
'); + var $replaced = $src.find('br').replaceWith(' '); + expect($replaced[0].parent).to.equal(null); + expect($.html($src)).to.equal('a b c d'); + }); + it('(fn) : should invoke the callback with the correct argument and context', function() { var $fruits = $(fruits); var origChildren = $fruits.children().toArray();