Replies: 4 comments 1 reply
-
I've started out with using |
Beta Was this translation helpful? Give feedback.
-
Also new to Rust. I've seen two patterns mentioned for Rust when you have a large graph with shared references to nodes. Using ids to look up nodes in a map- or array-like structure is the other pattern. People seem to recommend both patterns. Looking at Node, I think if we stick with IDs, we'll want to use a newtype wrapper around the primitive ( struct NodeId(usize); |
Beta Was this translation helpful? Give feedback.
-
Let's stick with nodeId's for now then. A dom- or nodetree should always carry an arena with it, so we can always fetch the actual node data when needed. If we happen to need some cloning, at least we clone large trees with node-ids instead of complete nodes with all info. |
Beta Was this translation helpful? Give feedback.
-
Closing discussion as we're settling on ID approach for now |
Beta Was this translation helpful? Give feedback.
-
I first started experimenting with reworking
Node
to be more inline with the DOM spec (4.5) (not a complete 1:1 implementation of the spec):I've replaced the original IDs with reference counters to remove the dependency on
NodeArena
and we can directly access children/siblings from a givenNode
. I used reference counters since we could have multiple things pointing to a node (siblings, children, parents, etc.) Although keep in mind I'm extremely new to rust so if there's a better alternative to this, we can replace that.Then I restructured
Document
to be the following:When a
Document
is first created, aNode::new_document()
node is created and assigned as the root andcurrent_node
points to it.In theory, as we are parsing, we would have access to to
document.current_node
and candocument.current_node.append_child
whenever we are creating new nodes (or look at siblings, children, etc. as we are parsing.) TheNode
itself will have utility methods to check for children, check if the node allows certain children/text nodes, etc.Of course, we'll have to update the
current_node
as we are parsing when we append children. I did something similar in one of my projects, where whenever we start a new<tag>
we update thecurrent_node
to this current tag, and when the tag closes</tag>
we update thecurrent_node
to point back to its parentAfter parsing is finished, the same document is returned which would initially be pointing to the root
document
node, then can be traversed with something like (note: I'm writing this directly in github, there are definitely syntax errors, but you should get the idea)Obviously, implementing all these changes would require a significant rework on the parser, which I took a look at but I'm too new to rust to understand the macros used currently.
We can discuss this more if we all think this type of structure is worth it in the long run, and if so, we can create an experimental branch and people can help me rework the parser to accomadate the new node/document structure
Beta Was this translation helpful? Give feedback.
All reactions