@@ -5,57 +5,59 @@ var TypeIndex = require('./type-index');
55var walkers = exports ;
66
77
8- walkers . topScan = function ( node , nodeIndex , parent , iterator , opts ) {
8+ walkers . topScan = function ( node , nodeIndex , parent , opts ) {
99 if ( parent ) {
1010 // We would like to avoid spinning an extra loop through the starting
1111 // node's siblings just to count its typeIndex.
1212 throw Error ( 'topScan is supposed to be called from the root node' ) ;
1313 }
1414
15- iterator ( node , nodeIndex , parent ) ;
15+ if ( ! opts . typeIndex ) {
16+ opts . iterator ( node , nodeIndex , parent ) ;
17+ }
1618 walkers . descendant . apply ( this , arguments ) ;
1719} ;
1820
1921
20- walkers . descendant = function ( node , nodeIndex , parent , iterator , opts ) {
22+ walkers . descendant = function ( node , nodeIndex , parent , opts ) {
2123 if ( ! node . children || ! node . children . length ) {
2224 return ;
2325 }
2426
25- if ( ( opts = opts || { } ) . typeIndex ) {
27+ if ( opts . typeIndex ) {
2628 var typeIndex = TypeIndex ( ) ;
2729 }
2830
2931 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 ) ;
32+ opts . iterator ( child , childIndex , node ,
33+ opts . typeIndex ? { typeIndex : typeIndex ( child ) } : undefined ) ;
34+ walkers . descendant ( child , childIndex , node , opts ) ;
3335 } ) ;
3436} ;
3537
3638
37- walkers . child = function ( node , nodeIndex , parent , iterator , opts ) {
39+ walkers . child = function ( node , nodeIndex , parent , opts ) {
3840 if ( ! node . children || ! node . children . length ) {
3941 return ;
4042 }
4143
42- if ( ( opts = opts || { } ) . typeIndex ) {
44+ if ( opts . typeIndex ) {
4345 var typeIndex = TypeIndex ( ) ;
4446 }
4547
4648 node . children . forEach ( function ( child , childIndex ) {
47- iterator ( child , childIndex , node ,
48- opts . typeIndex ? { typeIndex : typeIndex ( child ) } : undefined ) ;
49+ opts . iterator ( child , childIndex , node ,
50+ opts . typeIndex ? { typeIndex : typeIndex ( child ) } : undefined ) ;
4951 } ) ;
5052} ;
5153
5254
53- walkers . adjacentSibling = function ( node , nodeIndex , parent , iterator , opts ) {
55+ walkers . adjacentSibling = function ( node , nodeIndex , parent , opts ) {
5456 if ( ! parent ) {
5557 return ;
5658 }
5759
58- if ( ( opts = opts || { } ) . typeIndex ) {
60+ if ( opts . typeIndex ) {
5961 var typeIndex = TypeIndex ( ) ;
6062
6163 // Prefill type indexes with preceding nodes.
@@ -66,18 +68,18 @@ walkers.adjacentSibling = function (node, nodeIndex, parent, iterator, opts) {
6668
6769 if ( ++ nodeIndex < parent . children . length ) {
6870 node = parent . children [ nodeIndex ] ;
69- iterator ( node , nodeIndex , parent ,
70- opts . typeIndex ? { typeIndex : typeIndex ( node ) } : undefined ) ;
71+ opts . iterator ( node , nodeIndex , parent ,
72+ opts . typeIndex ? { typeIndex : typeIndex ( node ) } : undefined ) ;
7173 }
7274} ;
7375
7476
75- walkers . generalSibling = function ( node , nodeIndex , parent , iterator , opts ) {
77+ walkers . generalSibling = function ( node , nodeIndex , parent , opts ) {
7678 if ( ! parent ) {
7779 return ;
7880 }
7981
80- if ( ( opts = opts || { } ) . typeIndex ) {
82+ if ( opts . typeIndex ) {
8183 var typeIndex = TypeIndex ( ) ;
8284
8385 // Prefill type indexes with preceding nodes.
@@ -88,7 +90,7 @@ walkers.generalSibling = function (node, nodeIndex, parent, iterator, opts) {
8890
8991 while ( ++ nodeIndex < parent . children . length ) {
9092 node = parent . children [ nodeIndex ] ;
91- iterator ( node , nodeIndex , parent ,
92- opts . typeIndex ? { typeIndex : typeIndex ( node ) } : undefined ) ;
93+ opts . iterator ( node , nodeIndex , parent ,
94+ opts . typeIndex ? { typeIndex : typeIndex ( node ) } : undefined ) ;
9395 }
9496} ;
0 commit comments