Best approach to get leaves present down a particular node?

Hello, what would be a good way to get “text” and “foobar” given that we’re at ClauseA?

I’m not sure how to limit the traversal only for the children of ClausA, with a combination for nextSibling and next

                 ├─ ClauseA [872..898]
                 │   ├─ Action [872..876]: "text"
                 │   └─ ClauseB [877..898]
                 │       └─ RuleB [877..898]
                 │           └─ SubRuleB [877..898]: "foobar"

next will automatically move to parent nodes, so it isn’t much use here. Something like this should work:

function iterInside(cursor, f) {
  let depth = 0
  for (;;) {
    if (cursor.firstChild()) {
      depth++
    } else {
      for (;;) {
        if (!depth) return
        if (cursor.nextSibling()) break
        cursor.parent()
        depth--
      }
    }
    f(cursor)
  }
}
1 Like

Many thanks, that did the trick.