NodeRef

Struct NodeRef 

Source
pub struct NodeRef<'a> {
    pub id: NodeId,
    pub tree: &'a Tree,
}
Expand description

Represents a reference to a node in the tree. It keeps a node id and a reference to the tree, which allows to access to the actual tree node with NodeData.

Fields§

§id: NodeId§tree: &'a Tree

Implementations§

Source§

impl<'a> NodeRef<'a>

Source

pub fn new(id: NodeId, tree: &'a Tree) -> Self

Creates a new node reference.

Source

pub fn query<F, B>(&self, f: F) -> Option<B>
where F: FnOnce(&TreeNode) -> B,

Selects the node from the tree and applies a function to it.

Source

pub fn query_or<F, B>(&self, default: B, f: F) -> B
where F: FnOnce(&TreeNode) -> B,

Selects the node from the tree and applies a function to it. Accepts a default value to return for a case if the node doesn’t exist.

Source

pub fn update<F, B>(&self, f: F) -> Option<B>
where F: FnOnce(&mut TreeNode) -> B,

Selects the node from the tree and applies a function to it.

Source

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

Returns the parent node of the selected node.

Source

pub fn children(&self) -> Vec<Self>

Returns the child nodes of the selected node.

Source

pub fn children_it(&self, rev: bool) -> impl Iterator<Item = Self>

Returns the iterator child nodes of the selected node.

Source

pub fn ancestors(&self, max_depth: Option<usize>) -> Vec<Self>

Returns ancestor nodes of the selected node.

§Arguments
  • max_depth - The maximum depth of the ancestors. If None, or Some(0) the maximum depth is unlimited.
§Returns

Vec<Self>

Source

pub fn ancestors_it( &self, max_depth: Option<usize>, ) -> impl Iterator<Item = Self>

Returns the iterator ancestor nodes of the selected node.

§Arguments
  • max_depth - The maximum depth of the ancestors. If None, or Some(0) the maximum depth is unlimited.
§Returns

impl Iterator<Item = Self>

Source

pub fn descendants(&self) -> Vec<Self>

Returns the descendant nodes of the selected node.

§Returns

Vec<NodeRef> – a vector of descendant nodes

Source

pub fn descendants_it(&self) -> impl Iterator<Item = Self>

Returns an iterator of the descendant nodes of the selected node.

§Returns

impl Iterator<Item = Self> – an iterator of descendant nodes

Source

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

Returns the first child node of the selected node.

Source

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

Returns the last child node of the selected node.

Source

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

Returns the next sibling node of the selected node.

Source

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

Returns the previous sibling node of the selected node.

Source

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

Returns the last sibling node of the selected node.

Source§

impl NodeRef<'_>

Source

pub fn remove_from_parent(&self)

Removes the selected node from its parent node, but keeps it in the tree.

Source

pub fn remove_children(&self)

Removes all children nodes of the selected node.

Source

pub fn insert_before<P: NodeIdProver>(&self, id_provider: P)

Inserts another node by id before the selected node. Another node takes place of the selected node shifting it to right.

Source

pub fn insert_after<P: NodeIdProver>(&self, id_provider: P)

Inserts another node by id after the selected node. Another node takes place of the next sibling of the selected node.

Source

pub fn append_child<P: NodeIdProver>(&self, id_provider: P)

Appends another node by id to the selected node.

Source

pub fn append_children<P: NodeIdProver>(&self, id_provider: P)

Appends another node and it’s siblings to the selected node.

Source

pub fn prepend_child<P: NodeIdProver>(&self, id_provider: P)

Prepend another node by id to the selected node.

Source

pub fn prepend_children<P: NodeIdProver>(&self, id_provider: P)

Prepend another node and it’s siblings to the selected node.

Source

pub fn insert_siblings_before<P: NodeIdProver>(&self, id_provider: P)

Inserts another node and it’s siblings before the current node shifting itself.

Source

pub fn insert_siblings_after<P: NodeIdProver>(&self, id_provider: P)

Inserts another node and it’s siblings after the current node.

Source

pub fn replace_with<P: NodeIdProver>(&self, id_provider: P)

Replaces the current node with other node by id. It’is actually a shortcut of two operations: NodeRef::insert_before and NodeRef::remove_from_parent.

Source

pub fn replace_with_html<T>(&self, html: T)
where T: Into<StrTendril>,

Replaces the current node with other node, created from the given fragment html. Behaves similarly to crate::Selection::replace_with_html but only for one node.

Source

pub fn append_html<T>(&self, html: T)
where T: Into<StrTendril>,

Parses given fragment html and appends its contents to the selected node.

Source

pub fn prepend_html<T>(&self, html: T)
where T: Into<StrTendril>,

Parses given fragment html and appends its contents to the selected node.

Source

pub fn before_html<T>(&self, html: T)
where T: Into<StrTendril>,

Parses given fragment html inserts its contents before to the selected node.

Source

pub fn after_html<T>(&self, html: T)
where T: Into<StrTendril>,

Parses given fragment html inserts its contents after to the selected node.

Source

pub fn set_html<T>(&self, html: T)
where T: Into<StrTendril>,

Parses given fragment html and sets its contents to the selected node.

Source

pub fn set_text<T>(&self, text: T)
where T: Into<StrTendril>,

Parses given text and sets its contents to the selected node.

This operation replaces any contents of the selected node with the given text. Doesn’t escapes the text.

Source

pub fn wrap_node<P: NodeIdProver>(&self, new_parent: P)

Wraps the current node in a new parent element. The parent node becomes the parent of the current node, replacing it in the original structure.

Source

pub fn wrap_html<T>(&self, html: T)
where T: Into<StrTendril>,

Wraps the current node with the given HTML fragment. The outermost node of the fragment becomes the new parent of the current node.

Important: The HTML fragment must be a one, valid HTML element.

Source

pub fn unwrap_node(&self)

Unwrap the node (and it’s siblings) from its parent, removing the parent node from the tree. If the parent does not exist or is not an element, it does nothing.

Source§

impl NodeRef<'_>

Source

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

Returns the next sibling, that is an NodeData::Element of the selected node.

Source

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

Returns the previous sibling, that is an NodeData::Element of the selected node.

Source

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

Returns the first child, that is an NodeData::Element of the selected node.

Source

pub fn element_children(&self) -> Vec<Self>

Returns children, that are NodeData::Elements of the selected node.

Source§

impl NodeRef<'_>

Source

pub fn node_name(&self) -> Option<StrTendril>

Returns the name of the selected node if it is an NodeData::Element otherwise None.

Source

pub fn id_attr(&self) -> Option<StrTendril>

Returns the value of the id attribute

Source

pub fn class(&self) -> Option<StrTendril>

Returns the value of the class attribute

Source

pub fn has_class(&self, class: &str) -> bool

Checks if node has a specified class

Source

pub fn add_class(&self, class: &str)

Adds a class to the node

Source

pub fn remove_class(&self, class: &str)

Removes a class from the node

Source

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

Returns the value of the specified attribute

Source

pub fn attr_or<T>(&self, name: &str, default: T) -> StrTendril
where Tendril<UTF8>: From<T>,

Returns the value of the specified attribute

Source

pub fn attrs(&self) -> Vec<Attribute>

Returns all attributes

Source

pub fn set_attr(&self, name: &str, val: &str)

Sets the value of the specified attribute to the node.

Source

pub fn remove_attr(&self, name: &str)

Removes the specified attribute from the element.

Source

pub fn remove_attrs(&self, names: &[&str])

Removes the specified attributes from the element.

§Arguments
  • names: A slice of attribute names to remove. Empty slice removes no attributes.
Source

pub fn retain_attrs(&self, names: &[&str])

Retains only the attributes with the specified names.

§Arguments
  • names: A slice of attribute names to retain. Empty slice retains no attributes.
Source

pub fn remove_all_attrs(&self)

Removes all attributes from the element.

Source

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

Checks if node has a specified attribute

Source

pub fn rename(&self, name: &str)

Renames the node if node is an NodeData::Element.

Source§

impl NodeRef<'_>

Source

pub fn is_document(&self) -> bool

Returns true if this node is a document.

Source

pub fn is_fragment(&self) -> bool

Returns true if this node is a fragment.

Source

pub fn is_element(&self) -> bool

Returns true if this node is an element.

Source

pub fn is_text(&self) -> bool

Returns true if this node is a text node.

Source

pub fn is_comment(&self) -> bool

Returns true if this node is a comment.

Source

pub fn is_doctype(&self) -> bool

Returns true if this node is a DOCTYPE.

Source

pub fn may_have_children(&self) -> bool

Checks if node may have children nodes.

Source§

impl NodeRef<'_>

Source

pub fn html(&self) -> StrTendril

Returns the HTML representation of the DOM tree. Panics if serialization fails.

Source

pub fn inner_html(&self) -> StrTendril

Returns the HTML representation of the DOM tree without the outermost node. Panics if serialization fails.

Source

pub fn try_html(&self) -> Option<StrTendril>

Source

pub fn try_inner_html(&self) -> Option<StrTendril>

Source

pub fn text(&self) -> StrTendril

Returns the text of the node and its descendants.

Source

pub fn immediate_text(&self) -> StrTendril

Returns the text of the node without its descendants.

Source

pub fn formatted_text(&self) -> StrTendril

Returns the formatted text of the node and its descendants. This is the same as the text() method, but with a few differences:

  • Whitespace is normalized so that there is only one space between words.
  • All whitespace is removed from the beginning and end of the text.
  • After block elements, a double newline is added.
  • For elements like br, ‘hr’, ‘li’, ‘tr’ a single newline is added.
Source

pub fn has_text(&self, needle: &str) -> bool

Checks if the node contains the specified text

Source

pub fn has_only_text(&self) -> bool

Checks if the node contains only text node

Source

pub fn is_empty_element(&self) -> bool

Checks if the node is an empty element.

Determines if the node is an element, has no child elements, and any text nodes it contains consist only of whitespace.

Source

pub fn normalize(&self)

Merges adjacent text nodes and removes empty text nodes.

Normalization is necessary to ensure that adjacent text nodes are merged into one text node.

Source

pub fn strip_elements(&self, names: &[&str])

Strips all elements with the specified names from the node’s descendants.

If matched element has children, they will be assigned to the parent of the matched element.

§Arguments
  • names - A list of element names to strip.
Source

pub fn to_fragment(&self) -> Document

Creates a full copy of the node’s contents as a Document fragment.

Source§

impl NodeRef<'_>

Source

pub fn is_match(&self, matcher: &Matcher) -> bool

Checks if the node matches the given matcher

Source

pub fn is(&self, sel: &str) -> bool

Checks if the node matches the given selector

Source

pub fn base_uri(&self) -> Option<StrTendril>

Returns the base URI of the document.

This is the value of the <base> element in the document’s head, or None if the document does not have a <base> element.

Source

pub fn find(&self, path: &[&str]) -> Vec<Self>

Finds all descendant elements of this node that match the given path.

The path is a sequence of element names. The method returns a vector of NodeRefs that correspond to the matching elements. The elements are returned in the order they appear in the document tree.

§Experimental

This method is experimental and may change in the future. The path argument will be revised.

Source

pub fn normalized_char_count(&self) -> usize

Traverses the tree and counts all text content of a node and its descendants, but only counting each sequence of whitespace as a single character.

This function will traverse the tree and count all text content from the node and its descendants.

It has an advantage over node.text().split_whitespace().count() because it doesn’t need to collect and consume the text content.

§Returns

The number of characters that would be in the text content if it were normalized, where normalization means treating any sequence of whitespace characters as a single space.

Source§

impl<'a> NodeRef<'a>

Source

pub fn element_ref(&self) -> Option<Ref<'a, Element>>

Returns a reference to the element node that this node references, if it is an element.

Returns None if the node is not an element.

Source

pub fn qual_name_ref(&self) -> Option<Ref<'a, QualName>>

Gets node’s qualified name

Returns None if the node is not an element or the element name cannot be accessed.

Source

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

Checks if the node is an element with the given name.

Returns false if the node is not an element.

Source

pub fn is_nonempty_text(&self) -> bool

Checks if the node is a nonempty text node.

Returns true if the node is a text node and its text content is not empty. Returns false if the node is not a text node or its text content is empty.

Source§

impl NodeRef<'_>

Source

pub fn md(&self, skip_tags: Option<&[&str]>) -> StrTendril

Produces a Markdown representation of the node and its descendants,
skipping elements matching the specified skip_tags list along with their descendants.

  • If skip_tags is None, the default list is used: ["script", "style", "meta", "head"].
  • To process all elements without exclusions, pass Some(&[]).

Trait Implementations§

Source§

impl<'a> Clone for NodeRef<'a>

Source§

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

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<'a> Debug for NodeRef<'a>

Source§

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

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

impl Element for NodeRef<'_>

Source§

fn opaque(&self) -> OpaqueElement

Converts self into an opaque representation. It can be crucial.

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

Whether this element is a link.

Source§

fn is_html_slot_element(&self) -> bool

Whether the element is an HTML element.

Source§

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

Returns the mapping from the exportparts attribute in the regular direction, that is, outer-tree->inner-tree.

Source§

fn is_empty(&self) -> bool

Whether this element matches :empty.

Source§

fn is_root(&self) -> bool

Whether this element matches :root, i.e. whether it is the root element of a document.

Source§

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

Returns the first element child. Skips non-element nodes.

Source§

type Impl = InnerSelector

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

Source§

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

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 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, pseudo: &<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 has_id( &self, name: &<Self::Impl as SelectorImpl>::Identifier, case_sensitivity: CaseSensitivity, ) -> bool

Source§

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

Source§

fn is_part(&self, _name: &CssLocalName) -> 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.
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> From<NodeRef<'a>> for Selection<'a>

Source§

fn from(node: NodeRef<'a>) -> Selection<'a>

Converts to this type from the input type.
Source§

impl NodeIdProver for &NodeRef<'_>

Source§

fn node_id(&self) -> &NodeId

Returns the NodeId
Source§

impl<'a> Copy for NodeRef<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for NodeRef<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for NodeRef<'a>

§

impl<'a> !UnwindSafe for NodeRef<'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> 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.