Combinators for selecting DOM nodes.
Treepath is a library for selecting DOM nodes with combinators. It works very
document.querySelector, but can be extended to work with any kind
of tree-like data structure — and is also composable!
var dom = require'domparser'var t = require'treepath'var xml = parseFromString'<div id="site"><a href="/en/foo">Foo</a></div>'documentElement// div atselectttag'div' ttag'a' xml// => ['<a href="/en/foo">...</a>']// *tselect return true xml// => ['<div id="site">...</div>', '<a href="/en/foo">...</a>']// #site, *[href*="/en/"]tselecttortid'site' tattrContains'href' '/en/' xml// => ['<div>...</div>', '<a>...</a>']
Grab it from NPM:
$ npm install treepath
( TBD )
treepath.select basically works like this: it takes a
query, which is a
list of predicate functions, and something fulfilling the DOM
interface. It then returns all nodes that match the given query.
A query is a sequence of steps, where each item of the array is a predicate step. We apply a predicate at the current step to a node, and if it's successful, apply the next step to its children, and so on until there are no more steps. If an item matches successfully at the end the path, we return it.
tag'div' tag'a' tag'span'
Is equivalent to the following CSS expression:
div a span
This library assumes an ES5 environment, but can be easily supported in ES3 platforms by the use of shims. Just include es5-shim :)
Copyright (c) 2013 Quildreen Motta.
Released under the MIT licence.