/** * Options to configure TreeWalker */ interface TreeWalkerOptions { /** * The node where to start walking the tree. */ root?: NodeType; /** * The visitor doing some work on a visited node. */ visitor?: Partial>; /** * Indicates if the root node shall be visited or not. */ visitRoot?: boolean; } /** * Codes indicating how to proceed walking. */ declare enum VisitCodes { /** * Continue visiting. */ CONTINUE = "CONTINUE", /** * Terminate. */ TERMINATE = "TERMINATE", /** * Continue without visiting subtree. */ SKIP_SUBTREE = "SKIP_SUBTREE", /** * Continue without visiting the siblings of this node. * If returned from the preVisit then the children are also * skipped and postVisit is skipped. */ SKIP_SIBLINGS = "SKIP_SIBLINGS" } /** * A visitor which handles a node visited by a TreeWalker. */ interface Visitor { /** * Node handle to do something before visiting the node's children. * * @param node The current node. * @param context The walker's context at current node. * @returns Indicates how the walker shall proceed, the default is VisitCodes.CONTINUE. */ preVisit(node: NodeType, context: Context): VisitCodes | void; /** * Node handle to do something after visiting the node's children. * * @param node The current node. * @param context The walker's context at current node. * @returns Indicates how the walker shall proceed, the default is VisitCodes.CONTINUE. */ postVisit(node: NodeType, context: Context): VisitCodes | void; /** * Retrieves all children at a given node. * HINT: This not necessarily have to be the children of the current node. * An implementation may give the visitor a chance to return an arbitrary list of child nodes. * By default, the node's children are considered to walk deeper the tree. * * @param node The current node. * @param context The walker's context at current node. * @returns Indicates how the walker shall proceed, the default is VisitCodes.CONTINUE. */ getChildren(node: NodeType, context: Context): NodeType[] | Iterable | void | undefined; } /** Base type of tree nodes. */ interface Node { /** Optional array of child nodes. */ children?: Node[]; /** Optional accessor function for child nodes. */ getChildren?(): Node[] | Iterable | void | undefined; } /** Current visitor context. */ interface Context { /** Path of the current node (root is first item, and so on). */ axis: NodeType[]; /** The current node's parent, if any. */ parent?: NodeType; } /** * A TreeWalker walks over a tree by visiting all available nodes. */ interface TreeWalkerInterface { /** * Starts walking the tree by visiting all nodes. * HINT: If no root node were present when constructing the TreeWalker, * the root node must be present in the walk options. * * @param options The options for walking across the tree. */ walk(options?: TreeWalkerOptions): void; } /** * Constructs a TreeWalker. * * @typeParam NodeType The type of tree nodes. Defaults to {@link Node}. * @param options The options for walking across the tree. * The root node may be set globally for this TreeWalker instance. * If the root node is not set, it has to be set in the options when start walking the tree. * By default `visitRoot` is true. * @returns The tree walker ready for walking the tree. */ declare function TreeWalker(options?: TreeWalkerOptions): TreeWalkerInterface; export { TreeWalker, VisitCodes, TreeWalker as default }; export type { Context, Node, TreeWalkerInterface, TreeWalkerOptions, Visitor };