-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from danielrentz/task/297-document-createNode…
…Iterator #297@minor: Add method Document.createNodeIterator.
- Loading branch information
Showing
10 changed files
with
294 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import INode from '../nodes/node/INode'; | ||
|
||
export default interface INodeFilter { | ||
acceptNode(node: INode): number; | ||
} | ||
type INodeFilter = ((node: INode) => number) | { acceptNode(node: INode): number }; | ||
|
||
export default INodeFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import INodeFilter from './INodeFilter'; | ||
import TreeWalker from './TreeWalker'; | ||
import INode from '../nodes/node/INode'; | ||
|
||
/** | ||
* The NodeIterator object represents the nodes of a document subtree and a position within them. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator | ||
*/ | ||
export default class NodeIterator { | ||
public root: INode = null; | ||
public whatToShow = -1; | ||
public filter: INodeFilter = null; | ||
|
||
private readonly _walker: TreeWalker; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param root Root. | ||
* @param [whatToShow] What to show. | ||
* @param [filter] Filter. | ||
*/ | ||
constructor(root: INode, whatToShow = -1, filter: INodeFilter = null) { | ||
this.root = root; | ||
this.whatToShow = whatToShow; | ||
this.filter = filter; | ||
this._walker = new TreeWalker(root, whatToShow, filter); | ||
} | ||
|
||
/** | ||
* Moves the current Node to the next visible node in the document order. | ||
* | ||
* @returns Current node. | ||
*/ | ||
public nextNode(): INode { | ||
return this._walker.nextNode(); | ||
} | ||
|
||
/** | ||
* Moves the current Node to the previous visible node in the document order, and returns the found node. It also moves the current node to this one. If no such node exists, or if it is before that the root node defined at the object construction, returns null and the current node is not changed. | ||
* | ||
* @returns Current node. | ||
*/ | ||
public previousNode(): INode { | ||
return this._walker.previousNode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.