Skip to main content

TreeBuilder

Struct TreeBuilder 

Source
pub struct TreeBuilder { /* private fields */ }
Expand description

Builds a DOM tree from a token stream.

Maintains an open-elements stack and processes each token to either push new nodes, pop closed elements, or append text/comment/doctype nodes.

Implementations§

Source§

impl TreeBuilder

Source

pub fn new() -> Self

Create a new tree builder with default capacity.

Source

pub fn with_capacity_hint(input_len: usize) -> Self

Create a tree builder with capacity tuned to the expected input size.

Uses heuristics to estimate node, text, and attribute counts from the input byte length, reducing reallocations for large documents.

Source

pub fn enable_tag_index(&mut self)

Enable the inline tag index for O(1) tag lookups after parsing.

When enabled, each element’s tag is indexed during tree construction, eliminating the need for a separate DFS pass in DocumentIndex::build.

Source

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

Enable source-backed text nodes.

Stores an owned copy of input in the arena. Text nodes whose content is borrowed from input (entity-free) will reference the source instead of copying to the text slab.

Source

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

Set source pointer tracking without copying data to the arena.

Use this with Arena::set_source_owned after tokenization to avoid a redundant memcpy when the caller owns the input String.

Source

pub fn process(&mut self, token: &Token<'_>) -> Option<NodeId>

Process a single token and insert it into the tree.

Returns the NodeId of the newly created node, or None if the token did not produce a node (e.g. a close tag or depth limit hit).

Source

pub fn finish(self) -> (Arena, NodeId)

Finish building and return the root node id and the arena.

Trait Implementations§

Source§

impl Default for TreeBuilder

Source§

fn default() -> Self

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

impl TreeSink for TreeBuilder

Source§

fn open_tag(&mut self, tag: Tag, name: &str, attr_raw: &str, self_closing: bool)

An opening tag was encountered. Read more
Source§

fn close_tag(&mut self, tag: Tag, name: &str)

A closing tag was encountered.
Source§

fn text(&mut self, raw: &str)

Raw text content (not entity-decoded). Read more
Source§

fn comment(&mut self, content: &str)

A comment was encountered.
Source§

fn doctype(&mut self, content: &str)

A DOCTYPE declaration was encountered.
Source§

fn cdata(&mut self, content: &str)

A CDATA section was encountered.

Auto Trait Implementations§

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.