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 TreeImplementations§
Source§impl<'a> NodeRef<'a>
impl<'a> NodeRef<'a>
Sourcepub fn query<F, B>(&self, f: F) -> Option<B>
pub fn query<F, B>(&self, f: F) -> Option<B>
Selects the node from the tree and applies a function to it.
Sourcepub fn query_or<F, B>(&self, default: B, f: F) -> B
pub fn query_or<F, B>(&self, default: B, f: F) -> 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.
Sourcepub fn update<F, B>(&self, f: F) -> Option<B>
pub fn update<F, B>(&self, f: F) -> Option<B>
Selects the node from the tree and applies a function to it.
Sourcepub fn children_it(&self, rev: bool) -> impl Iterator<Item = Self>
pub fn children_it(&self, rev: bool) -> impl Iterator<Item = Self>
Returns the iterator child nodes of the selected node.
Sourcepub fn ancestors_it(
&self,
max_depth: Option<usize>,
) -> impl Iterator<Item = Self>
pub fn ancestors_it( &self, max_depth: Option<usize>, ) -> impl Iterator<Item = Self>
Sourcepub fn descendants(&self) -> Vec<Self>
pub fn descendants(&self) -> Vec<Self>
Returns the descendant nodes of the selected node.
§Returns
Vec<NodeRef> – a vector of descendant nodes
Sourcepub fn descendants_it(&self) -> impl Iterator<Item = Self>
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
Sourcepub fn first_child(&self) -> Option<Self>
pub fn first_child(&self) -> Option<Self>
Returns the first child node of the selected node.
Sourcepub fn last_child(&self) -> Option<Self>
pub fn last_child(&self) -> Option<Self>
Returns the last child node of the selected node.
Sourcepub fn next_sibling(&self) -> Option<Self>
pub fn next_sibling(&self) -> Option<Self>
Returns the next sibling node of the selected node.
Sourcepub fn prev_sibling(&self) -> Option<Self>
pub fn prev_sibling(&self) -> Option<Self>
Returns the previous sibling node of the selected node.
Sourcepub fn last_sibling(&self) -> Option<Self>
pub fn last_sibling(&self) -> Option<Self>
Returns the last sibling node of the selected node.
Source§impl NodeRef<'_>
impl NodeRef<'_>
Sourcepub fn remove_from_parent(&self)
pub fn remove_from_parent(&self)
Removes the selected node from its parent node, but keeps it in the tree.
Sourcepub fn remove_children(&self)
pub fn remove_children(&self)
Removes all children nodes of the selected node.
Sourcepub fn insert_before<P: NodeIdProver>(&self, id_provider: P)
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.
Sourcepub fn insert_after<P: NodeIdProver>(&self, id_provider: P)
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.
Sourcepub fn append_child<P: NodeIdProver>(&self, id_provider: P)
pub fn append_child<P: NodeIdProver>(&self, id_provider: P)
Appends another node by id to the selected node.
Sourcepub fn append_children<P: NodeIdProver>(&self, id_provider: P)
pub fn append_children<P: NodeIdProver>(&self, id_provider: P)
Appends another node and it’s siblings to the selected node.
Sourcepub fn prepend_child<P: NodeIdProver>(&self, id_provider: P)
pub fn prepend_child<P: NodeIdProver>(&self, id_provider: P)
Prepend another node by id to the selected node.
Sourcepub fn prepend_children<P: NodeIdProver>(&self, id_provider: P)
pub fn prepend_children<P: NodeIdProver>(&self, id_provider: P)
Prepend another node and it’s siblings to the selected node.
Sourcepub fn insert_siblings_before<P: NodeIdProver>(&self, id_provider: P)
pub fn insert_siblings_before<P: NodeIdProver>(&self, id_provider: P)
Inserts another node and it’s siblings before the current node shifting itself.
Sourcepub fn insert_siblings_after<P: NodeIdProver>(&self, id_provider: P)
pub fn insert_siblings_after<P: NodeIdProver>(&self, id_provider: P)
Inserts another node and it’s siblings after the current node.
Sourcepub fn replace_with<P: NodeIdProver>(&self, id_provider: P)
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.
Sourcepub fn replace_with_html<T>(&self, html: T)where
T: Into<StrTendril>,
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.
Sourcepub fn append_html<T>(&self, html: T)where
T: Into<StrTendril>,
pub fn append_html<T>(&self, html: T)where
T: Into<StrTendril>,
Parses given fragment html and appends its contents to the selected node.
Sourcepub fn prepend_html<T>(&self, html: T)where
T: Into<StrTendril>,
pub fn prepend_html<T>(&self, html: T)where
T: Into<StrTendril>,
Parses given fragment html and appends its contents to the selected node.
Sourcepub fn before_html<T>(&self, html: T)where
T: Into<StrTendril>,
pub fn before_html<T>(&self, html: T)where
T: Into<StrTendril>,
Parses given fragment html inserts its contents before to the selected node.
Sourcepub fn after_html<T>(&self, html: T)where
T: Into<StrTendril>,
pub fn after_html<T>(&self, html: T)where
T: Into<StrTendril>,
Parses given fragment html inserts its contents after to the selected node.
Sourcepub fn set_html<T>(&self, html: T)where
T: Into<StrTendril>,
pub fn set_html<T>(&self, html: T)where
T: Into<StrTendril>,
Parses given fragment html and sets its contents to the selected node.
Sourcepub fn set_text<T>(&self, text: T)where
T: Into<StrTendril>,
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.
Sourcepub fn wrap_node<P: NodeIdProver>(&self, new_parent: P)
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.
Sourcepub fn wrap_html<T>(&self, html: T)where
T: Into<StrTendril>,
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.
Sourcepub fn unwrap_node(&self)
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<'_>
impl NodeRef<'_>
Sourcepub fn next_element_sibling(&self) -> Option<Self>
pub fn next_element_sibling(&self) -> Option<Self>
Returns the next sibling, that is an NodeData::Element of the selected node.
Sourcepub fn prev_element_sibling(&self) -> Option<Self>
pub fn prev_element_sibling(&self) -> Option<Self>
Returns the previous sibling, that is an NodeData::Element of the selected node.
Sourcepub fn first_element_child(&self) -> Option<Self>
pub fn first_element_child(&self) -> Option<Self>
Returns the first child, that is an NodeData::Element of the selected node.
Sourcepub fn element_children(&self) -> Vec<Self>
pub fn element_children(&self) -> Vec<Self>
Returns children, that are NodeData::Elements of the selected node.
Source§impl NodeRef<'_>
impl NodeRef<'_>
Sourcepub fn node_name(&self) -> Option<StrTendril>
pub fn node_name(&self) -> Option<StrTendril>
Returns the name of the selected node if it is an NodeData::Element otherwise None.
Sourcepub fn id_attr(&self) -> Option<StrTendril>
pub fn id_attr(&self) -> Option<StrTendril>
Returns the value of the id attribute
Sourcepub fn class(&self) -> Option<StrTendril>
pub fn class(&self) -> Option<StrTendril>
Returns the value of the class attribute
Sourcepub fn remove_class(&self, class: &str)
pub fn remove_class(&self, class: &str)
Removes a class from the node
Sourcepub fn attr(&self, name: &str) -> Option<StrTendril>
pub fn attr(&self, name: &str) -> Option<StrTendril>
Returns the value of the specified attribute
Sourcepub fn attr_or<T>(&self, name: &str, default: T) -> StrTendril
pub fn attr_or<T>(&self, name: &str, default: T) -> StrTendril
Returns the value of the specified attribute
Sourcepub fn set_attr(&self, name: &str, val: &str)
pub fn set_attr(&self, name: &str, val: &str)
Sets the value of the specified attribute to the node.
Sourcepub fn remove_attr(&self, name: &str)
pub fn remove_attr(&self, name: &str)
Removes the specified attribute from the element.
Sourcepub fn remove_attrs(&self, names: &[&str])
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.
Sourcepub fn retain_attrs(&self, names: &[&str])
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.
Sourcepub fn remove_all_attrs(&self)
pub fn remove_all_attrs(&self)
Removes all attributes from the element.
Sourcepub fn rename(&self, name: &str)
pub fn rename(&self, name: &str)
Renames the node if node is an NodeData::Element.
Source§impl NodeRef<'_>
impl NodeRef<'_>
Sourcepub fn is_document(&self) -> bool
pub fn is_document(&self) -> bool
Returns true if this node is a document.
Sourcepub fn is_fragment(&self) -> bool
pub fn is_fragment(&self) -> bool
Returns true if this node is a fragment.
Sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Returns true if this node is an element.
Sourcepub fn is_comment(&self) -> bool
pub fn is_comment(&self) -> bool
Returns true if this node is a comment.
Sourcepub fn is_doctype(&self) -> bool
pub fn is_doctype(&self) -> bool
Returns true if this node is a DOCTYPE.
Sourcepub fn may_have_children(&self) -> bool
pub fn may_have_children(&self) -> bool
Checks if node may have children nodes.
Source§impl NodeRef<'_>
impl NodeRef<'_>
Sourcepub fn html(&self) -> StrTendril
pub fn html(&self) -> StrTendril
Returns the HTML representation of the DOM tree. Panics if serialization fails.
Sourcepub fn inner_html(&self) -> StrTendril
pub fn inner_html(&self) -> StrTendril
Returns the HTML representation of the DOM tree without the outermost node. Panics if serialization fails.
pub fn try_html(&self) -> Option<StrTendril>
pub fn try_inner_html(&self) -> Option<StrTendril>
Sourcepub fn text(&self) -> StrTendril
pub fn text(&self) -> StrTendril
Returns the text of the node and its descendants.
Sourcepub fn immediate_text(&self) -> StrTendril
pub fn immediate_text(&self) -> StrTendril
Returns the text of the node without its descendants.
Sourcepub fn formatted_text(&self) -> StrTendril
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.
Sourcepub fn has_only_text(&self) -> bool
pub fn has_only_text(&self) -> bool
Checks if the node contains only text node
Sourcepub fn is_empty_element(&self) -> bool
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.
Sourcepub fn normalize(&self)
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.
Sourcepub fn strip_elements(&self, names: &[&str])
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.
Sourcepub fn to_fragment(&self) -> Document
pub fn to_fragment(&self) -> Document
Creates a full copy of the node’s contents as a Document fragment.
Source§impl NodeRef<'_>
impl NodeRef<'_>
Sourcepub fn base_uri(&self) -> Option<StrTendril>
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.
Sourcepub fn find(&self, path: &[&str]) -> Vec<Self>
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.
Sourcepub fn normalized_char_count(&self) -> usize
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>
impl<'a> NodeRef<'a>
Sourcepub fn element_ref(&self) -> Option<Ref<'a, Element>>
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.
Sourcepub fn qual_name_ref(&self) -> Option<Ref<'a, QualName>>
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.
Sourcepub fn has_name(&self, name: &str) -> bool
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.
Sourcepub fn is_nonempty_text(&self) -> bool
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<'_>
impl NodeRef<'_>
Sourcepub fn md(&self, skip_tags: Option<&[&str]>) -> StrTendril
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_tagsisNone, the default list is used:["script", "style", "meta", "head"]. - To process all elements without exclusions, pass
Some(&[]).
Trait Implementations§
Source§impl Element for NodeRef<'_>
impl Element for NodeRef<'_>
Source§fn opaque(&self) -> OpaqueElement
fn opaque(&self) -> OpaqueElement
Converts self into an opaque representation. It can be crucial.
Source§fn parent_node_is_shadow_root(&self) -> bool
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>
fn containing_shadow_host(&self) -> Option<Self>
The host of the containing shadow root, if any.
Source§fn is_pseudo_element(&self) -> bool
fn is_pseudo_element(&self) -> bool
Whether we’re matching on a pseudo-element.
Source§fn prev_sibling_element(&self) -> Option<Self>
fn prev_sibling_element(&self) -> Option<Self>
Skips non-element nodes.
Source§fn next_sibling_element(&self) -> Option<Self>
fn next_sibling_element(&self) -> Option<Self>
Skips non-element nodes.
Source§fn has_namespace(
&self,
ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl,
) -> bool
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
fn is_same_type(&self, other: &Self) -> bool
Whether this element and the other element have the same local name and namespace.
Source§fn is_html_slot_element(&self) -> bool
fn is_html_slot_element(&self) -> bool
Whether the element is an HTML element.
Source§fn imported_part(&self, _name: &CssLocalName) -> Option<CssLocalName>
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_root(&self) -> bool
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>
fn first_element_child(&self) -> Option<Self>
Returns the first element child. Skips non-element nodes.