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>
impl<'a, 'b> TreeRef<'a, 'b>
pub fn id(&self) -> TreeRefId
Sourcepub fn spanned_data(&self) -> &ParsedNode<'a>
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).
Sourcepub fn closest(&self, selector: &str) -> Option<Self>
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.
Sourcepub fn prev_sibling(&self) -> Option<Self>
pub fn prev_sibling(&self) -> Option<Self>
Get a reference to the previous sibling of this node.
Sourcepub fn next_sibling(&self) -> Option<Self>
pub fn next_sibling(&self) -> Option<Self>
Get a reference to the next sibling of this node.
Sourcepub fn children<'c>(&'c self) -> impl Iterator<Item = TreeRef<'a, 'b>> + 'c
pub fn children<'c>(&'c self) -> impl Iterator<Item = TreeRef<'a, 'b>> + 'c
Get a list of references of the children of this node.
Sourcepub fn children_id(&self) -> Vec<TreeRefId>
pub fn children_id(&self) -> Vec<TreeRefId>
Get a cloned list of IDs of this node children.
Sourcepub fn select<'c>(
&self,
selector: &'c str,
) -> Result<Option<TreeRef<'_, '_>>, SelectorParseError<'c>>
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
Sourcepub fn select_all(&self, selector: &str) -> Vec<TreeRef<'_, '_>>
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.
Sourcepub fn name(&self) -> Result<Cow<'a, str>>
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
.
Sourcepub fn text(&self) -> Cow<'a, str>
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 %}
).
Sourcepub fn attr(&self, name: &str) -> Result<Option<String>>
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.
Sourcepub fn attr_spanned(
&self,
name: &str,
) -> Result<Option<Vec<Spanned<TemplateBlock<'_>>>>>
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.
Sourcepub fn attrs(&self) -> Result<impl Iterator<Item = (&Cow<'_, str>, String)>>
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
.
Sourcepub fn attrs_spanned(&self) -> Result<impl Iterator<Item = &Attr<'_>>>
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
.
pub fn sub_tree(&self) -> Tree<'a>
pub fn traverse_breadth(&self) -> impl Iterator<Item = TreeRef<'a, '_>>
pub fn traverse_depth(&self) -> impl Iterator<Item = TreeRef<'a, '_>>
pub fn is_matching(&self, selector_list: &SelectorList<InnerSelector>) -> bool
Trait Implementations§
Source§impl Element for TreeRef<'_, '_>
impl Element for TreeRef<'_, '_>
type Impl = InnerSelector
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
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, local_name: &<Self::Impl as SelectorImpl>::BorrowedLocalName, ) -> bool
Source§fn has_namespace(
&self,
_ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl,
) -> bool
fn has_namespace( &self, _ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl, ) -> 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.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
fn match_non_ts_pseudo_class( &self, _pc: &<Self::Impl as SelectorImpl>::NonTSPseudoClass, _context: &mut MatchingContext<'_, Self::Impl>, ) -> bool
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)
fn apply_selector_flags(&self, _flags: ElementSelectorFlags)
Source§fn is_html_slot_element(&self) -> bool
fn is_html_slot_element(&self) -> bool
fn has_id( &self, id: &<Self::Impl as SelectorImpl>::Identifier, case_sensitivity: CaseSensitivity, ) -> bool
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>
fn imported_part( &self, _name: &<Self::Impl as SelectorImpl>::Identifier, ) -> Option<<Self::Impl as SelectorImpl>::Identifier>
exportparts
attribute in the reverse
direction, that is, in an outer-tree -> inner-tree direction.fn is_part(&self, _name: &<Self::Impl as SelectorImpl>::Identifier) -> 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