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 NodeHandle
s to Node
s.
Implementations
sourceimpl<'a> VDom<'a>
impl<'a> VDom<'a>
sourcepub fn parser_mut(&mut self) -> &mut Parser<'a>
pub fn parser_mut(&mut self) -> &mut Parser<'a>
Returns a mutable reference to the underlying parser
sourcepub fn get_element_by_id<'b, S>(&'b self, id: S) -> Option<NodeHandle> where
S: Into<Bytes<'a>>,
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.
sourcepub fn get_elements_by_class_name<'b>(
&'b self,
id: &'b str
) -> Box<dyn Iterator<Item = NodeHandle> + 'b>
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.
sourcepub fn nodes(&self) -> &[Node<'a>]
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.
sourcepub fn nodes_mut(&mut self) -> &mut [Node<'a>]
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.
sourcepub fn children(&self) -> &[NodeHandle]
pub fn children(&self) -> &[NodeHandle]
Returns the topmost subnodes (“children”) of this DOM
sourcepub fn children_mut(&mut self) -> &mut [NodeHandle]
pub fn children_mut(&mut self) -> &mut [NodeHandle]
Returns a mutable reference to the topmost subnodes (“children”) of this DOM
sourcepub fn version(&self) -> Option<HTMLVersion>
pub fn version(&self) -> Option<HTMLVersion>
Returns the HTML version.
This is determined by the <!DOCTYPE>
tag
sourcepub fn outer_html(&self) -> String
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>"#);
sourcepub fn query_selector<'b>(
&'b self,
selector: &'b str
) -> Option<QuerySelectorIterator<'a, 'b, Self>>
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
sourceimpl<'a> QueryIterable<'a> for VDom<'a>
impl<'a> QueryIterable<'a> for VDom<'a>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more