Skip to content

Commit

Permalink
Add a method that obtains the document root.
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Oct 16, 2023
1 parent 2f6fb28 commit f0ab225
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ impl Element {
self.parent(doc).map_or(false, |p| p.is_container())
}

/// Returns the "top" parent of this element. If the element is attached, the "top" parent
/// is the document root. Otherwise, the "top" parent is the root of the detached sub-tree.
pub fn top_parent(&self, doc: &Document) -> Element {
let mut e = *self;
while let Some(parent) = e.parent(doc) {
if parent.is_container() {
return e;
}
e = parent;
}
e
}

/// Get full name of element, including its namespace prefix.
/// Use [`Element::name()`] to get its name without the prefix.
pub fn full_name<'a>(&self, doc: &'a Document) -> &'a str {
Expand Down

0 comments on commit f0ab225

Please sign in to comment.