Skip to content

Commit 551c2c9

Browse files
rvaggrwaldron
authored andcommitted
Fixes #12449. make replaceWith() clone elements where required. Closes jquerygh-920
1 parent 13449a9 commit 551c2c9

File tree

2 files changed

+128
-113
lines changed

2 files changed

+128
-113
lines changed

src/manipulation.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,29 @@ jQuery.fn.extend({
258258
value = jQuery( value ).detach();
259259
}
260260

261-
this.each( function( i ) {
262-
var next = this.nextSibling,
263-
parent = this.parentNode,
264-
// HTML argument replaced by "this" element
265-
// 1. There were no supporting tests
266-
// 2. There was no internal code relying on this
267-
// 3. There was no documentation of an html argument
268-
val = !isFunc ? value : value.call( this, i, this );
261+
return this.domManip( [ value ], true, function( elem, i ) {
262+
var next, parent;
269263

270264
if ( isDisconnected( this ) ) {
271-
// for disconnected elements, we replace with the new content in the set. We use
272-
// clone here to ensure that each replaced instance is unique
273-
self[ i ] = jQuery( val ).clone()[ 0 ];
265+
// for disconnected elements, we simply replace
266+
// with the new content in the set
267+
self[ i ] = elem;
274268
return;
275269
}
276270

277-
jQuery( this ).remove();
271+
if ( this.nodeType === 1 || this.nodeType === 11 ) {
272+
next = this.nextSibling;
273+
parent = this.parentNode;
278274

279-
if ( next ) {
280-
jQuery( next ).before( val );
281-
} else {
282-
jQuery( parent ).append( val );
275+
jQuery( this ).remove();
276+
277+
if ( next ) {
278+
next.parentNode.insertBefore( elem, next );
279+
} else {
280+
parent.appendChild( elem );
281+
}
283282
}
284283
});
285-
286-
return this;
287284
},
288285

289286
detach: function( selector ) {
@@ -344,7 +341,8 @@ jQuery.fn.extend({
344341
table && jQuery.nodeName( this[i], "table" ) ?
345342
findOrAppend( this[i], "tbody" ) :
346343
this[i],
347-
node
344+
node,
345+
i
348346
);
349347
}
350348

0 commit comments

Comments
 (0)