Struct TreeRef

Source
pub struct TreeRef<'a, 'b> {
    pub id: TreeRefId,
    pub tree: &'b Tree<'a>,
}
Expand description

A reference to a node in the tree.

Fields§

§id: TreeRefId

A unique ID identifying the node in the tree.

§tree: &'b Tree<'a>

The tree owning this node.

Implementations§

Source§

impl<'a, 'b> TreeRef<'a, 'b>

Source

pub fn id(&self) -> TreeRefId

Source

pub fn spanned_data(&self) -> &ParsedNode<'a>

Get an immutable reference to the underlying ParsedNode containing the Node and the location of the span of text where it was defined.

§Panics

Panics if the node does not exist in the tree (it could have been removed).

Source

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

Get an immutable reference to the underlying Node.

§Panics

Panics if the node does not exist in the tree (it could have been removed).

Source

pub fn parent(&self) -> Self

Get a reference to the parent of this node.

Source

pub fn closest(&self, selector: &str) -> Option<Self>

Search all parents of the node until it finds a node matching the provided CSS selector.

Returns None if the selector is invalid or if a node matching the selector could not be found.

This method is equivalent to the Element.closest method in Javascript.

Source

pub fn prev_sibling(&self) -> Option<Self>

Get a reference to the previous sibling of this node.

Source

pub fn next_sibling(&self) -> Option<Self>

Get a reference to the next sibling of this node.

Source

pub fn children<'c>(&'c self) -> impl Iterator<Item = TreeRef<'a, 'b>> + 'c

Get a list of references of the children of this node.

Source

pub fn children_id(&self) -> Vec<TreeRefId>

Get a cloned list of IDs of this node children.

Source

pub fn select<'c>( &self, selector: &'c str, ) -> Result<Option<TreeRef<'_, '_>>, SelectorParseError<'c>>

Search the first node matching the CSS selector in the tree, relatively to this node.

The traversal order used is the same as in the official DOM specification ie preorder, depth-first.

It is similar to the document.querySelector function in JavaScript.

§Errors

Returns selectors::parser::SelectorParseError if the CSS selector is not a valid CSS selector or Ok(None) if no node matching the CSS selector is found in the tree

Source

pub fn select_all(&self, selector: &str) -> Vec<TreeRef<'_, '_>>

Select several nodes in the tree, relatively to this node, using a CSS selector.

It is similar to the document.querySelectorAll function in JavaScript.

Returns an empty Vec if the selector is not a valid CSS selector.

Source

pub fn name(&self) -> Result<Cow<'a, str>>

Get the element name of the TreeRef.

§Errors

Returns Error::BadNodeType if the underlying node is not a Node::Element.

Source

pub fn text(&self) -> Cow<'a, str>

Get the text of the TreeRef.

Returns the concatenated text content of child nodes if it is a Node::Element.

Returns the text content if it is a Node::TemplateBlock(TemplateBlock::RawText(_)).

All template expressions and statements just return their raw content with no spaces inside their delimiters (will return {{expr}} even if it was {{ expr }}, {!expr!} event if it was {! expr !} and {%stmt%} even if it was {% stmt %}).

Source

pub fn attr(&self, name: &str) -> Result<Option<String>>

Get the value of an attribute belonging to a Node::Element.

All template expressions and statements just return their raw content with no spaces inside their delimiters (will return {{expr}} even if it was {{ expr }}, {!expr!} event if it was {! expr !} and {%stmt%} even if it was {% stmt %}).

§Errors

Returns Error::BadNodeType if the underlying node is not a Node::Element.

Returns None if the attribute is not present.

Returns Some("") if the attribute is present but does not have a value (e.g. in <input type="checkbox" checked>, the checked HTML attribute is present but does not have a value).

Returns Some(value) if the attribute is present and does have a value.

Source

pub fn attr_spanned( &self, name: &str, ) -> Result<Option<Vec<Spanned<TemplateBlock<'_>>>>>

Get the value of an attribute belonging to a Node::Element.

Template expressions and statements can be returned.

§Errors

Returns Error::BadNodeType if the underlying node is not a Node::Element.

Returns None if the attribute is not present.

Source

pub fn attrs(&self) -> Result<impl Iterator<Item = (&Cow<'_, str>, String)>>

Get all attributes belonging to a Node::Element.

All template expressions and statements just return their raw content with no spaces inside their delimiters (will return {{expr}} even if it was {{ expr }}, {!expr!} event if it was {! expr !} and {%stmt%} even if it was {% stmt %}).

An empty attribute value means that an attribute key was given without a value. See TreeRef::attr for more information.

§Errors

Returns Error::BadNodeType if the underlying node is not a Node::Element.

Source

pub fn attrs_spanned(&self) -> Result<impl Iterator<Item = &Attr<'_>>>

Get all attributes belonging to a Node::Element.

Template expressions and statements can be returned.

§Errors

Returns Error::BadNodeType if the underlying node is not a Node::Element.

Source

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

Source

pub fn traverse_breadth(&self) -> impl Iterator<Item = TreeRef<'a, '_>>

Source

pub fn traverse_depth(&self) -> impl Iterator<Item = TreeRef<'a, '_>>

Source

pub fn is_matching(&self, selector_list: &SelectorList<InnerSelector>) -> bool

Trait Implementations§

Source§

impl<'a, 'b> Clone for TreeRef<'a, 'b>

Source§

fn clone(&self) -> TreeRef<'a, 'b>

Returns a duplicate 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 Debug for TreeRef<'_, '_>

Source§

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

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

impl Element for TreeRef<'_, '_>

Source§

type Impl = InnerSelector

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 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, local_name: &<Self::Impl as SelectorImpl>::BorrowedLocalName, ) -> bool

Source§

fn has_namespace( &self, _ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl, ) -> bool

Empty string for no namespace
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 attr_matches( &self, _ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl>::NamespaceUrl>, local_name: &<Self::Impl as SelectorImpl>::LocalName, operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl>::AttrValue>, ) -> bool

Source§

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

Source§

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

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.
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: &<Self::Impl as SelectorImpl>::Identifier, case_sensitivity: CaseSensitivity, ) -> bool

Source§

fn has_class( &self, name: &<Self::Impl as SelectorImpl>::Identifier, case_sensitivity: CaseSensitivity, ) -> bool

Source§

fn imported_part( &self, _name: &<Self::Impl as SelectorImpl>::Identifier, ) -> Option<<Self::Impl as SelectorImpl>::Identifier>

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

fn is_part(&self, _name: &<Self::Impl as SelectorImpl>::Identifier) -> 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 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§

fn has_custom_state( &self, _name: &<Self::Impl as SelectorImpl>::Identifier, ) -> bool

Source§

fn add_element_unique_hashes(&self, _filter: &mut BloomFilter) -> bool

Add hashes unique to this element to the given filter, returning true if any got added.
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§

impl PartialEq for TreeRef<'_, '_>

Source§

fn eq(&self, other: &Self) -> 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, 'b> Copy for TreeRef<'a, 'b>

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for TreeRef<'a, 'b>

§

impl<'a, 'b> RefUnwindSafe for TreeRef<'a, 'b>

§

impl<'a, 'b> Send for TreeRef<'a, 'b>

§

impl<'a, 'b> Sync for TreeRef<'a, 'b>

§

impl<'a, 'b> Unpin for TreeRef<'a, 'b>

§

impl<'a, 'b> UnwindSafe for TreeRef<'a, 'b>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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.