Commit 8660ea1a authored by Noah Sloan's avatar Noah Sloan Committed by jeresig

Only detach the incoming elements to replaceWith if they're DOM nodes. Fixes #5986.

parent e76ba32c
...@@ -268,16 +268,17 @@ jQuery.fn.extend({ ...@@ -268,16 +268,17 @@ jQuery.fn.extend({
if ( this[0] && this[0].parentNode ) { if ( this[0] && this[0].parentNode ) {
// Make sure that the elements are removed from the DOM before they are inserted // Make sure that the elements are removed from the DOM before they are inserted
// this can help fix replacing a parent with child elements // this can help fix replacing a parent with child elements
if ( !jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
value = jQuery( value ).detach();
} else {
return this.each(function(i) { return this.each(function(i) {
var self = jQuery(this), old = self.html(); var self = jQuery(this), old = self.html();
self.replaceWith( value.call( this, i, old ) ); self.replaceWith( value.call( this, i, old ) );
}); });
} }
if ( typeof value !== "string" ) {
value = jQuery(value).detach();
}
return this.each(function() { return this.each(function() {
var next = this.nextSibling, parent = this.parentNode; var next = this.nextSibling, parent = this.parentNode;
......
...@@ -650,7 +650,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() { ...@@ -650,7 +650,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
}); });
var testReplaceWith = function(val) { var testReplaceWith = function(val) {
expect(15); expect(17);
jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' )); jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
ok( jQuery("#replace")[0], 'Replace element with string' ); ok( jQuery("#replace")[0], 'Replace element with string' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' ); ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
...@@ -660,6 +660,12 @@ var testReplaceWith = function(val) { ...@@ -660,6 +660,12 @@ var testReplaceWith = function(val) {
ok( jQuery("#first")[0], 'Replace element with element' ); ok( jQuery("#first")[0], 'Replace element with element' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' ); ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
reset();
jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
jQuery('#baz').replaceWith("Baz");
equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
reset(); reset();
jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] )); jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
ok( jQuery("#first")[0], 'Replace element with array of elements' ); ok( jQuery("#first")[0], 'Replace element with array of elements' );
...@@ -721,7 +727,7 @@ test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -721,7 +727,7 @@ test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
test("replaceWith(Function)", function() { test("replaceWith(Function)", function() {
testReplaceWith(functionReturningObj); testReplaceWith(functionReturningObj);
expect(16); expect(18);
var y = jQuery("#yahoo")[0]; var y = jQuery("#yahoo")[0];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment