pub struct Selector { /* private fields */ }
Expand description
Wrapper around CSS selectors.
Represents a “selector group”, i.e. a comma-separated list of selectors.
Implementations§
source§impl Selector
impl Selector
sourcepub fn parse(selectors: &str) -> Result<Self, SelectorErrorKind<'_>>
pub fn parse(selectors: &str) -> Result<Self, SelectorErrorKind<'_>>
Parses a CSS selector group.
Examples found in repository?
examples/document.rs (line 15)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() {
let mut input = String::new();
let mut stdout = io::stdout();
let mut stdin = io::stdin();
write!(stdout, "CSS selector: ").unwrap();
stdout.flush().unwrap();
stdin.read_line(&mut input).unwrap();
let selector = Selector::parse(&input).unwrap();
write!(stdout, "HTML document:\n").unwrap();
stdout.flush().unwrap();
input.clear();
stdin.read_to_string(&mut input).unwrap();
let document = Html::parse_document(&input);
println!("{:#?}", document);
for node in document.select(&selector) {
println!("{:?}", node.value());
}
}
More examples
examples/fragment.rs (line 15)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() {
let mut input = String::new();
let mut stdout = io::stdout();
let mut stdin = io::stdin();
write!(stdout, "CSS selector: ").unwrap();
stdout.flush().unwrap();
stdin.read_line(&mut input).unwrap();
let selector = Selector::parse(&input).unwrap();
write!(stdout, "HTML fragment:\n").unwrap();
stdout.flush().unwrap();
input.clear();
stdin.read_to_string(&mut input).unwrap();
let fragment = Html::parse_fragment(&input);
println!("{:#?}", fragment);
for node in fragment.select(&selector) {
println!("{:?}", node.value());
}
}
sourcepub fn matches(&self, element: &ElementRef<'_>) -> bool
pub fn matches(&self, element: &ElementRef<'_>) -> bool
Returns true if the element matches this selector.
Examples found in repository?
src/html/mod.rs (line 132)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
fn next(&mut self) -> Option<ElementRef<'a>> {
for node in self.inner.by_ref() {
if let Some(element) = ElementRef::wrap(node) {
if element.parent().is_some() && self.selector.matches(&element) {
return Some(element);
}
}
}
None
}
}
impl<'a, 'b> DoubleEndedIterator for Select<'a, 'b> {
fn next_back(&mut self) -> Option<Self::Item> {
for node in self.inner.by_ref().rev() {
if let Some(element) = ElementRef::wrap(node) {
if element.parent().is_some() && self.selector.matches(&element) {
return Some(element);
}
}
}
None
}
sourcepub fn matches_with_scope(
&self,
element: &ElementRef<'_>,
scope: Option<ElementRef<'_>>
) -> bool
pub fn matches_with_scope(
&self,
element: &ElementRef<'_>,
scope: Option<ElementRef<'_>>
) -> bool
Returns true if the element matches this selector.
The optional scope
argument is used to specify which element has :scope
pseudo-class.
When it is None
, :scope
will match the root element.
Examples found in repository?
More examples
src/element_ref/mod.rs (line 103)
99 100 101 102 103 104 105 106 107 108 109 110
fn next(&mut self) -> Option<ElementRef<'a>> {
for edge in &mut self.inner {
if let Edge::Open(node) = edge {
if let Some(element) = ElementRef::wrap(node) {
if self.selector.matches_with_scope(&element, Some(self.scope)) {
return Some(element);
}
}
}
}
None
}