Permalink
Browse files

Wrap: Support .unwrap( selector) for selective unwrapping

Fixes gh-1744
Closes gh-2003
  • Loading branch information...
dmethvin committed Jan 12, 2015
1 parent 06f6cd1 commit 7b09235ceed57bbcc26fc2c76147eb4e95ebdb92
Showing with 30 additions and 6 deletions.
  1. +5 −6 src/wrap.js
  2. +25 −0 test/unit/wrap.js
View
@@ -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;
}
});
View
@@ -298,6 +298,31 @@ test( "unwrap()", function() {
jQuery("body > span.unwrap").remove();
});
test( "unwrap( selector )", function() {
expect( 5 );
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>" );
// Shouldn't unwrap, no match
jQuery( "#unwrap1 span" ) .unwrap( "#unwrap2" );

This comment has been minimized.

Show comment
Hide comment
@arthurvr

arthurvr Mar 30, 2015

Member

) .unwrap

Why the space?

@arthurvr

arthurvr Mar 30, 2015

Member

) .unwrap

Why the space?

This comment has been minimized.

Show comment
Hide comment
@dmethvin

dmethvin Mar 30, 2015

Member

Yeah, those shouldn't be there and I guess JSCS doesn't check for that case. However I think I'll leave it until a bigger style sweep since this doesn't conform as-is and hopefully we can automate this whole process.

@dmethvin

dmethvin Mar 30, 2015

Member

Yeah, those shouldn't be there and I guess JSCS doesn't check for that case. However I think I'll leave it until a bigger style sweep since this doesn't conform as-is and hopefully we can automate this whole process.

This comment has been minimized.

Show comment
Hide comment
@arthurvr

arthurvr Mar 30, 2015

Member

Sure

@arthurvr
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(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() {
expect( 2 );

0 comments on commit 7b09235

Please sign in to comment.