Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/manipulation.js
Expand Up @@ -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 ) {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/manipulation.js
Expand Up @@ -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" );
});