Skip to content

Commit 482bdb9

Browse files
$.fn.empty: ensuring it supports multi-element collections
1 parent 2b565fe commit 482bdb9

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/manipulation/empty.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ interface Cash {
77

88
Cash.prototype.empty = function ( this: Cash ) {
99

10-
const ele = this[0];
11-
12-
if ( ele ) {
10+
return this.each ( ( i, ele ) => {
1311

1412
while ( ele.firstChild ) {
1513

1614
ele.removeChild ( ele.firstChild );
1715

1816
}
1917

20-
}
21-
22-
return this;
18+
});
2319

2420
};

test/modules/manipulation.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var fixture = '\
44
<div class="anchor">content</div>\
55
</div>\
66
<div class="uncle"></div>\
7-
<div class="aunt"></div>\
7+
<div class="aunt">\
8+
<div class="cousin">content</div>\
9+
</div>\
810
';
911

1012
describe ( 'Manipulation', { beforeEach: getFixtureInit ( fixture ) }, function ( it ) {
@@ -180,7 +182,20 @@ describe ( 'Manipulation', { beforeEach: getFixtureInit ( fixture ) }, function
180182

181183
parent.empty ();
182184

183-
t.is ( parent.contents ().length, 0 );
185+
t.is ( parent.contents ().length, 0 );
186+
187+
});
188+
189+
it ( 'supports multiple elements in the collection', function ( t ) {
190+
191+
var parent = $('.parent');
192+
var aunt = $('.aunt');
193+
var parents = parent.add ( aunt );
194+
195+
parents.empty ();
196+
197+
t.is ( parent.contents ().length, 0 );
198+
t.is ( aunt.contents ().length, 0 );
184199

185200
});
186201

0 commit comments

Comments
 (0)