Skip to content

Commit 7b09235

Browse files
committed
Wrap: Support .unwrap( selector) for selective unwrapping
Fixes gh-1744 Closes gh-2003
1 parent 06f6cd1 commit 7b09235

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/wrap.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ jQuery.fn.extend({
6363
});
6464
},
6565

66-
unwrap: function() {
67-
return this.parent().each(function() {
68-
if ( !jQuery.nodeName( this, "body" ) ) {
69-
jQuery( this ).replaceWith( this.childNodes );
70-
}
71-
}).end();
66+
unwrap: function( selector ) {
67+
this.parent( selector ).not( "body" ).each(function() {
68+
jQuery( this ).replaceWith( this.childNodes );
69+
});
70+
return this;
7271
}
7372
});
7473

test/unit/wrap.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ test( "unwrap()", function() {
298298
jQuery("body > span.unwrap").remove();
299299
});
300300

301+
test( "unwrap( selector )", function() {
302+
303+
expect( 5 );
304+
305+
jQuery( "body" ).append( " <div id='unwrap' style='display: none;'> <div id='unwrap1'> <span class='unwrap'>a</span> <span class='unwrap'>b</span> </div> <div id='unwrap2'> <span class='unwrap'>c</span> <span class='unwrap'>d</span> </div> </div>" );
306+
307+
// Shouldn't unwrap, no match
308+
jQuery( "#unwrap1 span" ) .unwrap( "#unwrap2" );
309+
equal( jQuery("#unwrap1").length, 1, "still wrapped" );
310+
311+
// Shouldn't unwrap, no match
312+
jQuery( "#unwrap1 span" ) .unwrap( "span" );
313+
equal( jQuery("#unwrap1").length, 1, "still wrapped" );
314+
315+
// Unwraps
316+
jQuery( "#unwrap1 span" ) .unwrap( "#unwrap1" );
317+
equal( jQuery("#unwrap1").length, 0, "unwrapped match" );
318+
319+
// Check return values
320+
deepEqual( jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "quote" ).get(), "return on unmatched unwrap" );
321+
deepEqual( jQuery( "#unwrap2 span" ).get(), jQuery( "#unwrap2 span" ).unwrap( "#unwrap2" ).get(), "return on matched unwrap" );
322+
323+
jQuery("body > span.unwrap").remove();
324+
});
325+
301326
test( "jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() {
302327

303328
expect( 2 );

0 commit comments

Comments
 (0)