dom-iterator
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,3 +5,3 @@ { | ||
"description": "iterate through DOM nodes", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "iterate", |
0.2.0 / 2014-02-10 | ||
================== | ||
* BREAKING peak no longer chains. | ||
* added it.next([expr], [n]) | ||
0.1.1 / 2014-02-07 | ||
================== | ||
* add iterator context to select, reject | ||
0.1.0 / 2014-02-07 | ||
@@ -3,0 +14,0 @@ ================== |
32
index.js
@@ -126,5 +126,6 @@ /** | ||
var next = dir == 'nextSibling'; | ||
return function walk(expr, peak) { | ||
return function walk(expr, n, peak) { | ||
expr = this.compile(expr); | ||
var node = (peak) ? this.peaked : this.peaked = this.node; | ||
n = n && n > 0 ? n : 1; | ||
var node = this.node; | ||
var closing = this.closingTag; | ||
@@ -155,5 +156,5 @@ var revisit = this._revisit; | ||
if (expr(node) && this.selects(node) && this.rejects(node)) { | ||
if (peak) this.peaked = node; | ||
else this.node = node; | ||
if (expr(node) && this.selects(node, peak) && this.rejects(node, peak)) { | ||
if (--n) continue; | ||
if (!peak) this.node = node; | ||
this.closingTag = closing; | ||
@@ -186,2 +187,4 @@ return node; | ||
* | ||
* @param {Node} node | ||
* @param {Boolean} peak | ||
* @return {Boolean} | ||
@@ -191,3 +194,3 @@ * @api private | ||
iterator.prototype.selects = function(node) { | ||
iterator.prototype.selects = function(node, peak) { | ||
var exprs = this._selects; | ||
@@ -198,3 +201,3 @@ var len = exprs.length; | ||
for (var i = 0; i < len; i++) { | ||
if (exprs[i](node)) return true; | ||
if (exprs[i].call(this, node, peak)) return true; | ||
}; | ||
@@ -223,2 +226,4 @@ | ||
* | ||
* @param {Node} node | ||
* @param {Boolean} peak | ||
* @return {Boolean} | ||
@@ -228,3 +233,3 @@ * @api private | ||
iterator.prototype.rejects = function(node) { | ||
iterator.prototype.rejects = function(node, peak) { | ||
var exprs = this._rejects; | ||
@@ -235,3 +240,3 @@ var len = exprs.length; | ||
for (var i = 0; i < len; i++) { | ||
if (exprs[i](node)) return false; | ||
if (exprs[i].call(this, node, peak)) return false; | ||
}; | ||
@@ -293,10 +298,5 @@ | ||
n = undefined == n ? 1 : n; | ||
var node; | ||
if (!n) return this.node; | ||
else if (n > 0) while(n--) node = this.next(expr, true); | ||
else while(n++) node = this.prev(expr, true); | ||
this.peaked = node; | ||
return node; | ||
else if (n > 0) return this.next(expr, n, true); | ||
else return this.prev(expr, Math.abs(n), true); | ||
}; | ||
@@ -303,0 +303,0 @@ |
{ | ||
"name": "dom-iterator", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "iterator for mini-html-parser", | ||
@@ -16,3 +16,4 @@ "main": "index.js", | ||
"mini-html-parser": "0.0.3", | ||
"mocha": "~1.17.1" | ||
"mocha": "~1.17.1", | ||
"component-test": "~0.1.3" | ||
}, | ||
@@ -19,0 +20,0 @@ "scripts": { |
@@ -41,9 +41,15 @@ | ||
### `iterator#next()` | ||
### `iterator#next([expr], [n])` | ||
Gets the next DOM `node`. If no `node` exists, return `null`. | ||
You may pass an expression `expr`, to grab the first node that | ||
matches `expr`. | ||
Additionally, you can pass a number to select the `nth` node. | ||
Defaults to `1` or the `1st` node. | ||
```js | ||
var node = it.next() | ||
var next = it.next() | ||
// select the 2nd element node we come across | ||
var next = it.next(Node.ElementNode, 2) | ||
``` | ||
@@ -58,6 +64,12 @@ | ||
Gets the previous DOM `node`. If no `node` exists, return `null`. | ||
You may pass an expression `expr`, to grab the first node that | ||
matches `expr`. | ||
Additionally, you can pass a number to select the `nth` node. | ||
Defaults to `1` or the `1st` node. | ||
```js | ||
var node = it.prev() | ||
var prev = it.prev() | ||
// select the 2nd element node we come across | ||
var prev = it.prev(Node.ElementNode, 2) | ||
``` | ||
@@ -83,5 +95,8 @@ | ||
This is basically saying, "select all element nodes or comment nodes | ||
or nodes with the nodeValue "sloth" or nodes that pass the function `fn`". | ||
This is saying: | ||
> select all element nodes or comment nodes | ||
> or nodes with the nodeValue "sloth" or | ||
> nodes that pass the function `fn`. | ||
### `iterator.reject(expr)` | ||
@@ -101,5 +116,8 @@ | ||
This is basically saying, "reject all element nodes and comment nodes | ||
and nodes with the nodeValue sloth and nodes that pass the function `fn`". | ||
This is saying: | ||
> reject all element nodes and comment nodes | ||
> and nodes with the nodeValue sloth and | ||
> nodes that pass the function `fn`". | ||
### `iterator.revisit(revisit)` | ||
@@ -143,3 +161,3 @@ | ||
### `iterator.peak([n])` | ||
### `iterator.peak([expr], [n])` | ||
@@ -158,3 +176,3 @@ Sometimes you want to peak on the following or previous node without actually visiting it. With `peak` you can peak forward or backwards `n` steps. If no `n` is given, peak forward 1 step. | ||
```js | ||
it.peak(-3) // peak backwards 3 steps | ||
it.peak(Node.ElementNode, -3) // peak backwards 3 steps, only selecting element nodes | ||
``` | ||
@@ -176,9 +194,18 @@ | ||
On the server: | ||
```js | ||
npm install component-test | ||
npm install | ||
make test | ||
``` | ||
Or in the browser: | ||
```js | ||
npm install | ||
make test-browser | ||
``` | ||
## License | ||
MIT |
@@ -246,2 +246,3 @@ /** | ||
i = iterator(dom); | ||
console.log(i.peak(3)); | ||
assert('EM' == i.peak(3).nodeName); | ||
@@ -248,0 +249,0 @@ assert('BODY' == i.node.nodeName); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26890
669
205
1
3