Skip to content

Commit

Permalink
Merge branch 'master' into selectable-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
cfvescovo authored Nov 9, 2023
2 parents 0996a3f + 509bcb3 commit be0d59c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ html5ever = "0.26"
selectors = "0.25.0"
tendril = "0.4.3"
ahash = "0.8"
indexmap = { version = "2.0.2", optional = true }
indexmap = { version = "2.1.0", optional = true }
once_cell = "1.0"

[dependencies.getopts]
Expand Down
30 changes: 30 additions & 0 deletions src/element_ref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ impl<'a> ElementRef<'a> {
inner: self.traverse(),
}
}

/// Iterate over all child nodes which are elements
///
/// # Example
///
/// ```
/// # use scraper::Html;
/// let fragment = Html::parse_fragment("foo<span>bar</span><a>baz</a>qux");
///
/// let children = fragment.root_element().child_elements().map(|element| element.value().name()).collect::<Vec<_>>();
/// assert_eq!(children, ["span", "a"]);
/// ```
pub fn child_elements(&self) -> impl Iterator<Item = ElementRef<'a>> {
self.children().filter_map(ElementRef::wrap)
}

/// Iterate over all descendent nodes which are elements
///
/// # Example
///
/// ```
/// # use scraper::Html;
/// let fragment = Html::parse_fragment("foo<span><b>bar</b></span><a><i>baz</i></a>qux");
///
/// let descendants = fragment.root_element().descendent_elements().map(|element| element.value().name()).collect::<Vec<_>>();
/// assert_eq!(descendants, ["html", "span", "b", "a", "i"]);
/// ```
pub fn descendent_elements(&self) -> impl Iterator<Item = ElementRef<'a>> {
self.descendants().filter_map(ElementRef::wrap)
}
}

impl<'a> Deref for ElementRef<'a> {
Expand Down

0 comments on commit be0d59c

Please sign in to comment.