Skip to main content

Dom

Struct Dom 

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

A parsed HTML document: an arena of nodes, a single string buffer they reference, the top-level (root) nodes, and any recovery diagnostics.

Build one with crate::parse. It is immutable once built; traverse it with NodeRef handles from Dom::root, Dom::roots, Dom::get, or the find_by_* helpers.

Implementations§

Source§

impl Dom

Source

pub fn len(&self) -> usize

Number of nodes in the arena.

Source

pub fn is_empty(&self) -> bool

Whether the document contains no nodes.

Source

pub fn errors(&self) -> &[ParseError]

Recovery diagnostics collected during parsing (empty for clean input).

Source

pub fn get(&self, id: NodeId) -> Option<NodeRef<'_>>

A handle to a node by id, or None if the id is out of range.

Source

pub fn root(&self) -> Option<NodeRef<'_>>

The first top-level node (the document element for a typical page).

Source

pub fn roots(&self) -> impl Iterator<Item = NodeRef<'_>>

All top-level nodes, in document order.

Source

pub fn nodes(&self) -> impl Iterator<Item = NodeRef<'_>>

Every node in the arena, in document (pre-order) order.

Source

pub fn find_by_tag<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a

All elements whose tag name equals name (ASCII case-insensitive).

Source

pub fn find_by_id<'a>(&'a self, id: &'a str) -> Option<NodeRef<'a>>

The first element with id="…" equal to id, if any.

Source

pub fn find_by_class<'a>( &'a self, class: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a

All elements whose class attribute contains class.

Source§

impl Dom

Source

pub fn to_html(&self) -> String

Serialize the whole document back to an HTML string.

let dom = domtree::parse("<p class='x'>1 &lt; 2 &amp; 3</p>");
assert_eq!(dom.to_html(), r#"<p class="x">1 &lt; 2 &amp; 3</p>"#);
Source§

impl Dom

Source

pub fn to_json(&self) -> String

Available on crate feature json only.

Serialize the document to a compact JSON string.

The shape is self-describing: every node is an object with a "type" of element / text / comment / doctype, elements carry tag, attrs (an object) and children (an array), and the whole document is wrapped in { "type": "document", "errors": N, "children": [...] }.

let dom = domtree::parse("<a href=\"/x\">hi</a>");
let json = dom.to_json();
assert!(json.contains("\"tag\":\"a\""));
assert!(json.contains("\"href\":\"/x\""));
assert!(json.contains("\"value\":\"hi\""));
Source§

impl Dom

Source

pub fn select<'a>( &'a self, selector: &str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a

Available on crate feature query only.

Find every element matching a CSS selector, in document order.

let dom = domtree::parse(
    "<ul><li class='x'>a</li><li>b</li></ul><ol><li>c</li></ol>",
);
// descendant + class
assert_eq!(dom.select("ul li.x").count(), 1);
// child combinator
assert_eq!(dom.select("ol > li").count(), 1);
// selector list
assert_eq!(dom.select("ul, ol").count(), 2);

Trait Implementations§

Source§

impl Clone for Dom

Source§

fn clone(&self) -> Dom

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dom

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Dom

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Dom

§

impl RefUnwindSafe for Dom

§

impl Send for Dom

§

impl Sync for Dom

§

impl Unpin for Dom

§

impl UnsafeUnpin for Dom

§

impl UnwindSafe for Dom

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.