Permalink
Browse files

Merge pull request #20 from Nami-Doc/patch-3

Add .empty
  • Loading branch information...
tj committed Mar 13, 2013
2 parents 607c63c + 104b38e commit 997d6bedc9b41ea3e292fe82e85ca9dcc0bdfbeb
Showing with 44 additions and 2 deletions.
  1. +10 −1 Readme.md
  2. +22 −1 index.js
  3. +12 −0 test/dom.js
View
@@ -259,6 +259,15 @@ dom('ul li').each(function(li, i){
});
```
+### .empty()
+
+ Empties the elements.
+
+```js
+var list = dom('<div><a href="/meow.html">cute kitty</a></div>');
+assert('' == list.empty().html());
+```
+
### .forEach(fn)
Iterate elements passing each one, and its index:
@@ -352,4 +361,4 @@ dom('ul li').last().remove();
## License
- MIT
+ MIT
View
@@ -616,7 +616,7 @@ List.prototype.find = function(selector){
// TODO: real implementation
var list = new List([], this.selector);
var el, els;
- for (var i = 0; i < this.els.length; ++i) {
+ for (var i = 0, len = this.els.length; i < len; ++i) {
el = this.els[i];
els = el.querySelectorAll(selector);
for (var j = 0; j < els.length; ++j) {
@@ -626,6 +626,27 @@ List.prototype.find = function(selector){
return list;
};
+/**
+ * Empty the dom list
+ *
+ * @return self
+ * @api public
+ */
+List.prototype.empty = function(){
+ var elem, el
+ , i = 0;
+
+ for (var i = 0, len = this.els.length; i < len; ++i) {
+ el = this.els[i];
+
+ while (el.firstChild) {
+ el.removeChild(el.firstChild);
+ }
+ }
+
+ return this;
+}
+
/**
* Attribute accessors.
*/
View
@@ -310,6 +310,18 @@ describe('.filter(fn)', function(){
})
})
+describe('.empty()', function(){
+ it('should return itself for chaining', function(){
+ var list = dom('<div></div>');
+ assert(list == list.empty());
+ })
+
+ it('should empty the element(s)', function(){
+ var list = dom('<div><a href="/meow.html">cute kitty</a></div>');
+ assert('' == list.empty().html());
+ })
+})
+
describe('.attr()', function(){
describe('with a key and value', function(){
it('should set the attribute', function(){

0 comments on commit 997d6be

Please sign in to comment.