VDom

Struct VDom 

Source
pub struct VDom<'a> { /* private fields */ }
Expand description

VDom represents a Document Object Model

It is the result of parsing an HTML document. Internally it is only a wrapper around the Parser struct, in which all of the HTML tags are stored. Many functions of the public API take a reference to a Parser as a parameter to resolve NodeHandles to Nodes.

Implementations§

Source§

impl<'a> VDom<'a>

Source

pub fn parser(&self) -> &Parser<'a>

Returns a reference to the underlying parser

Source

pub fn parser_mut(&mut self) -> &mut Parser<'a>

Returns a mutable reference to the underlying parser

Source

pub fn get_element_by_id<'b, S>(&'b self, id: S) -> Option<NodeHandle>
where S: Into<Bytes<'a>>,

Finds an element by its id attribute.

Source

pub fn get_elements_by_class_name<'b>( &'b self, id: &'b str, ) -> Box<dyn Iterator<Item = NodeHandle> + 'b>

Returns a list of elements that match a given class name.

Source

pub fn nodes(&self) -> &[Node<'a>]

Returns a slice of all the elements in the HTML document

The difference between children() and nodes() is that children only returns the immediate children of the root node, while nodes() returns all nodes, including nested tags.

§Order

The order of the returned nodes is the same as the order of the nodes in the HTML document.

Source

pub fn nodes_mut(&mut self) -> &mut [Node<'a>]

Returns a mutable slice of all the elements in the HTML document

The difference between children() and nodes() is that children only returns the immediate children of the root node, while nodes() returns all nodes, including nested tags.

Source

pub fn children(&self) -> &[NodeHandle]

Returns the topmost subnodes (“children”) of this DOM

Source

pub fn children_mut(&mut self) -> &mut [NodeHandle]

Returns a mutable reference to the topmost subnodes (“children”) of this DOM

Source

pub fn version(&self) -> Option<HTMLVersion>

Returns the HTML version. This is determined by the <!DOCTYPE> tag

Source

pub fn outer_html(&self) -> String

Returns the contained markup of all of the elements in this DOM.

Equivalent to Element#outerHTML in browsers)

§Example
let html = r#"<div><p href="/about" id="find-me">Hello world</p></div>"#;
let mut dom = tl::parse(html, Default::default()).unwrap();

let element = dom.get_element_by_id("find-me")
    .unwrap()
    .get_mut(dom.parser_mut())
    .unwrap()
    .as_tag_mut()
    .unwrap();

element.attributes_mut().get_mut("href").flatten().unwrap().set("/");

assert_eq!(dom.outer_html(), r#"<div><p href="/" id="find-me">Hello world</p></div>"#);
Source

pub fn query_selector<'b>( &'b self, selector: &'b str, ) -> Option<QuerySelectorIterator<'a, 'b, Self>>

Tries to parse the query selector and returns an iterator over elements that match the given query selector.

§Example
let dom = tl::parse("<div><p class=\"foo\">bar</div>", tl::ParserOptions::default()).unwrap();
let handle = dom.query_selector("p.foo").and_then(|mut iter| iter.next()).unwrap();
let node = handle.get(dom.parser()).unwrap();
assert_eq!(node.inner_text(dom.parser()), "bar");

Trait Implementations§

Source§

impl<'a> Debug for VDom<'a>

Source§

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

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

impl<'a> From<Parser<'a>> for VDom<'a>

Source§

fn from(parser: Parser<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> QueryIterable<'a> for VDom<'a>

Source§

fn get<'b>( &'b self, parser: &'b Parser<'a>, index: usize, ) -> Option<(&'b Node<'a>, NodeHandle)>

Gets a node at a specific index
Source§

fn len(&self, _parser: &Parser<'_>) -> usize

Gets or computes the length (number of nodes)
Source§

fn start(&self) -> Option<InnerNodeHandle>

Gets the starting index

Auto Trait Implementations§

§

impl<'a> Freeze for VDom<'a>

§

impl<'a> RefUnwindSafe for VDom<'a>

§

impl<'a> !Send for VDom<'a>

§

impl<'a> !Sync for VDom<'a>

§

impl<'a> Unpin for VDom<'a>

§

impl<'a> UnwindSafe for VDom<'a>

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.