Skip to content

Commit 69e6e53

Browse files
committed
Adding in .unwrap() support, thanks to Ben Alman! Fixes #5191.
1 parent 70b9aed commit 69e6e53

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/manipulation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ jQuery.fn.extend({
9090
});
9191
},
9292

93+
unwrap: function() {
94+
return this.parent().each(function(){
95+
if ( !jQuery.nodeName( this, "body" ) ) {
96+
jQuery( this ).replaceWith( this.childNodes );
97+
}
98+
}).end();
99+
},
100+
93101
append: function() {
94102
return this.domManip(arguments, true, function(elem){
95103
if ( this.nodeType === 1 ) {

test/unit/manipulation.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,37 @@ test("wrapInner(String|Element)", function() {
116116
// testWrapInner(functionReturningObj)
117117
// })
118118

119+
var testUnwrap = function() {
120+
expect(9);
121+
122+
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 id="unwrap3"> <b><span class="unwrap unwrap3">e</span></b> <b><span class="unwrap unwrap3">f</span></b> </div> </div>');
123+
124+
var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
125+
abcdef = jQuery('#unwrap span').get();
126+
127+
equals( jQuery('#unwrap1 span, #unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
128+
same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
129+
130+
same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
131+
132+
same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
133+
134+
same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
135+
136+
same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
137+
138+
same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
139+
same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
140+
141+
same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
142+
143+
jQuery('body > span.unwrap').remove();
144+
}
145+
146+
test("unwrap()", function() {
147+
testUnwrap();
148+
});
149+
119150
var testAppend = function(valueObj) {
120151
expect(21);
121152
var defaultText = 'Try them out:'

0 commit comments

Comments
 (0)