pub struct ElementRef<'a> { /* private fields */ }
Expand description
Wrapper around a reference to an element node.
This wrapper implements the Element
trait from the selectors
crate, which allows it to be
matched against CSS selectors.
Implementations§
Source§impl<'a> ElementRef<'a>
impl<'a> ElementRef<'a>
Sourcepub fn wrap(node: NodeRef<'a, Node>) -> Option<Self>
pub fn wrap(node: NodeRef<'a, Node>) -> Option<Self>
Wraps a NodeRef
only if it references a Node::Element
.
Sourcepub fn value(&self) -> &'a Element
pub fn value(&self) -> &'a Element
Returns the Element
referenced by self
.
Examples found in repository?
7fn main() {
8 let mut input = String::new();
9 let mut stdout = io::stdout();
10 let mut stdin = io::stdin();
11
12 write!(stdout, "CSS selector: ").unwrap();
13 stdout.flush().unwrap();
14 stdin.read_line(&mut input).unwrap();
15 let selector = Selector::parse(&input).unwrap();
16
17 writeln!(stdout, "HTML document:").unwrap();
18 stdout.flush().unwrap();
19 input.clear();
20 stdin.read_to_string(&mut input).unwrap();
21 let document = Html::parse_document(&input);
22
23 println!("{:#?}", document);
24
25 for node in document.select(&selector) {
26 println!("{:?}", node.value());
27 }
28}
More examples
7fn main() {
8 let mut input = String::new();
9 let mut stdout = io::stdout();
10 let mut stdin = io::stdin();
11
12 write!(stdout, "CSS selector: ").unwrap();
13 stdout.flush().unwrap();
14 stdin.read_line(&mut input).unwrap();
15 let selector = Selector::parse(&input).unwrap();
16
17 writeln!(stdout, "HTML fragment:").unwrap();
18 stdout.flush().unwrap();
19 input.clear();
20 stdin.read_to_string(&mut input).unwrap();
21 let fragment = Html::parse_fragment(&input);
22
23 println!("{:#?}", fragment);
24
25 for node in fragment.select(&selector) {
26 println!("{:?}", node.value());
27 }
28}
Sourcepub fn select<'b>(&self, selector: &'b Selector) -> Select<'a, 'b> ⓘ
pub fn select<'b>(&self, selector: &'b Selector) -> Select<'a, 'b> ⓘ
Returns an iterator over descendent elements matching a selector.
Sourcepub fn inner_html(&self) -> String
pub fn inner_html(&self) -> String
Returns the inner HTML of this element.
Sourcepub fn child_elements(&self) -> impl Iterator<Item = ElementRef<'a>>
pub fn child_elements(&self) -> impl Iterator<Item = ElementRef<'a>>
Iterate over all child nodes which are elements
§Example
let fragment = Html::parse_fragment("foo<span>bar</span><a>baz</a>qux");
let children = fragment.root_element().child_elements().map(|element| element.value().name()).collect::<Vec<_>>();
assert_eq!(children, ["span", "a"]);
Sourcepub fn descendent_elements(&self) -> impl Iterator<Item = ElementRef<'a>>
pub fn descendent_elements(&self) -> impl Iterator<Item = ElementRef<'a>>
Iterate over all descendent nodes which are elements
§Example
let fragment = Html::parse_fragment("foo<span><b>bar</b></span><a><i>baz</i></a>qux");
let descendants = fragment.root_element().descendent_elements().map(|element| element.value().name()).collect::<Vec<_>>();
assert_eq!(descendants, ["html", "span", "b", "a", "i"]);
Methods from Deref<Target = NodeRef<'a, Node>>§
Sourcepub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>
pub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>
Returns the previous sibling of this node.
Sourcepub fn next_sibling(&self) -> Option<NodeRef<'a, T>>
pub fn next_sibling(&self) -> Option<NodeRef<'a, T>>
Returns the next sibling of this node.
Sourcepub fn first_child(&self) -> Option<NodeRef<'a, T>>
pub fn first_child(&self) -> Option<NodeRef<'a, T>>
Returns the first child of this node.
Sourcepub fn last_child(&self) -> Option<NodeRef<'a, T>>
pub fn last_child(&self) -> Option<NodeRef<'a, T>>
Returns the last child of this node.
Sourcepub fn has_siblings(&self) -> bool
pub fn has_siblings(&self) -> bool
Returns true if this node has siblings.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Returns true if this node has children.
Sourcepub fn prev_siblings(&self) -> PrevSiblings<'a, T>
pub fn prev_siblings(&self) -> PrevSiblings<'a, T>
Returns an iterator over previous siblings.
Sourcepub fn next_siblings(&self) -> NextSiblings<'a, T>
pub fn next_siblings(&self) -> NextSiblings<'a, T>
Returns an iterator over next siblings.
Sourcepub fn first_children(&self) -> FirstChildren<'a, T>
pub fn first_children(&self) -> FirstChildren<'a, T>
Returns an iterator over first children.
Sourcepub fn last_children(&self) -> LastChildren<'a, T>
pub fn last_children(&self) -> LastChildren<'a, T>
Returns an iterator over last children.
Sourcepub fn traverse(&self) -> Traverse<'a, T>
pub fn traverse(&self) -> Traverse<'a, T>
Returns an iterator which traverses the subtree starting at this node.
Sourcepub fn descendants(&self) -> Descendants<'a, T>
pub fn descendants(&self) -> Descendants<'a, T>
Returns an iterator over this node and its descendants.
Trait Implementations§
Source§impl<'a> Clone for ElementRef<'a>
impl<'a> Clone for ElementRef<'a>
Source§fn clone(&self) -> ElementRef<'a>
fn clone(&self) -> ElementRef<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ElementRef<'_>
impl Debug for ElementRef<'_>
Source§impl<'a> Deref for ElementRef<'a>
impl<'a> Deref for ElementRef<'a>
Source§impl Element for ElementRef<'_>
Note: will never match against non-tree-structure pseudo-classes.
impl Element for ElementRef<'_>
Note: will never match against non-tree-structure pseudo-classes.
type Impl = Simple
Source§fn opaque(&self) -> OpaqueElement
fn opaque(&self) -> OpaqueElement
fn parent_element(&self) -> Option<Self>
Source§fn parent_node_is_shadow_root(&self) -> bool
fn parent_node_is_shadow_root(&self) -> bool
Source§fn containing_shadow_host(&self) -> Option<Self>
fn containing_shadow_host(&self) -> Option<Self>
Source§fn is_pseudo_element(&self) -> bool
fn is_pseudo_element(&self) -> bool
fn is_part(&self, _name: &CssLocalName) -> bool
Source§fn is_same_type(&self, other: &Self) -> bool
fn is_same_type(&self, other: &Self) -> bool
other
element have the same local name and namespace.Source§fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName>
fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName>
exportparts
attribute in the reverse
direction, that is, in an outer-tree -> inner-tree direction.Source§fn prev_sibling_element(&self) -> Option<Self>
fn prev_sibling_element(&self) -> Option<Self>
Source§fn next_sibling_element(&self) -> Option<Self>
fn next_sibling_element(&self) -> Option<Self>
Source§fn first_element_child(&self) -> Option<Self>
fn first_element_child(&self) -> Option<Self>
fn is_html_element_in_html_document(&self) -> bool
fn has_local_name(&self, name: &CssLocalName) -> bool
Source§fn has_namespace(&self, namespace: &Namespace) -> bool
fn has_namespace(&self, namespace: &Namespace) -> bool
fn attr_matches( &self, ns: &NamespaceConstraint<&Namespace>, local_name: &CssLocalName, operation: &AttrSelectorOperation<&CssString>, ) -> bool
fn match_non_ts_pseudo_class( &self, _pc: &NonTSPseudoClass, _context: &mut MatchingContext<'_, Self::Impl>, ) -> bool
fn match_pseudo_element( &self, _pe: &PseudoElement, _context: &mut MatchingContext<'_, Self::Impl>, ) -> bool
Source§fn is_html_slot_element(&self) -> bool
fn is_html_slot_element(&self) -> bool
fn has_id(&self, id: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool
fn has_class( &self, name: &CssLocalName, case_sensitivity: CaseSensitivity, ) -> bool
fn has_custom_state(&self, _name: &CssLocalName) -> bool
Source§fn is_root(&self) -> bool
fn is_root(&self) -> bool
:root
,
i.e. whether it is the root element of a document. Read more