From: Dave Methvin Date: Mon, 12 Jan 2015 03:17:41 +0000 (-0500) Subject: Wrap: Support .unwrap( selector) for selective unwrapping X-Git-Tag: 3.0.0-alpha1+compat~93 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e38138af6ac3fa850a092adf09366a68850189a8;p=jquery.git Wrap: Support .unwrap( selector) for selective unwrapping Fixes gh-1744 Closes gh-2003 (cherry picked from commit 7b09235ceed57bbcc26fc2c76147eb4e95ebdb92) --- diff --git a/src/wrap.js b/src/wrap.js index 053b9b5b9..acab1ae95 100644 --- a/src/wrap.js +++ b/src/wrap.js @@ -63,12 +63,11 @@ jQuery.fn.extend({ }); }, - unwrap: function() { - return this.parent().each(function() { - if ( !jQuery.nodeName( this, "body" ) ) { - jQuery( this ).replaceWith( this.childNodes ); - } - }).end(); + unwrap: function( selector ) { + this.parent( selector ).not( "body" ).each(function() { + jQuery( this ).replaceWith( this.childNodes ); + }); + return this; } }); diff --git a/test/unit/wrap.js b/test/unit/wrap.js index ffc5a92e7..97647f809 100644 --- a/test/unit/wrap.js +++ b/test/unit/wrap.js @@ -330,6 +330,31 @@ test( "unwrap()", function() { jQuery("body > span.unwrap").remove(); }); +test( "unwrap( selector )", function() { + + expect( 5 ); + + jQuery( "body" ).append( " " ); + + // Shouldn't unwrap, no match + jQuery( "#unwrap1 span" ) .unwrap( "#unwrap2" ); + equal( jQuery("#unwrap1").length, 1, "still wrapped" ); + + // Shouldn't unwrap, no match + jQuery( "#unwrap1 span" ) .unwrap( "span" ); + equal( jQuery("#unwrap1").length, 1, "still wrapped" ); + + // Unwraps + jQuery( "#unwrap1 span" ) .unwrap( "#unwrap1" ); + equal( jQuery("#unwrap1").length, 0, "unwrapped match" ); + + // Check return values + deepEqual( jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "quote" ).get(), "return on unmatched unwrap" ); + deepEqual( jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "#unwrap2" ).get(), "return on matched unwrap" ); + + jQuery("body > span.unwrap").remove(); +}); + test( "jQuery() & wrap[Inner/All]() handle unknown elems (#10667)", function() { expect( 2 );