From d8f7e8371230a6dd504ca02e65442745b6ff6ce2 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 22 Apr 2013 21:08:18 -0400 Subject: [PATCH] Fix #13810: .replaceWith(nextSibling) --- src/manipulation.js | 4 ++++ test/unit/manipulation.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/manipulation.js b/src/manipulation.js index 2cc6d8e20..6204ca7c9 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -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 ); } diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 7f6867489..c487efacb 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -879,7 +879,7 @@ test( "insertAfter(String|Element|Array|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("
").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("should not throw an error") ); equal( non_existent.length, 0, "Length of non existent element." ); -- 2.39.5