pub struct Arena { /* private fields */ }Expand description
Arena-based storage for all DOM nodes, text content, and attributes.
Nodes are stored in a contiguous Vec<Node> for cache-line-friendly access.
Text and attributes are stored in separate slabs and referenced by
offset+length from each node.
Implementations§
Source§impl Arena
impl Arena
Sourcepub fn with_capacity(node_cap: usize, text_cap: usize, attr_cap: usize) -> Self
pub fn with_capacity(node_cap: usize, text_cap: usize, attr_cap: usize) -> Self
Create a new arena with pre-allocated capacity.
Sourcepub fn enable_tag_index(&mut self)
pub fn enable_tag_index(&mut self)
Enable the inline tag index.
Once enabled, every Arena::new_element call appends the node id to
the corresponding tag bucket. Consumers can retrieve the index via
Arena::tag_index.
Sourcepub fn tag_index(&self) -> Option<&[Vec<NodeId>; 256]>
pub fn tag_index(&self) -> Option<&[Vec<NodeId>; 256]>
Get the pre-built tag index, if it was enabled during construction.
Sourcepub fn new_element(&mut self, tag: Tag, depth: u16) -> NodeId
pub fn new_element(&mut self, tag: Tag, depth: u16) -> NodeId
Allocate a new element node and return its id.
Sourcepub fn set_unknown_tag_name(&mut self, node: NodeId, tag_name: &str)
pub fn set_unknown_tag_name(&mut self, node: NodeId, tag_name: &str)
Store the original tag name for an unknown/custom element.
Sourcepub fn new_text(&mut self, depth: u16, text: &str) -> NodeId
pub fn new_text(&mut self, depth: u16, text: &str) -> NodeId
Allocate a new text node, storing content in the text slab.
Sourcepub fn new_text_ref(
&mut self,
depth: u16,
source_offset: u32,
len: u32,
) -> NodeId
pub fn new_text_ref( &mut self, depth: u16, source_offset: u32, len: u32, ) -> NodeId
Allocate a text node that references a region of the original source.
Instead of copying content to the text slab, this stores a
(source_offset, len) pair and sets NodeFlags::IS_TEXT_FROM_SOURCE.
The source must have been set via Arena::set_source before calling
this method.
Sourcepub fn set_source(&mut self, input: &str)
pub fn set_source(&mut self, input: &str)
Store an owned copy of the input source for source-backed text nodes.
Sourcepub fn set_source_owned(&mut self, source: String)
pub fn set_source_owned(&mut self, source: String)
Transfer an already-owned String as the source buffer (zero copy).
When the caller owns the input String (e.g., from an HTTP response),
this avoids the memcpy that set_source performs.
Sourcepub fn new_comment(&mut self, depth: u16, text: &str) -> NodeId
pub fn new_comment(&mut self, depth: u16, text: &str) -> NodeId
Allocate a new comment node, storing content in the text slab.
Sourcepub fn new_doctype(&mut self, depth: u16, text: &str) -> NodeId
pub fn new_doctype(&mut self, depth: u16, text: &str) -> NodeId
Allocate a new doctype node, storing content in the text slab.
Sourcepub fn set_attrs(&mut self, node: NodeId, attrs: &[Attribute<'_>])
pub fn set_attrs(&mut self, node: NodeId, attrs: &[Attribute<'_>])
Set attributes for a node from tokenizer attributes.
Sourcepub fn set_attrs_raw_lazy(&mut self, node: NodeId, attr_raw: &str)
pub fn set_attrs_raw_lazy(&mut self, node: NodeId, attr_raw: &str)
Store raw attribute bytes for lazy parsing.
Instead of parsing attributes immediately, stores the raw attribute
region in attr_str_slab and records offset/len on the node. Attributes
are parsed on first access via Arena::attrs or
Arena::ensure_attrs_parsed.
Sourcepub fn set_attrs_from_raw(&mut self, node: NodeId, attr_raw: &str)
pub fn set_attrs_from_raw(&mut self, node: NodeId, attr_raw: &str)
Parse attributes directly from a raw attribute region into the slab.
Skips all intermediate Vec<Attribute> allocation — names and values
are written directly to attr_str_slab and compact Attribute structs
are pushed to attr_slab in a single pass.
Sourcepub fn set_element_index(&mut self, node: NodeId, index: u16)
pub fn set_element_index(&mut self, node: NodeId, index: u16)
Set the 1-based element sibling index for a node.
Called by [TreeBuilder] after appending an element child.
Sourcepub fn set_self_closing(&mut self, node: NodeId)
pub fn set_self_closing(&mut self, node: NodeId)
Set the self-closing flag on a node.
Sourcepub fn append_child(&mut self, parent: NodeId, child: NodeId)
pub fn append_child(&mut self, parent: NodeId, child: NodeId)
Append child as the last child of parent.
Updates all tree links: parent, first_child, last_child, prev_sibling, next_sibling. Uses unchecked indexing since NodeIds are always valid indices created by this arena.
Sourcepub fn attr_value(&self, attr: &Attribute) -> Option<&str>
pub fn attr_value(&self, attr: &Attribute) -> Option<&str>
Get the value of an attribute, or None for boolean attributes.
Sourcepub fn attrs(&self, node: NodeId) -> &[Attribute]
pub fn attrs(&self, node: NodeId) -> &[Attribute]
Get the attributes for a node (read-only, requires prior parse).
If the node has lazy (unparsed) attributes, returns an empty slice.
Call Arena::ensure_attrs_parsed first to guarantee parsing.
Sourcepub fn has_lazy_attrs(&self, node: NodeId) -> bool
pub fn has_lazy_attrs(&self, node: NodeId) -> bool
Returns true if the node has lazy (unparsed) attributes.
Sourcepub fn ensure_attrs_parsed(&mut self, node: NodeId)
pub fn ensure_attrs_parsed(&mut self, node: NodeId)
Ensure the node’s attributes are parsed into the slab.
If the node has lazy attributes (stored as raw bytes), parses them now and updates the node’s attr_offset/attr_count. No-op if already parsed or if the node has no attributes.
Sourcepub fn attrs_mut(&mut self, node: NodeId) -> &[Attribute]
pub fn attrs_mut(&mut self, node: NodeId) -> &[Attribute]
Get the attributes for a node, parsing lazily if needed.
Convenience method that combines Arena::ensure_attrs_parsed and
Arena::attrs.
Sourcepub fn text(&self, node: NodeId) -> &str
pub fn text(&self, node: NodeId) -> &str
Get the text content for a node (direct text, not recursive).
Sourcepub fn unknown_tag_name(&self, node: NodeId) -> Option<&str>
pub fn unknown_tag_name(&self, node: NodeId) -> Option<&str>
Get the preserved name for an unknown/custom element.
Sourcepub fn ensure_all_attrs_parsed(&mut self)
pub fn ensure_all_attrs_parsed(&mut self)
Parse all lazy (unparsed) attributes in a single batch pass.
Called after tree building completes to resolve all deferred attribute parsing before handing the arena to consumers. This gives better cache locality than interleaving attr parsing with node creation.