Element

Struct Element 

Source
pub struct Element<'a> {
    pub name: &'a str,
    pub attrs: &'a [Attr<'a>],
    /* private fields */
}
Expand description

An HTML element like <p> or <div>.

§Indexing

The Element type allows access to attributes by index, because it implements the Index trait.

let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div class='foo' readonly></div>").unwrap();
let div = fragment.first_child().unwrap().as_element().unwrap();
assert_eq!(div["class"], htmlite::Attr::Class("foo"));
assert_eq!(div["readonly"], htmlite::Attr::Readonly(true));

However be careful, if you try to access index attribute that is not present you will get a panic. Use Element::get to check if an attribute is present.

Fields§

§name: &'a str

This element’s tag name

§attrs: &'a [Attr<'a>]

The element’s attributes

Implementations§

Source§

impl<'a> Element<'a>

Source

pub fn get(&self, name: &str) -> Option<&Attr<'a>>

Return a reference to the attribute name if it exists.

The attribute name comparison is case-insensitive.

Methods from Deref<Target = Node<'a>>§

Source

pub fn children(&self) -> Following<'a>

Returns an iterator of references to children nodes.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<a></a><b></b>").unwrap();

assert_eq!(
    fragment.children().map(Node::outer_html).collect::<Vec<_>>(),
    ["<a></a>","<b></b>"]
);
Source

pub fn reverse_children(&self) -> Preceding<'a>

Returns an iterator of references to children nodes in reverse order.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<a></a><b></b>").unwrap();

assert_eq!(
    fragment.reverse_children().map(Node::outer_html).collect::<Vec<_>>(),
    ["<b></b>","<a></a>"]
);
Source

pub fn ancestors(&'a self) -> Ancestors<'a>

Returns an iterator of references to ancestor nodes.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<body><section><div></div></section></body>").unwrap();
let div = fragment.select("div").next().unwrap();

let iter = div.ancestors().filter_map(Node::as_element).map(|e| e.name);
assert_eq!(
    iter.collect::<Vec<_>>(),
    ["section", "body"]
);
Source

pub fn preceding(&'a self) -> Preceding<'a>

Returns an iterator of references to the nodes before this node.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<div></div><main></main><b></b>").unwrap();
let div = fragment.select("b").next().unwrap();

let iter = div.preceding().filter_map(Node::as_element).map(|e| e.name);
assert_eq!(
    iter.collect::<Vec<_>>(),
    ["main", "div"]
);
Source

pub fn following(&'a self) -> Following<'a>

Returns an iterator of references to the nodes after this node.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<b></b><div></div><main></main>").unwrap();
let div = fragment.select("b").next().unwrap();

let iter = div.following().filter_map(Node::as_element).map(|e| e.name);
assert_eq!(
    iter.collect::<Vec<_>>(),
    ["div", "main"]
);
Source

pub fn descendants(&'a self) -> Descendants<'a>

Returns an iterator of references to descendant nodes.

Parent nodes will appear before their children.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<aside></aside><main><section></section></main>").unwrap();

let iter = fragment.descendants().filter_map(Node::as_element).map(|e| e.name);
assert_eq!(
    iter.collect::<Vec<_>>(),
    ["aside", "main", "section"]
);
Source

pub fn text_content(&'a self) -> TextContent<'a>

Returns an iterator of text within this node and its descendants.

§Examples
use htmlite::{NodeArena, Node};
let arena = NodeArena::new();
let fragment = htmlite::parse(&arena, "<div>hello</div><section><main>world</main></section>").unwrap();

assert_eq!(
    fragment.text_content().collect::<Vec<_>>(),
    ["hello",  "world"]
);
Source

pub fn select<'s>(&'a self, selector: &'s str) -> Selected<'a, 's>

Returns an iterator of references to descendant elements that match the given CSS selector.

§CSS selector syntax:

This methods supports the CSS 2.1 selector syntax. The only pseudo-element supported is :first-child as the others imply a live DOM.

Some examples are provided bellow, consult the spec for more details.

PatternMeaning
*Matches any element
divMatches any element whose tag name is div
div buttonMatches any button element that is an ancestor of a div element
div > buttonMatches any button element that is a direct child of a div element
div:first-childMatches any div element if it is the first child of its parent
div + buttonMatches any button element if it is immediately preceded a div element
div[foo]Matches any div element with the foo attribute set
div[foo="warning"]Matches any div element whose foo attribute value is exactly warning
div[foo~="box"]Matches any div element whose foo attribute value is a whitespace-seperated list of words, one of which is box
div[foo|="box"]Matches any div element whose foo attribute value is a hyphen-seperated list of words, begining with box
div#myidMatches any div element with an id attribute value equal to myid
`div.myclassMatches any div element with a class attribute value equal to myclass
Source

pub fn parent(&self) -> Option<&'a Node<'a>>

Returns a reference to the parent node if any.

let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>text</div>").unwrap();
let div = fragment.first_child().unwrap();
let text = div.first_child().unwrap();
assert_eq!(text.parent(), Some(div));

Fragment nodes have no parents.

let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "text<div></div>").unwrap();
assert_eq!(fragment.parent(), None);
Source

pub fn first_child(&self) -> Option<&'a Node<'a>>

Returns a reference to the first child of this node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "text<div></div>").unwrap();

assert_eq!(fragment.first_child().unwrap().is_text(), true);
Source

pub fn last_child(&self) -> Option<&'a Node<'a>>

Returns a reference to the last child of this node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div></div>text").unwrap();

assert!(fragment.last_child().unwrap().is_text());
Source

pub fn previous(&self) -> Option<&'a Node<'a>>

Returns a reference to this node’s previous sibling node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!--comment-->text").unwrap();
let text = fragment.last_child().unwrap();

assert!(text.previous().unwrap().is_comment());
Source

pub fn next(&self) -> Option<&'a Node<'a>>

Returns a reference to this node’s next sibling node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "text<!--comment-->").unwrap();
let text = fragment.first_child().unwrap();

assert!(text.next().unwrap().is_comment());
Source

pub fn is_element(&self) -> bool

Returns true if this is an element node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div></div>").unwrap();
assert!(fragment.first_child().unwrap().is_element());
Source

pub fn is_text(&self) -> bool

Returns true if this is a text node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "text").unwrap();
assert!(fragment.first_child().unwrap().is_text());
Source

pub fn is_doctype(&self) -> bool

Returns true if this is a doctype node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!doctype foo>").unwrap();
assert!(fragment.first_child().unwrap().is_doctype());
Source

pub fn is_comment(&self) -> bool

Returns true if this is a comment node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!--comment-->").unwrap();
assert!(fragment.first_child().unwrap().is_comment());
Source

pub fn is_fragment(&self) -> bool

Returns true if this is a fragment node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!--comment-->").unwrap();
assert!(fragment.is_fragment());
Source

pub fn as_element(&self) -> Option<&Element<'a>>

Returns a reference to the Element if this is an element node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div></div>").unwrap();
assert_eq!(
    fragment.first_child().and_then(htmlite::Node::as_element).map(|e| e.name),
    Some("div")
);
Source

pub fn as_text(&'a self) -> Option<&'a Text<'a>>

Returns a reference to the text data if this is text node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "text").unwrap();
assert_eq!(
    fragment.first_child().and_then(htmlite::Node::as_text).map(|t| t.text),
    Some("text")
);
Source

pub fn as_comment(&'a self) -> Option<&'a Comment<'a>>

Returns a reference to the comment data if this is a comment node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!--comment-->").unwrap();
assert_eq!(
    fragment.first_child().and_then(htmlite::Node::as_comment).map(|c| c.comment),
    Some("comment")
);
Source

pub fn as_doctype(&'a self) -> Option<&'a Doctype<'a>>

Returns a reference to the doctype name data if this is doctype node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<!doctype blah>").unwrap();
assert_eq!(
    fragment.first_child().and_then(htmlite::Node::as_doctype).map(|d| d.name),
    Some("blah")
);
Source

pub fn outer_html(&self) -> String

Serializes this node and its descendants as an HTML string

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>text</div>").unwrap();
let div = fragment.select("div").next().unwrap();

assert_eq!(div.outer_html(), "<div>text</div>");
Source

pub fn inner_html(&self) -> String

Serializes this node’s descendants as an HTML string

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>text</div>").unwrap();
let div = fragment.select("div").next().unwrap();

assert_eq!(div.inner_html(), "text");
Source

pub fn detach(&'a self) -> &'a Node<'a>

Orphans this node by detaching it from its parent and siblings.

Children are not affected.

let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>foo</div>").unwrap();
let child = fragment.first_child().unwrap();
assert_eq!(fragment.outer_html(), "<div>foo</div>");

child.detach();
assert_eq!(fragment.outer_html(), "");
assert_eq!(child.outer_html(), "<div>foo</div>");
Source

pub fn append<I>(&'a self, children: I) -> &'a Node<'a>
where I: IntoIterator<Item = &'a Node<'a>>,

Inserts children after this node’s last child.

Appending a fragment appends all the fragment node’s children. The fragment will be empty after this method because its children will be detached before being appended to self.

§Panics

This method panics if self is a Text, comment or Doctype node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>text</div>").unwrap();
let child = fragment.first_child().unwrap();

child.append(h.span([], []));
assert_eq!(fragment.outer_html(), "<div>text<span></span></div>");
Source

pub fn prepend<I>(&'a self, children: I) -> &'a Node<'a>
where I: IntoIterator<Item = &'a Node<'a>>, <I as IntoIterator>::IntoIter: DoubleEndedIterator,

Inserts children before this node’s first child.

Prepending a fragment prepends all the fragment node’s children. The fragment will be empty after this method because its children will be detached before being prepended to self.

§Panics

This method panics if self is a Text, comment or Doctype node.

§Example
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div>text</div>").unwrap();
let child = fragment.first_child().unwrap();

child.prepend(h.span([], []));
assert_eq!(fragment.outer_html(), "<div><span></span>text</div>");
Source

pub fn insert_before<I>(&'a self, siblings: I) -> &'a Node<'a>
where I: IntoIterator<Item = &'a Node<'a>>,

Inserts siblings before this node.

Inserting a fragment inserts all the fragment node’s children. The fragment will be empty after this method because its children will be detached before being inserted before self.

§Panics

Fragment nodes have no siblings, so this method panics if self is a fragment node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div></div>").unwrap();
let child = fragment.first_child().unwrap();

child.insert_before(h.span([], []));
assert_eq!(fragment.outer_html(), "<span></span><div></div>");
Source

pub fn insert_after<I>(&'a self, siblings: I) -> &'a Node<'a>
where I: IntoIterator<Item = &'a Node<'a>>, <I as IntoIterator>::IntoIter: DoubleEndedIterator,

Inserts siblings after this node.

Inserting a fragment inserts all the fragment node’s children. The fragment will be empty after this method because its children will be detached before being inserted after self.

§Panics

Fragment nodes have no siblings, so this method panics if self is a fragment node.

§Examples
let h = htmlite::NodeArena::new();
let fragment = htmlite::parse(&h, "<div></div>").unwrap();
let child = fragment.first_child().unwrap();

child.insert_after(h.span([], []));
assert_eq!(fragment.outer_html(), "<div></div><span></span>");

Trait Implementations§

Source§

impl Debug for Element<'_>

Source§

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

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

impl<'a> Deref for Element<'a>

Source§

type Target = Node<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a> Element for &Element<'a>

Source§

fn parent_element(&self) -> Option<Self>

Returns a parent element.
Source§

fn prev_sibling_element(&self) -> Option<Self>

Returns a previous sibling element.
Source§

fn has_local_name(&self, name: &str) -> bool

Checks that the element has a specified local name.
Source§

fn attribute_matches( &self, local_name: &str, operator: AttributeOperator<'_>, ) -> bool

Checks that the element has a specified attribute.
Source§

fn pseudo_class_matches(&self, class: PseudoClass<'_>) -> bool

Checks that the element matches a specified pseudo-class.
Source§

impl<'a> Index<&str> for Element<'a>

Source§

type Output = Attr<'a>

The returned type after indexing.
Source§

fn index(&self, index: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> PartialEq for Element<'a>

Source§

fn eq(&self, other: &Element<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for Element<'a>

Source§

impl<'a> StructuralPartialEq for Element<'a>

Auto Trait Implementations§

§

impl<'a> !Freeze for Element<'a>

§

impl<'a> !RefUnwindSafe for Element<'a>

§

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

§

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

§

impl<'a> Unpin for Element<'a>

§

impl<'a> !UnwindSafe for Element<'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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.