|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +var TypeIndex = require('./type-index'); |
| 4 | + |
3 | 5 | var walkers = exports; |
4 | 6 |
|
5 | 7 |
|
6 | | -walkers.topScan = function (node, nodeIndex, parent, iterator) { |
| 8 | +walkers.topScan = function (node, nodeIndex, parent, iterator, opts) { |
| 9 | + if (parent) { |
| 10 | + // We would like to avoid spinning an extra loop through the starting |
| 11 | + // node's siblings just to count its typeIndex. |
| 12 | + throw Error('topScan is supposed to be called from the root node'); |
| 13 | + } |
| 14 | + |
7 | 15 | iterator(node, nodeIndex, parent); |
8 | 16 | walkers.descendant.apply(this, arguments); |
9 | 17 | }; |
10 | 18 |
|
11 | 19 |
|
12 | | -walkers.descendant = function (node, nodeIndex, parent, iterator) { |
13 | | - if (node.children) { |
14 | | - node.children.forEach(function (child, childIndex) { |
15 | | - iterator(child, childIndex, node); |
16 | | - walkers.descendant(child, childIndex, node, iterator); |
17 | | - }); |
| 20 | +walkers.descendant = function (node, nodeIndex, parent, iterator, opts) { |
| 21 | + if (!node.children || !node.children.length) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if ((opts = opts || {}).typeIndex) { |
| 26 | + var typeIndex = TypeIndex(); |
18 | 27 | } |
| 28 | + |
| 29 | + node.children.forEach(function (child, childIndex) { |
| 30 | + iterator(child, childIndex, node, |
| 31 | + opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined); |
| 32 | + walkers.descendant(child, childIndex, node, iterator, opts); |
| 33 | + }); |
19 | 34 | }; |
20 | 35 |
|
21 | 36 |
|
22 | | -walkers.child = function (node, nodeIndex, parent, iterator) { |
23 | | - if (node.children) { |
24 | | - node.children.forEach(function (child, childIndex) { |
25 | | - iterator(child, childIndex, node); |
26 | | - }); |
| 37 | +walkers.child = function (node, nodeIndex, parent, iterator, opts) { |
| 38 | + if (!node.children || !node.children.length) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if ((opts = opts || {}).typeIndex) { |
| 43 | + var typeIndex = TypeIndex(); |
27 | 44 | } |
| 45 | + |
| 46 | + node.children.forEach(function (child, childIndex) { |
| 47 | + iterator(child, childIndex, node, |
| 48 | + opts.typeIndex ? { typeIndex: typeIndex(child) } : undefined); |
| 49 | + }); |
28 | 50 | }; |
29 | 51 |
|
30 | 52 |
|
31 | | -walkers.adjacentSibling = function (node, nodeIndex, parent, iterator) { |
32 | | - if (parent && ++nodeIndex < parent.children.length) { |
33 | | - iterator(parent.children[nodeIndex], nodeIndex, parent); |
| 53 | +walkers.adjacentSibling = function (node, nodeIndex, parent, iterator, opts) { |
| 54 | + if (!parent) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if ((opts = opts || {}).typeIndex) { |
| 59 | + var typeIndex = TypeIndex(); |
| 60 | + |
| 61 | + // Prefill type indexes with preceding nodes. |
| 62 | + for (var prevIndex = 0; prevIndex <= nodeIndex; ++prevIndex) { |
| 63 | + typeIndex(parent.children[prevIndex]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (++nodeIndex < parent.children.length) { |
| 68 | + node = parent.children[nodeIndex]; |
| 69 | + iterator(node, nodeIndex, parent, |
| 70 | + opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined); |
34 | 71 | } |
35 | 72 | }; |
36 | 73 |
|
37 | 74 |
|
38 | | -walkers.generalSibling = function (node, nodeIndex, parent, iterator) { |
39 | | - if (parent) { |
40 | | - while (++nodeIndex < parent.children.length) { |
41 | | - iterator(parent.children[nodeIndex], nodeIndex, parent); |
| 75 | +walkers.generalSibling = function (node, nodeIndex, parent, iterator, opts) { |
| 76 | + if (!parent) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if ((opts = opts || {}).typeIndex) { |
| 81 | + var typeIndex = TypeIndex(); |
| 82 | + |
| 83 | + // Prefill type indexes with preceding nodes. |
| 84 | + for (var prevIndex = 0; prevIndex <= nodeIndex; ++prevIndex) { |
| 85 | + typeIndex(parent.children[prevIndex]); |
42 | 86 | } |
43 | 87 | } |
| 88 | + |
| 89 | + while (++nodeIndex < parent.children.length) { |
| 90 | + node = parent.children[nodeIndex]; |
| 91 | + iterator(node, nodeIndex, parent, |
| 92 | + opts.typeIndex ? { typeIndex: typeIndex(node) } : undefined); |
| 93 | + } |
44 | 94 | }; |
0 commit comments