Handle the case of an empty string. Fixes #13401. #1163

Closed
wants to merge 2 commits into
from
View
@@ -241,15 +241,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.remove() :
+ this.domManip( [ value ], true, function( elem ) {
+ var next = this.nextSibling,
+ parent = this.parentNode;
+
+ if ( parent ) {
+ jQuery( this ).remove();
+ parent.insertBefore( elem, next );
+ }
+ });
},
detach: function( selector ) {
@@ -2225,3 +2225,20 @@ test( "Make sure specific elements with content created correctly (#13232)", 20,
ok( jQuery.nodeName( this, results[ i ] ) );
});
});
+
+test( "replaceWith() - Handle the case of an empty string (#13401)", 4, function() {
+ var div = jQuery( "<div/>" ),
+ p = jQuery( "<p/>" );
+
+ div.html( p );
+ equal( div.html(), "<p></p>", "Check element exists" );
+
+ p.replaceWith( " " );
+ equal( div.html(), " ", "Check element is replaced" );
+
+ div.html( p );
+ equal( div.html(), "<p></p>", "Check element exists" );
+
+ p.replaceWith( "" );
+ equal( div.html(), "", "Check element is replaced" );
+});