1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::Select;
use crate::node_data_ref::NodeDataRef;
use crate::select::Selectors;
use crate::tree::ElementData;
#[cfg(feature = "namespaces")]
use super::ElementsInNamespace;
/// Convenience methods for element iterators.
pub trait ElementIterator: Sized + Iterator<Item = NodeDataRef<ElementData>> {
/// Filter this element iterator to elements maching the given selectors.
///
/// # Errors
///
/// Returns `Err(())` if the selector string fails to parse.
#[inline]
fn select(self, selectors: &str) -> Result<Select<Self>, ()> {
Selectors::compile(selectors).map(|s| Select {
iter: self,
selectors: s,
})
}
/// Filter this element iterator to elements in the given namespace.
///
/// **Note:** This method requires the `namespaces` feature to be enabled.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate html5ever;
/// #[cfg(feature = "namespaces")]
/// {
/// # use brik::parse_html;
/// # use brik::traits::*;
/// let html = r#"<!DOCTYPE html>
/// <html>
/// <body>
/// <div>HTML element</div>
/// <svg xmlns="http://www.w3.org/2000/svg">
/// <circle r="10"/>
/// <rect width="20" height="20"/>
/// </svg>
/// </body>
/// </html>
/// "#;
///
/// let doc = parse_html().one(html);
///
/// // Find all SVG elements
/// let svg_elements: Vec<_> = doc
/// .descendants()
/// .elements()
/// .elements_in_ns(ns!(svg))
/// .collect();
///
/// assert_eq!(svg_elements.len(), 3); // svg, circle, rect
/// }
/// ```
#[inline]
#[cfg(feature = "namespaces")]
fn elements_in_ns(self, namespace: html5ever::Namespace) -> ElementsInNamespace<Self> {
ElementsInNamespace {
iter: self,
namespace,
}
}
}
impl<I> ElementIterator for I where I: Iterator<Item = NodeDataRef<ElementData>> {}