From 7b09235ceed57bbcc26fc2c76147eb4e95ebdb92 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Sun, 11 Jan 2015 22:17:41 -0500 Subject: [PATCH] Wrap: Support .unwrap( selector) for selective unwrapping Fixes gh-1744 Closes gh-2003 --- src/wrap.js | 11 +++++------ test/unit/wrap.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) 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 ); -- 2.39.5