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>

source

pub fn wrap(node: NodeRef<'a, Node>) -> Option<Self>

Wraps a NodeRef only if it references a Node::Element.

source

pub fn value(&self) -> &'a Element

Returns the Element referenced by self.

Examples found in repository?
examples/document.rs (line 26)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let mut input = String::new();
    let mut stdout = io::stdout();
    let mut stdin = io::stdin();

    write!(stdout, "CSS selector: ").unwrap();
    stdout.flush().unwrap();
    stdin.read_line(&mut input).unwrap();
    let selector = Selector::parse(&input).unwrap();

    writeln!(stdout, "HTML document:").unwrap();
    stdout.flush().unwrap();
    input.clear();
    stdin.read_to_string(&mut input).unwrap();
    let document = Html::parse_document(&input);

    println!("{:#?}", document);

    for node in document.select(&selector) {
        println!("{:?}", node.value());
    }
}
More examples
Hide additional examples
examples/fragment.rs (line 26)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let mut input = String::new();
    let mut stdout = io::stdout();
    let mut stdin = io::stdin();

    write!(stdout, "CSS selector: ").unwrap();
    stdout.flush().unwrap();
    stdin.read_line(&mut input).unwrap();
    let selector = Selector::parse(&input).unwrap();

    writeln!(stdout, "HTML fragment:").unwrap();
    stdout.flush().unwrap();
    input.clear();
    stdin.read_to_string(&mut input).unwrap();
    let fragment = Html::parse_fragment(&input);

    println!("{:#?}", fragment);

    for node in fragment.select(&selector) {
        println!("{:?}", node.value());
    }
}
source

pub fn select<'b>(&self, selector: &'b Selector) -> Select<'a, 'b>

Returns an iterator over descendent elements matching a selector.

source

pub fn html(&self) -> String

Returns the HTML of this element.

source

pub fn inner_html(&self) -> String

Returns the inner HTML of this element.

source

pub fn attr(&self, attr: &str) -> Option<&'a str>

Returns the value of an attribute.

source

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

Returns an iterator over descendent text nodes.

source

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"]);
source

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>>§

source

pub fn id(&self) -> NodeId

Returns the ID of this node.

source

pub fn tree(&self) -> &'a Tree<T>

Returns the tree owning this node.

source

pub fn value(&self) -> &'a T

Returns the value of this node.

source

pub fn parent(&self) -> Option<NodeRef<'a, T>>

Returns the parent of this node.

source

pub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>

Returns the previous sibling of this node.

source

pub fn next_sibling(&self) -> Option<NodeRef<'a, T>>

Returns the next sibling of this node.

source

pub fn first_child(&self) -> Option<NodeRef<'a, T>>

Returns the first child of this node.

source

pub fn last_child(&self) -> Option<NodeRef<'a, T>>

Returns the last child of this node.

source

pub fn has_siblings(&self) -> bool

Returns true if this node has siblings.

source

pub fn has_children(&self) -> bool

Returns true if this node has children.

source

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

Returns an iterator over ancestors.

source

pub fn prev_siblings(&self) -> PrevSiblings<'a, T>

Returns an iterator over previous siblings.

source

pub fn next_siblings(&self) -> NextSiblings<'a, T>

Returns an iterator over next siblings.

source

pub fn first_children(&self) -> FirstChildren<'a, T>

Returns an iterator over first children.

source

pub fn last_children(&self) -> LastChildren<'a, T>

Returns an iterator over last children.

source

pub fn children(&self) -> Children<'a, T>

Returns an iterator over children.

source

pub fn traverse(&self) -> Traverse<'a, T>

Returns an iterator which traverses the subtree starting at this node.

source

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>

source§

fn clone(&self) -> ElementRef<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for ElementRef<'a>

source§

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

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

impl<'a> Deref for ElementRef<'a>

§

type Target = NodeRef<'a, Node>

The resulting type after dereferencing.
source§

fn deref(&self) -> &NodeRef<'a, Node>

Dereferences the value.
source§

impl<'a> Element for ElementRef<'a>

Note: will never match against non-tree-structure pseudo-classes.

§

type Impl = Simple

source§

fn opaque(&self) -> OpaqueElement

Converts self into an opaque representation.
source§

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

source§

fn parent_node_is_shadow_root(&self) -> bool

Whether the parent node of this element is a shadow root.
source§

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

The host of the containing shadow root, if any.
source§

fn is_pseudo_element(&self) -> bool

Whether we’re matching on a pseudo-element.
source§

fn is_part(&self, _name: &CssLocalName) -> bool

source§

fn is_same_type(&self, other: &Self) -> bool

Whether this element and the other element have the same local name and namespace.
source§

fn imported_part(&self, _: &CssLocalName) -> Option<CssLocalName>

Returns the mapping from the exportparts attribute in the reverse direction, that is, in an outer-tree -> inner-tree direction.
source§

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

Skips non-element nodes
source§

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

Skips non-element nodes
source§

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

Skips non-element nodes
source§

fn is_html_element_in_html_document(&self) -> bool

source§

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

source§

fn has_namespace(&self, namespace: &Namespace) -> bool

Empty string for no namespace
source§

fn attr_matches( &self, ns: &NamespaceConstraint<&Namespace>, local_name: &CssLocalName, operation: &AttrSelectorOperation<&CssString> ) -> bool

source§

fn match_non_ts_pseudo_class( &self, _pc: &NonTSPseudoClass, _context: &mut MatchingContext<'_, Self::Impl> ) -> bool

source§

fn match_pseudo_element( &self, _pe: &PseudoElement, _context: &mut MatchingContext<'_, Self::Impl> ) -> bool

Whether this element is a link.
source§

fn is_html_slot_element(&self) -> bool

Returns whether the element is an HTML element.
source§

fn has_id(&self, id: &CssLocalName, case_sensitivity: CaseSensitivity) -> bool

source§

fn has_class( &self, name: &CssLocalName, case_sensitivity: CaseSensitivity ) -> bool

source§

fn is_empty(&self) -> bool

Returns whether this element matches :empty. Read more
source§

fn is_root(&self) -> bool

Returns whether this element matches :root, i.e. whether it is the root element of a document. Read more
source§

fn apply_selector_flags(&self, _flags: ElementSelectorFlags)

Sets selector flags on the elemnt itself or the parent, depending on the flags, which indicate what kind of work may need to be performed when DOM state changes.
source§

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

The parent of a given pseudo-element, after matching a pseudo-element selector. Read more
source§

fn has_attr_in_no_namespace( &self, local_name: &<Self::Impl as SelectorImpl>::LocalName ) -> bool

source§

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

Returns the assigned element this element is assigned to. Read more
source§

fn ignores_nth_child_selectors(&self) -> bool

Returns whether this element should ignore matching nth child selector.
source§

impl<'a> PartialEq for ElementRef<'a>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Selectable<'a> for ElementRef<'a>

§

type Select<'b> = Select<'a, 'b>

Iterator over element references matching a [CSS selectorSelector
source§

fn select(self, selector: &Selector) -> Self::Select<'_>

Applies the given selector to the collection of elements represented by self
source§

impl<'a> Serialize for ElementRef<'a>

source§

fn serialize<S: Serializer>( &self, serializer: &mut S, traversal_scope: TraversalScope ) -> Result<(), Error>

Take the serializer and call its methods to serialize this type. The type will dictate which methods are called and with what parameters.
source§

impl<'a> Copy for ElementRef<'a>

source§

impl<'a> Eq for ElementRef<'a>

source§

impl<'a> StructuralPartialEq for ElementRef<'a>

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<'a> Unpin for ElementRef<'a>

§

impl<'a> !UnwindSafe for ElementRef<'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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.