mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Fixes #12449. make replaceWith() clone elements where required. Closes gh-920
This commit is contained in:
parent
13449a99b2
commit
551c2c9f4a
@ -258,32 +258,29 @@ jQuery.fn.extend({
|
||||
value = jQuery( value ).detach();
|
||||
}
|
||||
|
||||
this.each( function( i ) {
|
||||
var next = this.nextSibling,
|
||||
parent = this.parentNode,
|
||||
// HTML argument replaced by "this" element
|
||||
// 1. There were no supporting tests
|
||||
// 2. There was no internal code relying on this
|
||||
// 3. There was no documentation of an html argument
|
||||
val = !isFunc ? value : value.call( this, i, this );
|
||||
return this.domManip( [ value ], true, function( elem, i ) {
|
||||
var next, parent;
|
||||
|
||||
if ( isDisconnected( this ) ) {
|
||||
// for disconnected elements, we replace with the new content in the set. We use
|
||||
// clone here to ensure that each replaced instance is unique
|
||||
self[ i ] = jQuery( val ).clone()[ 0 ];
|
||||
// for disconnected elements, we simply replace
|
||||
// with the new content in the set
|
||||
self[ i ] = elem;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( this.nodeType === 1 || this.nodeType === 11 ) {
|
||||
next = this.nextSibling;
|
||||
parent = this.parentNode;
|
||||
|
||||
jQuery( this ).remove();
|
||||
|
||||
if ( next ) {
|
||||
jQuery( next ).before( val );
|
||||
next.parentNode.insertBefore( elem, next );
|
||||
} else {
|
||||
jQuery( parent ).append( val );
|
||||
parent.appendChild( elem );
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
detach: function( selector ) {
|
||||
@ -344,7 +341,8 @@ jQuery.fn.extend({
|
||||
table && jQuery.nodeName( this[i], "table" ) ?
|
||||
findOrAppend( this[i], "tbody" ) :
|
||||
this[i],
|
||||
node
|
||||
node,
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -602,6 +602,23 @@ test( "replaceWith on XML document (#9960)", function() {
|
||||
equal( newNode.length, 1, "ReplaceWith not working on document nodes." );
|
||||
});
|
||||
|
||||
// #12449
|
||||
test( "replaceWith([]) where replacing element requires cloning", function () {
|
||||
expect(2);
|
||||
jQuery("#qunit-fixture").append(
|
||||
"<div class='replaceable'></div><div class='replaceable'></div>"
|
||||
);
|
||||
// replacing set needs to be cloned so it can cover 3 replacements
|
||||
jQuery("#qunit-fixture .replaceable").replaceWith(
|
||||
jQuery("<span class='replaced'></span><span class='replaced'></span>")
|
||||
);
|
||||
equal( jQuery("#qunit-fixture").find(".replaceable").length, 0,
|
||||
"Make sure replaced elements were removed" );
|
||||
equal( jQuery("#qunit-fixture").find(".replaced").length, 4,
|
||||
"Make sure replacing elements were cloned" );
|
||||
});
|
||||
|
||||
|
||||
test( "append the same fragment with events (Bug #6997, 5566)", function() {
|
||||
|
||||
expect( 2 + ( doExtra ? 1 : 0 ) );
|
||||
|
Loading…
Reference in New Issue
Block a user