From 5c2d70979c15bbda5c90e1634abe11d8c350abcb Mon Sep 17 00:00:00 2001 From: Jordan Boesch Date: Sat, 5 Mar 2011 09:30:29 -0600 Subject: [PATCH 1/4] bug 6158; fixing replaceWith from throwing errors on empty elements --- src/manipulation.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/manipulation.js b/src/manipulation.js index ba316971..a4a81de4 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -261,6 +261,9 @@ jQuery.fn.extend({ } }); } else { + if ( !this.length ) { + return this; + } return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ); } }, From c9ef09c800ba7b6510d9e3a68f053ae29409f621 Mon Sep 17 00:00:00 2001 From: Jordan Boesch Date: Sat, 5 Mar 2011 09:46:12 -0600 Subject: [PATCH 2/4] bug 6158; fixing replaceWith from throwing errors on non existant elements --- src/manipulation.js | 7 +++---- test/unit/manipulation.js | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index a4a81de4..613faabb 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -261,10 +261,9 @@ jQuery.fn.extend({ } }); } else { - if ( !this.length ) { - return this; - } - return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ); + return ( this.length ) ? + this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) : + this; } }, diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 34425ed3..1169032c 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -739,7 +739,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() { }); var testReplaceWith = function(val) { - expect(20); + expect(21); jQuery('#yahoo').replaceWith(val( 'buga' )); ok( jQuery("#replace")[0], 'Replace element with string' ); ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' ); @@ -799,6 +799,9 @@ var testReplaceWith = function(val) { var set = jQuery("
").replaceWith(val("test")); equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." ); equals( set.length, 1, "Replace the disconnected node." ); + + var non_existant = jQuery('#does-not-exist').replaceWith( val("should not throw an error") ); + equals( non_existant.length, 0, "Length of non existant element." ) var $div = jQuery("
").appendTo("body"); // TODO: Work on jQuery(...) inline script execution @@ -827,7 +830,7 @@ test("replaceWith(String|Element|Array<Element>|jQuery)", function() { test("replaceWith(Function)", function() { testReplaceWith(functionReturningObj); - expect(21); + expect(22); var y = jQuery("#yahoo")[0]; From dd100bf5ac56d5d44afe45d73fe1824bd0135872 Mon Sep 17 00:00:00 2001 From: Jordan Boesch Date: Sat, 5 Mar 2011 09:59:25 -0600 Subject: [PATCH 3/4] bug 6158; fixing replaceWith from throwing errors on non existant elements; fixing semicolon --- test/unit/manipulation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 1169032c..ff3dff16 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -799,9 +799,9 @@ var testReplaceWith = function(val) { var set = jQuery("
").replaceWith(val("test")); equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." ); equals( set.length, 1, "Replace the disconnected node." ); - + var non_existant = jQuery('#does-not-exist').replaceWith( val("should not throw an error") ); - equals( non_existant.length, 0, "Length of non existant element." ) + equals( non_existant.length, 0, "Length of non existant element." ); var $div = jQuery("
").appendTo("body"); // TODO: Work on jQuery(...) inline script execution From 124acbfbc523614dc5835cfce2c82b867a59986f Mon Sep 17 00:00:00 2001 From: Jordan Boesch Date: Mon, 14 Mar 2011 14:17:02 -0600 Subject: [PATCH 4/4] removing parens --- src/manipulation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manipulation.js b/src/manipulation.js index 613faabb..27f81cc2 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -261,7 +261,7 @@ jQuery.fn.extend({ } }); } else { - return ( this.length ) ? + return this.length ? this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) : this; }