-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add document tree iterator #251
Conversation
@@ -185,20 +185,28 @@ impl TreeBuilder for DocumentTaskQueue { | |||
new_id | |||
} | |||
|
|||
fn create_text(&mut self, content: &str, parent_id: NodeId) { | |||
fn create_text(&mut self, content: &str, parent_id: NodeId) -> NodeId { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making create_text
and create_comment
return a node ID is not mandatory but I did it for convenience (in the tests) and also it makes the tree builder interface more consistent anyways..
|
||
while let Some(current_node_id) = node_stack.pop() { | ||
for current_node_id in tree_iterator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool side effect of using the iterator, we get this nice Python-like iteration syntax
60d6aa8
to
87528b9
Compare
let mut traversed_nodes = Vec::new(); | ||
for current_node_id in tree_iterator { | ||
traversed_nodes.push(current_node_id); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also:
let traversed_nodes = tree_iterator.collect::<Vec<_>>();
This adds a
TreeIterator
which takes a&DocumentHandle
and constructs an iterator which walks through the tree one node at a time in tree order.EDIT: below statement is incorrect, mutations are reflected in the iterator if it's still kept "open." I added a warning in the struct description about this.
This iterator is a snapshot of the document at the time the iterator is created and is not updated from mutations. In that case, a new iterator must be created.At the moment, the iterator returns
NodeId
s (thanks for the help from @emwalker) which is good enough for now but ideally I would like this to return some type of&Node
orRc<Node>
that the user agent can use directly without having to touch the document's "internals"DocumentHandle.query()
has been updated to use this new iterator as well which still passes all tests