Skip to main content

Arena

Struct Arena 

Source
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

Source

pub fn new() -> Self

Create a new empty arena.

Source

pub fn with_capacity(node_cap: usize, text_cap: usize, attr_cap: usize) -> Self

Create a new arena with pre-allocated capacity.

Source

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.

Source

pub fn tag_index(&self) -> Option<&[Vec<NodeId>; 256]>

Get the pre-built tag index, if it was enabled during construction.

Source

pub fn new_element(&mut self, tag: Tag, depth: u16) -> NodeId

Allocate a new element node and return its id.

Source

pub fn set_unknown_tag_name(&mut self, node: NodeId, tag_name: &str)

Store the original tag name for an unknown/custom element.

Source

pub fn new_text(&mut self, depth: u16, text: &str) -> NodeId

Allocate a new text node, storing content in the text slab.

Source

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.

Source

pub fn set_source(&mut self, input: &str)

Store an owned copy of the input source for source-backed text nodes.

Source

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.

Source

pub fn new_comment(&mut self, depth: u16, text: &str) -> NodeId

Allocate a new comment node, storing content in the text slab.

Source

pub fn new_doctype(&mut self, depth: u16, text: &str) -> NodeId

Allocate a new doctype node, storing content in the text slab.

Source

pub fn set_attrs(&mut self, node: NodeId, attrs: &[Attribute<'_>])

Set attributes for a node from tokenizer attributes.

Source

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.

Source

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.

Source

pub fn set_self_closing(&mut self, node: NodeId)

Set the self-closing flag on a node.

Source

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.

child must be a freshly allocated, not-yet-linked node, distinct from parent (and therefore from parent’s current last child). Internal callers always satisfy this; the debug assertions below catch misuse.

Source

pub fn attr_name(&self, attr: &Attribute) -> &str

Get the name of an attribute.

Source

pub fn attr_value(&self, attr: &Attribute) -> Option<&str>

Get the value of an attribute, or None for boolean attributes.

Source

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.

Source

pub fn text(&self, node: NodeId) -> &str

Get the text content for a node (direct text, not recursive).

Source

pub fn unknown_tag_name(&self, node: NodeId) -> Option<&str>

Get the preserved name for an unknown/custom element.

Source

pub fn get(&self, id: NodeId) -> &Node

Get a reference to a node by id.

Source

pub fn get_mut(&mut self, id: NodeId) -> &mut Node

Get a mutable reference to a node by id.

Source

pub fn len(&self) -> usize

Total number of nodes in the arena.

Source

pub fn is_empty(&self) -> bool

Returns true if the arena contains no nodes.

Trait Implementations§

Source§

impl Default for Arena

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Arena

§

impl RefUnwindSafe for Arena

§

impl Send for Arena

§

impl Sync for Arena

§

impl Unpin for Arena

§

impl UnsafeUnpin for Arena

§

impl UnwindSafe for Arena

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.