Skip to content

Commit c9ef09c

Browse files
committed
bug 6158; fixing replaceWith from throwing errors on non existant elements
1 parent 5c2d709 commit c9ef09c

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/manipulation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,9 @@ jQuery.fn.extend({
261261
}
262262
});
263263
} else {
264-
if ( !this.length ) {
265-
return this;
266-
}
267-
return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
264+
return ( this.length ) ?
265+
this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) :
266+
this;
268267
}
269268
},
270269

test/unit/manipulation.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
739739
});
740740

741741
var testReplaceWith = function(val) {
742-
expect(20);
742+
expect(21);
743743
jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
744744
ok( jQuery("#replace")[0], 'Replace element with string' );
745745
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
@@ -799,6 +799,9 @@ var testReplaceWith = function(val) {
799799
var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
800800
equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
801801
equals( set.length, 1, "Replace the disconnected node." );
802+
803+
var non_existant = jQuery('#does-not-exist').replaceWith( val("<b>should not throw an error</b>") );
804+
equals( non_existant.length, 0, "Length of non existant element." )
802805

803806
var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
804807
// TODO: Work on jQuery(...) inline script execution
@@ -827,7 +830,7 @@ test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
827830
test("replaceWith(Function)", function() {
828831
testReplaceWith(functionReturningObj);
829832

830-
expect(21);
833+
expect(22);
831834

832835
var y = jQuery("#yahoo")[0];
833836

0 commit comments

Comments
 (0)