npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dom-iterator

v1.0.0

Published

iterator for mini-html-parser

Downloads

425,643

Readme

dom-iterator

Feature-rich, well-tested Iterator for traversing DOM nodes. A better version of NodeIterator. Travels in both directions.

Can be used in node.js with mini-html-parser.

Installation

Install with component(1):

$ component install matthewmueller/dom-iterator

With node.js:

$ npm install dom-iterator

Example

var it = iterator(node);
var next;

while (next = it.next(Node.TEXT_NODE)) {
  console.log(next.nodeValue) // next textnodes after node
}

API

iterator(node, root)

Initialize an iterator starting on the node. Optionally you can specify a root to limit your traversal to a particular subtree. root must be either a parent or an ancestor of node.

var it = iterator(el.firstChild, el)

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.

var node = it.next()
// select the 2nd element node we come across
var next = it.next(Node.ELEMENT_NODE, 2)

Here's a look at how the DOM is traversed:

next

iterator#prev(), iterator#previous()

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.

var node = it.prev()
// select the 2nd element node we come across
var prev = it.prev(Node.ELEMENT_NODE, 2)

Here's a look at how the DOM is traversed:

prev

iterator.select(expr)

iterate over nodes that pass the expression expr. The expr can be an enum, number, string or function. If it's a number, the nodeType is compared.

This function can be chained where all expressions are OR-ed.

it.select(Node.ELEMENT_NODE)
  .select(8)
  .select('nodeValue == "sloth"')
  .select(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)

iterate over nodes that do not pass the expression expr. The expr can be an enum, number, string or function. If it's a number, the nodeType is compared.

This function can be chained where all expressions are AND-ed.

it.reject(Node.ELEMENT_NODE)
  .reject(8)
  .reject('nodeValue == "sloth"')
  .reject(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)

You can also skip over elements you already visited, by setting revisit to false. By default, revisit is set to true.

it.revisit(false);

Here's how that would change the iterator:

it.next():

next

it.prev()

prev

iterator.opening()

Jump to the opening tag of an element. This is the default.

var dom = domify('<em>hi</em>');
var it = it(dom).opening()
it.next() // 'hi'

iterator.closing()

Jump to the closing tag of an element

var dom = domify('<em>hi</em>');
var it = it(dom).closing()
it.prev() // 'hi'

iterator.peek([expr], [n])

Sometimes you want to peek on the following or previous node without actually visiting it. With peek you can peek forward or backwards n steps. If no n is given, peek forward 1 step.

Peaking forward:

it.peek(); // peek forward 1
it.peek(3); // peek forward 3 steps

Peaking backwards:

it.peek(Node.ELEMENT_NODE, -3) // peek backwards 3 steps, only selecting element nodes

iterator.reset([newNode])

Reset the iterator to the original node. Optionally pass a newNode to start at.

it.reset();

iterator.use(fn)

Add a plugin to the iterator.

Run Tests

On the server:

npm install
make test

Or in the browser:

npm install
make test-browser

License

MIT