From 03ab9b9f9061df1cf38fd6466c2241dfb2ae9cdc Mon Sep 17 00:00:00 2001 From: Mark Raddatz Date: Fri, 8 Feb 2013 05:43:25 +0800 Subject: [PATCH] Fix #13401: replaceWith(""). Close gh-1163. (cherry picked from commit 6a0ee2d9ed34b81d4ad0662423bf815a3110990f) --- src/manipulation.js | 20 +++++++++++--------- test/unit/manipulation.js | 9 +++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index 3e14c2b56..3a4f9a688 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -265,15 +265,17 @@ jQuery.fn.extend({ value = jQuery( value ).not( this ).detach(); } - return this.domManip( [ value ], true, function( elem ) { - var next = this.nextSibling, - parent = this.parentNode; - - if ( parent ) { - jQuery( this ).remove(); - parent.insertBefore( elem, next ); - } - }); + return value !== "" ? + this.domManip( [ value ], true, function( elem ) { + var next = this.nextSibling, + parent = this.parentNode; + + if ( parent ) { + jQuery( this ).remove(); + parent.insertBefore( elem, next ); + } + }) : + this.remove(); }, detach: function( selector ) { diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 73313ea97..b0736fbf6 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1261,6 +1261,15 @@ test( "replaceWith(string) for more than one element", function() { equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced"); }); +test( "replaceWith(\"\") (#13401)", 4, function() { + expect( 1 ); + + var div = jQuery("

"); + + div.children().replaceWith(""); + equal( div.html().toLowerCase(), "", "Replacing with empty string removes element" ); +}); + test( "replaceAll(String|Element|Array|jQuery)", function() { expect( 10 ); -- 2.39.5