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§
Methods from Deref<Target = Node<'a>>§
Sourcepub fn children(&self) -> Following<'a> ⓘ
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>"]
);
Sourcepub fn reverse_children(&self) -> Preceding<'a> ⓘ
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>"]
);
Sourcepub fn ancestors(&'a self) -> Ancestors<'a> ⓘ
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"]
);
Sourcepub fn preceding(&'a self) -> Preceding<'a> ⓘ
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"]
);
Sourcepub fn following(&'a self) -> Following<'a> ⓘ
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"]
);
Sourcepub fn descendants(&'a self) -> Descendants<'a> ⓘ
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"]
);
Sourcepub fn text_content(&'a self) -> TextContent<'a>
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"]
);
Sourcepub fn select<'s>(&'a self, selector: &'s str) -> Selected<'a, 's> ⓘ
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.
Pattern | Meaning |
---|---|
* | Matches any element |
div | Matches any element whose tag name is div |
div button | Matches any button element that is an ancestor of a div element |
div > button | Matches any button element that is a direct child of a div element |
div:first-child | Matches any div element if it is the first child of its parent |
div + button | Matches 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#myid | Matches any div element with an id attribute value equal to myid |
`div.myclass | Matches any div element with a class attribute value equal to myclass |
Sourcepub fn parent(&self) -> Option<&'a Node<'a>>
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);
Sourcepub fn first_child(&self) -> Option<&'a Node<'a>>
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);
Sourcepub fn last_child(&self) -> Option<&'a Node<'a>>
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());
Sourcepub fn previous(&self) -> Option<&'a Node<'a>>
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());
Sourcepub fn next(&self) -> Option<&'a Node<'a>>
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());
Sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Sourcepub fn is_doctype(&self) -> bool
pub fn is_doctype(&self) -> bool
Sourcepub fn is_comment(&self) -> bool
pub fn is_comment(&self) -> bool
Sourcepub fn is_fragment(&self) -> bool
pub fn is_fragment(&self) -> bool
Sourcepub fn as_element(&self) -> Option<&Element<'a>>
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")
);
Sourcepub fn as_comment(&'a self) -> Option<&'a Comment<'a>>
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")
);
Sourcepub fn as_doctype(&'a self) -> Option<&'a Doctype<'a>>
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")
);
Sourcepub fn outer_html(&self) -> String
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>");
Sourcepub fn inner_html(&self) -> String
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");
Sourcepub fn detach(&'a self) -> &'a Node<'a>
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>");
Sourcepub fn append<I>(&'a self, children: I) -> &'a Node<'a>where
I: IntoIterator<Item = &'a Node<'a>>,
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>");
Sourcepub fn prepend<I>(&'a self, children: I) -> &'a Node<'a>
pub fn prepend<I>(&'a self, children: I) -> &'a Node<'a>
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>");
Sourcepub fn insert_before<I>(&'a self, siblings: I) -> &'a Node<'a>where
I: IntoIterator<Item = &'a Node<'a>>,
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>");
Sourcepub fn insert_after<I>(&'a self, siblings: I) -> &'a Node<'a>
pub fn insert_after<I>(&'a self, siblings: I) -> &'a Node<'a>
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>");