feat(jqLite): implement the detach method

Closes #5461
This commit is contained in:
J. Bruni
2013-12-18 02:22:53 -02:00
committed by Michał Gołębiowski
parent b9389b26ba
commit 1a05daf5dc
2 changed files with 36 additions and 5 deletions

View File

@@ -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
@@ -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.
//////////////////////////////////////////
@@ -536,7 +543,7 @@ forEach({
return jqLiteInheritedData(element, '$injector');
},
removeAttr: function(element,name) {
removeAttr: function(element, name) {
element.removeAttribute(name);
},
@@ -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) {

View File

@@ -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({});
@@ -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>');