Skip to content

Commit 642e9a4

Browse files
committedSep 17, 2013
Simplify replaceWith method. Closes gh-1276
1 parent 80538b0 commit 642e9a4

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed
 

‎src/manipulation.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -442,38 +442,28 @@ jQuery.fn.extend({
442442
},
443443

444444
replaceWith: function() {
445-
var
446-
// Snapshot the DOM in case .domManip sweeps something relevant into its fragment
447-
args = jQuery.map( this, function( elem ) {
448-
return [ elem.nextSibling, elem.parentNode ];
449-
}),
450-
i = 0;
445+
var arg = arguments[ 0 ];
451446

452447
// Make the changes, replacing each context element with the new content
453448
this.domManip( arguments, function( elem ) {
454-
var next = args[ i++ ],
455-
parent = args[ i++ ];
449+
arg = this.parentNode;
456450

457-
if ( parent ) {
458-
// Don't use the snapshot next if it has moved (#13810)
459-
if ( next && next.parentNode !== parent ) {
460-
next = this.nextSibling;
461-
}
462-
jQuery( this ).remove();
463-
parent.insertBefore( elem, next );
451+
jQuery.cleanData( getAll( this ) );
452+
453+
if ( arg ) {
454+
arg.replaceChild( elem, this );
464455
}
465-
// Allow new content to include elements from the context set
466-
}, true );
456+
});
467457

468458
// Force removal if there was no new content (e.g., from empty arguments)
469-
return i ? this : this.remove();
459+
return arg && (arg.length || arg.nodeType) ? this : this.remove();
470460
},
471461

472462
detach: function( selector ) {
473463
return this.remove( selector, true );
474464
},
475465

476-
domManip: function( args, callback, allowIntersection ) {
466+
domManip: function( args, callback ) {
477467

478468
// Flatten any nested arrays
479469
args = concat.apply( [], args );
@@ -495,12 +485,12 @@ jQuery.fn.extend({
495485
if ( isFunction ) {
496486
args[ 0 ] = value.call( this, index, self.html() );
497487
}
498-
self.domManip( args, callback, allowIntersection );
488+
self.domManip( args, callback );
499489
});
500490
}
501491

502492
if ( l ) {
503-
fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, !allowIntersection && this );
493+
fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
504494
first = fragment.firstChild;
505495

506496
if ( fragment.childNodes.length === 1 ) {

‎test/unit/manipulation.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1010,20 +1010,21 @@ test( "replaceWith(string) for more than one element", function() {
10101010
equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced");
10111011
});
10121012

1013-
test( "empty replaceWith (#13401; #13596)", 4, function() {
1014-
expect( 6 );
1015-
1016-
var $el = jQuery("<div/>"),
1013+
test( "Empty replaceWith (#13401; #13596)", 8, function() {
1014+
var $el = jQuery( "<div/>" ),
10171015
tests = {
10181016
"empty string": "",
10191017
"empty array": [],
1020-
"empty collection": jQuery("#nonexistent")
1018+
"empty collection": jQuery( "#nonexistent" ),
1019+
1020+
// in case of jQuery(...).replaceWith();
1021+
"empty undefined": undefined
10211022
};
10221023

10231024
jQuery.each( tests, function( label, input ) {
1024-
$el.html("<a/>").children().replaceWith( input );
1025+
$el.html( "<a/>" ).children().replaceWith( input );
10251026
strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
1026-
$el.html("<b/>").children().replaceWith(function() { return input; });
1027+
$el.html( "<b/>" ).children().replaceWith(function() { return input; });
10271028
strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
10281029
});
10291030
});

0 commit comments

Comments
 (0)