]> source.dussan.org Git - jquery.git/commitdiff
Fix #13810: .replaceWith(nextSibling)
authorRichard Gibson <richard.gibson@gmail.com>
Tue, 23 Apr 2013 01:08:18 +0000 (21:08 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Tue, 23 Apr 2013 01:08:38 +0000 (21:08 -0400)
src/manipulation.js
test/unit/manipulation.js

index 2cc6d8e2094f67004ac271a880e2baab94a48c21..6204ca7c9a6dcaa927cfd4223b3dca37bcc81a99 100644 (file)
@@ -173,6 +173,10 @@ jQuery.fn.extend({
                                parent = args[ i++ ];
 
                        if ( parent ) {
+                               // Don't use the snapshot next if it has moved (#13810)
+                               if ( next && next.parentNode !== parent ) {
+                                       next = this.nextSibling;
+                               }
                                jQuery( this ).remove();
                                parent.insertBefore( elem, next );
                        }
index 7f68674898f9dad7c3db3fcf4b09c1d43fcb6c73..c487efacb1a20ebcd8c1146e8277c32224f984f8 100644 (file)
@@ -879,7 +879,7 @@ test( "insertAfter(String|Element|Array<Element>|jQuery)", function() {
 function testReplaceWith( val ) {
 
        var tmp, y, child, child2, set, non_existent, $div,
-               expected = 26;
+               expected = 29;
 
        expect( expected );
 
@@ -955,6 +955,18 @@ function testReplaceWith( val ) {
        equal( set[0].childNodes.length, 0, "No effect on a disconnected node." );
 
 
+       child = jQuery("#qunit-fixture").children().first();
+       $div = jQuery("<div class='pathological'/>").insertBefore( child );
+       $div.replaceWith( $div );
+       deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
+               "Self-replacement" );
+       $div.replaceWith( child );
+       deepEqual( jQuery("#qunit-fixture").children().first().get(), child.get(),
+               "Replacement with following sibling (#13810)" );
+       deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
+               "Replacement with following sibling (context removed)" );
+
+
        non_existent = jQuery("#does-not-exist").replaceWith( val("<b>should not throw an error</b>") );
        equal( non_existent.length, 0, "Length of non existent element." );