From 4cafb58ba43a85c1870b1ac6b233af262514997e Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Mon, 13 Apr 2015 16:05:48 -0400 Subject: Manipulation: Detect sneaky no-content replaceWith input Fixes gh-2204 Ref 642e9a45579cfa90861b8ea71a95dd077775caaf Closes gh-1752 Closes gh-2206 (cherry picked from commit 4b27ae16a2b911f75b341b56d9d939bc65a9657a) Conflicts: src/manipulation.js test/unit/manipulation.js --- test/unit/manipulation.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'test/unit') diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 8ce29b3a9..12ea6194b 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1329,15 +1329,20 @@ test( "replaceWith(string) for more than one element", function() { equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced"); }); -test( "Empty replaceWith (#13401; #13596)", 8, function() { - var $el = jQuery( "
" ), +test( "Empty replaceWith (trac-13401; trac-13596; gh-2204)", function() { + + expect( 25 ); + + var $el = jQuery( "
" ).html( "

0

" ), + expectedHTML = $el.html(), tests = { "empty string": "", "empty array": [], + "array of empty string": [ "" ], "empty collection": jQuery( "#nonexistent" ), - // in case of jQuery(...).replaceWith(); - "empty undefined": undefined + // in case of jQuery(...).replaceWith(); + "undefined": undefined }; jQuery.each( tests, function( label, input ) { @@ -1345,6 +1350,17 @@ test( "Empty replaceWith (#13401; #13596)", 8, function() { strictEqual( $el.html(), "", "replaceWith(" + label + ")" ); $el.html( "" ).children().replaceWith(function() { return input; }); strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" ); + $el.html( "" ).children().replaceWith(function( i ) { i; return input; }); + strictEqual( $el.html(), "", "replaceWith(other function returning " + label + ")" ); + $el.html( "

" ).children().replaceWith(function( i ) { + return i ? + input : + jQuery( this ).html( i + "" ); + }); + strictEqual( $el.eq( 0 ).html(), expectedHTML, + "replaceWith(function conditionally returning context)" ); + strictEqual( $el.eq( 1 ).html(), "", + "replaceWith(function conditionally returning " + label + ")" ); }); }); -- cgit v1.2.3