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

Commit

Permalink
feat(jqLite): implement the detach method
Browse files Browse the repository at this point in the history
Closes #5461
  • Loading branch information
jbruni authored and mgol committed Aug 14, 2014
1 parent b9389b2 commit 1a05daf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* - [`contents()`](http://api.jquery.com/contents/)
* - [`css()`](http://api.jquery.com/css/)
* - [`data()`](http://api.jquery.com/data/)
* - [`detach()`](http://api.jquery.com/detach/)
* - [`empty()`](http://api.jquery.com/empty/)
* - [`eq()`](http://api.jquery.com/eq/)
* - [`find()`](http://api.jquery.com/find/) - Limited to lookups by tag name
Expand Down Expand Up @@ -437,6 +438,12 @@ function jqLiteEmpty(element) {
}
}

function jqLiteRemove(element, keepData) {
if (!keepData) jqLiteDealoc(element);
var parent = element.parentNode;
if (parent) parent.removeChild(element);
}

//////////////////////////////////////////
// Functions which are declared directly.
//////////////////////////////////////////
Expand Down Expand Up @@ -536,7 +543,7 @@ forEach({
return jqLiteInheritedData(element, '$injector');
},

removeAttr: function(element,name) {
removeAttr: function(element, name) {
element.removeAttribute(name);
},

Expand Down Expand Up @@ -890,10 +897,10 @@ forEach({
wrapNode.appendChild(element);
},

remove: function(element) {
jqLiteDealoc(element);
var parent = element.parentNode;
if (parent) parent.removeChild(element);
remove: jqLiteRemove,

detach: function(element) {
jqLiteRemove(element, true);
},

after: function(element, newElement) {
Expand Down
24 changes: 24 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ describe('jqLite', function() {
}));


it('should keep data if an element is removed via detach()', function() {
var root = jqLite('<div><span>abc</span></div>'),
span = root.find('span'),
data = span.data();

span.data('foo', 'bar');
span.detach();

expect(data).toEqual({foo: 'bar'});

span.remove();
});


it('should retrieve all data if called without params', function() {
var element = jqLite(a);
expect(element.data()).toEqual({});
Expand Down Expand Up @@ -1586,6 +1600,16 @@ describe('jqLite', function() {
});


describe('detach', function() {
it('should detach', function() {
var root = jqLite('<div><span>abc</span></div>');
var span = root.find('span');
expect(span.detach()).toEqual(span);
expect(root.html()).toEqual('');
});
});


describe('after', function() {
it('should after', function() {
var root = jqLite('<div><span></span></div>');
Expand Down

0 comments on commit 1a05daf

Please sign in to comment.