diff --git a/src/jqLite.js b/src/jqLite.js index c1b5b36ec444..0cba5536ee7c 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -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 @@ -368,6 +369,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. ////////////////////////////////////////// @@ -450,7 +457,7 @@ forEach({ return jqLiteInheritedData(element, '$injector'); }, - removeAttr: function(element,name) { + removeAttr: function(element, name) { element.removeAttribute(name); }, @@ -796,10 +803,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) { diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 3a8fd4045c5b..95555e0a0723 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -344,6 +344,20 @@ describe('jqLite', function() { })); + it('should keep data if an element is removed via detach()', function() { + var root = jqLite('
abc
'), + 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({}); @@ -1301,6 +1315,16 @@ describe('jqLite', function() { }); + describe('detach', function() { + it('should detach', function() { + var root = jqLite('
abc
'); + var span = root.find('span'); + expect(span.detach()).toEqual(span); + expect(root.html()).toEqual(''); + }); + }); + + describe('after', function() { it('should after', function() { var root = jqLite('
');