Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

#4087 - insertAfter, insertBefore, etc do not work if origin and destination element is the same #1068

Closed
wants to merge 1 commit into from

2 participants

@dmethvin
Owner

@jquerybot retest

@dmethvin dmethvin closed this pull request from a commit
@PaulBRamos PaulBRamos Fix #4087. insertAfter, insertBefore, etc do not work if origin and d…
…estination are same element. Close gh-1068.
f12611f
@dmethvin dmethvin closed this in f12611f
@tp9 tp9 referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mescoda mescoda referenced this pull request from a commit in mescoda/jquery
@PaulBRamos PaulBRamos Fix #4087. insertAfter, insertBefore, etc do not work if origin and d…
…estination are same element. Close gh-1068.
1cdd78a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 10, 2012
  1. @PaulBRamos
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 4 deletions.
  1. +8 −4 src/manipulation.js
  2. +34 −0 test/unit/manipulation.js
View
12 src/manipulation.js
@@ -254,7 +254,7 @@ jQuery.fn.extend({
// Make sure that the elements are removed from the DOM before they are inserted
// this can help fix replacing a parent with child elements
if ( !isFunc && typeof value !== "string" ) {
- value = jQuery( value ).detach();
+ value = jQuery( value ).not( this ).detach();
}
return this.domManip( [ value ], true, function( elem ) {
@@ -303,7 +303,7 @@ jQuery.fn.extend({
if ( this[0] ) {
doc = this[0].ownerDocument;
fragment = doc.createDocumentFragment();
- jQuery.clean( args, doc, fragment );
+ jQuery.clean( args, doc, fragment, undefined, this );
first = fragment.firstChild;
if ( fragment.childNodes.length === 1 ) {
@@ -630,7 +630,7 @@ jQuery.extend({
return clone;
},
- clean: function( elems, context, fragment, scripts ) {
+ clean: function( elems, context, fragment, scripts, selection ) {
var elem, j, tmp, tag, wrap, tbody,
ret = [],
i = 0,
@@ -725,7 +725,11 @@ jQuery.extend({
safe = jQuery.contains( elem.ownerDocument, elem );
// Append to fragment
- fragment.appendChild( elem );
+ // #4087 - If origin and destination elements are the same, and this is
+ // that element, do not append to fragment
+ if ( !( selection && jQuery.inArray( elem, selection ) !== -1 ) ) {
+ fragment.appendChild( elem );
+ }
tmp = getAll( elem, "script" );
// Preserve script evaluation history
View
34 test/unit/manipulation.js
@@ -2274,3 +2274,37 @@ test( "wrapping scripts (#10470)", function() {
strictEqual( script.parentNode, jQuery("#qunit-fixture > b")[ 0 ], "correctly wrapped" );
jQuery( script ).remove();
});
+
+test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
+
+ expect( 10 );
+
+ var elems;
+
+ jQuery.each([
+ "appendTo",
+ "prependTo",
+ "insertBefore",
+ "insertAfter",
+ "replaceAll"
+ ], function( index, name ) {
+ elems = jQuery( [
+ "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
+ "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
+ "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
+ ] ).appendTo( "#qunit-fixture" );
+
+ // complex case based on http://jsfiddle.net/pbramos/gZ7vB/
+ jQuery("#test4087-complex div")[ name ]("#test4087-complex li:last-child div:last-child");
+ equal( jQuery("#test4087-complex li:last-child div").length, name === "replaceAll" ? 1 : 2, name +" a node to itself, complex case." );
+
+ // simple case
+ jQuery( ".test4087-1" )[ name ](".test4087-1");
+ equal( jQuery(".test4087-1").length, 1, name +" a node to itself, simple case." );
+
+ // clean for next test
+ jQuery("#test4087-complex").remove();
+ jQuery("#test4087-simple").remove();
+ jQuery("#test4087-multiple").remove();
+ });
+});
Something went wrong with that request. Please try again.