Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1a05daf

Browse files
jbrunimgol
authored andcommitted
feat(jqLite): implement the detach method
Closes #5461
1 parent b9389b2 commit 1a05daf

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/jqLite.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* - [`contents()`](http://api.jquery.com/contents/)
4747
* - [`css()`](http://api.jquery.com/css/)
4848
* - [`data()`](http://api.jquery.com/data/)
49+
* - [`detach()`](http://api.jquery.com/detach/)
4950
* - [`empty()`](http://api.jquery.com/empty/)
5051
* - [`eq()`](http://api.jquery.com/eq/)
5152
* - [`find()`](http://api.jquery.com/find/) - Limited to lookups by tag name
@@ -437,6 +438,12 @@ function jqLiteEmpty(element) {
437438
}
438439
}
439440

441+
function jqLiteRemove(element, keepData) {
442+
if (!keepData) jqLiteDealoc(element);
443+
var parent = element.parentNode;
444+
if (parent) parent.removeChild(element);
445+
}
446+
440447
//////////////////////////////////////////
441448
// Functions which are declared directly.
442449
//////////////////////////////////////////
@@ -536,7 +543,7 @@ forEach({
536543
return jqLiteInheritedData(element, '$injector');
537544
},
538545

539-
removeAttr: function(element,name) {
546+
removeAttr: function(element, name) {
540547
element.removeAttribute(name);
541548
},
542549

@@ -890,10 +897,10 @@ forEach({
890897
wrapNode.appendChild(element);
891898
},
892899

893-
remove: function(element) {
894-
jqLiteDealoc(element);
895-
var parent = element.parentNode;
896-
if (parent) parent.removeChild(element);
900+
remove: jqLiteRemove,
901+
902+
detach: function(element) {
903+
jqLiteRemove(element, true);
897904
},
898905

899906
after: function(element, newElement) {

test/jqLiteSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,20 @@ describe('jqLite', function() {
460460
}));
461461

462462

463+
it('should keep data if an element is removed via detach()', function() {
464+
var root = jqLite('<div><span>abc</span></div>'),
465+
span = root.find('span'),
466+
data = span.data();
467+
468+
span.data('foo', 'bar');
469+
span.detach();
470+
471+
expect(data).toEqual({foo: 'bar'});
472+
473+
span.remove();
474+
});
475+
476+
463477
it('should retrieve all data if called without params', function() {
464478
var element = jqLite(a);
465479
expect(element.data()).toEqual({});
@@ -1586,6 +1600,16 @@ describe('jqLite', function() {
15861600
});
15871601

15881602

1603+
describe('detach', function() {
1604+
it('should detach', function() {
1605+
var root = jqLite('<div><span>abc</span></div>');
1606+
var span = root.find('span');
1607+
expect(span.detach()).toEqual(span);
1608+
expect(root.html()).toEqual('');
1609+
});
1610+
});
1611+
1612+
15891613
describe('after', function() {
15901614
it('should after', function() {
15911615
var root = jqLite('<div><span></span></div>');

0 commit comments

Comments
 (0)