Skip to content

Commit f12611f

Browse files
PaulBRamosdmethvin
authored andcommitted
Fix jquery#4087. insertAfter, insertBefore, etc do not work if origin and destination are same element. Close jquerygh-1068.
1 parent aad235b commit f12611f

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/manipulation.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ jQuery.fn.extend({
254254
// Make sure that the elements are removed from the DOM before they are inserted
255255
// this can help fix replacing a parent with child elements
256256
if ( !isFunc && typeof value !== "string" ) {
257-
value = jQuery( value ).detach();
257+
value = jQuery( value ).not( this ).detach();
258258
}
259259

260260
return this.domManip( [ value ], true, function( elem ) {
@@ -303,7 +303,7 @@ jQuery.fn.extend({
303303
if ( this[0] ) {
304304
doc = this[0].ownerDocument;
305305
fragment = doc.createDocumentFragment();
306-
jQuery.clean( args, doc, fragment );
306+
jQuery.clean( args, doc, fragment, undefined, this );
307307
first = fragment.firstChild;
308308

309309
if ( fragment.childNodes.length === 1 ) {
@@ -619,7 +619,7 @@ jQuery.extend({
619619
return clone;
620620
},
621621

622-
clean: function( elems, context, fragment, scripts ) {
622+
clean: function( elems, context, fragment, scripts, selection ) {
623623
var elem, j, tmp, tag, wrap, tbody,
624624
ret = [],
625625
i = 0,
@@ -714,7 +714,11 @@ jQuery.extend({
714714
safe = jQuery.contains( elem.ownerDocument, elem );
715715

716716
// Append to fragment
717-
fragment.appendChild( elem );
717+
// #4087 - If origin and destination elements are the same, and this is
718+
// that element, do not append to fragment
719+
if ( !( selection && jQuery.inArray( elem, selection ) !== -1 ) ) {
720+
fragment.appendChild( elem );
721+
}
718722
tmp = getAll( elem, "script" );
719723

720724
// Preserve script evaluation history

test/unit/manipulation.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,3 +2274,37 @@ test( "wrapping scripts (#10470)", function() {
22742274
strictEqual( script.parentNode, jQuery("#qunit-fixture > b")[ 0 ], "correctly wrapped" );
22752275
jQuery( script ).remove();
22762276
});
2277+
2278+
test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
2279+
2280+
expect( 10 );
2281+
2282+
var elems;
2283+
2284+
jQuery.each([
2285+
"appendTo",
2286+
"prependTo",
2287+
"insertBefore",
2288+
"insertAfter",
2289+
"replaceAll"
2290+
], function( index, name ) {
2291+
elems = jQuery( [
2292+
"<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
2293+
"<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
2294+
"<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
2295+
] ).appendTo( "#qunit-fixture" );
2296+
2297+
// complex case based on http://jsfiddle.net/pbramos/gZ7vB/
2298+
jQuery("#test4087-complex div")[ name ]("#test4087-complex li:last-child div:last-child");
2299+
equal( jQuery("#test4087-complex li:last-child div").length, name === "replaceAll" ? 1 : 2, name +" a node to itself, complex case." );
2300+
2301+
// simple case
2302+
jQuery( ".test4087-1" )[ name ](".test4087-1");
2303+
equal( jQuery(".test4087-1").length, 1, name +" a node to itself, simple case." );
2304+
2305+
// clean for next test
2306+
jQuery("#test4087-complex").remove();
2307+
jQuery("#test4087-simple").remove();
2308+
jQuery("#test4087-multiple").remove();
2309+
});
2310+
});

0 commit comments

Comments
 (0)