Members
goDown
Return first (leftmost) child. If this node is a leaf, return self.
leftmostLeaf
Return leftmost descendant node. If this node is a leaf it will return itself.
leftmostNode
Return leftmost child node or null if this node has no children.
rightmostLeaf
Return rightmost descendant node. If this node is a leaf it will return itself.
rightmostNode
Return rightmost child node or null if this node has no children.
Methods
ancestor(traverseopt, callbackopt) → {Nodes}
Traverse this nodes ancestors. Same as calling each
with up
specified.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
traverse |
TraverseLike
|
<optional> |
A Traverse literal |
callback |
eachNode
|
<optional> |
A function to be called on the selected node, if any |
Returns:
- Type:
-
Nodes
An array of selected nodes, or a single node if first
or last
are specified.
Returns null
if first
or last
are specified and nothing was selected.
Example
// select any 'A' ancestors
node.ancestor('A')
// same as above
node.each({id: 'A', up: true})
attr(a, b) → {ast}
Assign an attribute
Parameters:
Name | Type | Description |
---|---|---|
a |
string
|
Object
|
the key to assign, or an object to merge with the nodes attributes |
b |
*
|
the value to assign to key |
climb(nopt, callbackopt) → (nullable) {AST}
Return the nth parent node.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
n |
number
|
<optional> |
0 |
ancestor to select |
callback |
eachNode
|
<optional> |
A function to be called on the selected node, if any |
Example
// return third ancestor
node.climb(2)
// same as above
node.each({up: true, last: true, depth: 2})
each(traverseopt, callbackopt) → {Nodes}
Perform a single traversal of the tree.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
traverse |
TraverseLike
|
<optional> |
A Traverse literal |
callback |
eachNode
|
<optional> |
A function to be called on each selected node |
Returns:
- Type:
-
Nodes
An array of selected nodes, or a single node if first
or last
are specified.
Returns null
if first
or last
are specified and nothing was selected.
Example
node.each() // return all descendants of `node`
node.each({self: true}) // return `node` and all of its descendants
node.each('Section') // return all descendants with id `Section`
first(traverseopt, callbackopt) → (nullable) {AST}
Return the first descendant selected by the given traverse. Same as calling
each
with first
specified.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
traverse |
TraverseLike
|
<optional> |
A Traverse literal |
callback |
eachNode
|
<optional> |
A function to be called on the selected node, if any |
Example
// select first 'A' node
node.first('A')
// same as above
node.each({id: 'A', first: true})
hasTag(…tags) → {boolean}
Returns true if the node has all of the given tags.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tags |
string
|
<repeatable> |
zero or more tags to check |
Returns:
- Type:
-
boolean
loc(data) → {AST}
Set location data for this node
Parameters:
Name | Type | Description |
---|---|---|
data |
Object
|
location data to be assigned |
match(…query) → {boolean}
Match this node against multiple queries
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
QueryLike
|
<repeatable> |
one or more QueryLikes to match against |
Returns:
- Type:
-
boolean
Returns true if any of the queries match
mutate(opt) → {AST}
Mutate this node in place, replacing any of its properties with those specified in the config object
Parameters:
Name | Type | Description |
---|---|---|
opt |
ASTConf
|
an ASTConf object |
read(k) → {*}
Merge the attributes of this node with all of its descendant nodes,
and return the value of k
Parameters:
Name | Type | Description |
---|---|---|
k |
string
|
the key to read |
Returns:
- Type:
-
*
The value of k
remove(nopt)
Remove a child node n. If n is not given, remove this node. with those specified in the config object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
n |
AST
|
<optional> |
an AST node |
replace(a, bopt) → {AST}
Replace child node a
with node b
If b
is not given, replace self with a
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
a |
AST
|
the child node to replaced, or the node to replace self with |
|
b |
AST
|
<optional> |
the node to replace child node |
select(…traverseopt, callbackopt) → {Array.<AST>}
Perform selections on the tree from a sequence of traversals. Returns the selected nodes if no callback is given.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
traverse |
TraverseLike
|
<optional> <repeatable> |
Zero-or-more Traverse literals |
callback |
eachNode
|
<optional> |
A function to be called on each selected node |
Example
// similar to the css selector `A .foo > B`
node.select('A', {tag: 'foo'}, {id: 'B', depth: 0})
tag(…tags) → {AST}
Assign one or more tags
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
tags |
Tags
|
<repeatable> |
one or more tags to assign |
when(…visitors)
Walk the tree and match each node against the given visitors. When a visitor matches a node its callback is called with the node. Multiple visitors may match each node.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
visitors |
VisitorLike
|
<repeatable> |
One-or-more Visitor literals, or Visitor objects |
Example
node.when(
['A', 'B', n=> n.foo()],
[{id: 'C', tag: 'x y'}, {tag: 'c'}, n=> n.bar()],
[{id: 'Y', leaf: true}, {id: 'Z', leaf: true}, n=> n.baz()],
)