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~88 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7b09235ceed57bbcc26fc2c76147eb4e95ebdb92;p=jquery.git Wrap: Support .unwrap( selector) for selective unwrapping Fixes gh-1744 Closes gh-2003 --- diff --git a/src/wrap.js b/src/wrap.js index ee02aca75..0d9bf0f2e 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 9e9de43fa..dcec277d2 100644 --- a/test/unit/wrap.js +++ b/test/unit/wrap.js @@ -298,6 +298,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 );