Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add .empty #20

Merged
merged 4 commits into from

2 participants

@vendethiel
Owner

I think we can import some more methods (allow .html() to set innerHTML, for example) and I can do a bigger PR if you want.
Maybe use component/css ?

test/dom.js
@@ -246,6 +246,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></div>');
+ assert('' == list.empty().html());
@tj Owner
tj added a note

we should match jquery here, with jquery if you had <div>Hello</div> you'd end up with <div></div>, also we could probably get away with .innerHTML = '' to speed things up

@vendethiel Owner

will do when I get home. does this needs a rebase too ?

@tj Owner
tj added a note

nope I think this PR is fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vendethiel
Owner

@visionmedia I used removeChild() because that's what jquery is doing. I have no idea why OTTOMH, do you ?

@tj
Owner
tj commented

weird maybe it is faster then (havent benched, too early for that), sounds good then!

@vendethiel vendethiel closed this
@vendethiel
Owner

actually this one was the ok one - no need for the other pr since you did it yourself

@vendethiel vendethiel reopened this
@tj
Owner
tj commented

yup this is all good, just needs a rebase

@vendethiel
Owner

Voilà, the conflict was on readme

@tj tj merged commit 997d6be into component:master
@vendethiel vendethiel deleted the unknown repository branch
@vendethiel
Owner

thanks!

@tj tj commented on the diff
@@ -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) {
@tj Owner
tj added a note

no need for this browsers can optimize that away

@vendethiel Owner

I've learnt it that way, will remember from now on :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vendethiel
Owner

weird maybe it is faster then (havent benched, too early for that), sounds good then!

Definitely faster.

http://jsperf.com/emptify-element/2
innerHTML = null : 1,890Ops
firstChild.remove() : 61,250,827Ops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 12, 2013
  1. @vendethiel

    Add .empty

    vendethiel authored
Commits on Jan 19, 2013
  1. @vendethiel
Commits on Feb 28, 2013
  1. @vendethiel
Commits on Mar 1, 2013
  1. @vendethiel

    merge with upstream

    vendethiel authored
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 2 deletions.
  1. +10 −1 Readme.md
  2. +22 −1 index.js
  3. +12 −0 test/dom.js
View
11 Readme.md
@@ -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
23 index.js
@@ -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) {
@tj Owner
tj added a note

no need for this browsers can optimize that away

@vendethiel Owner

I've learnt it that way, will remember from now on :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
el = this.els[i];
els = el.querySelectorAll(selector);
for (var j = 0; j < els.length; ++j) {
@@ -627,6 +627,27 @@ List.prototype.find = function(selector){
};
/**
+ * 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
12 test/dom.js
@@ -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(){
Something went wrong with that request. Please try again.