style: Add a way to match a single compound selector.

Also improve the ergonomics of matches_complex_selector.

Bug: 1368240
MozReview-Commit-ID: 9DWDvyZmetM
This commit is contained in:
Emilio Cobos Álvarez 2017-06-13 10:58:47 +02:00
parent 262f6adc30
commit 151b636562
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 91 additions and 17 deletions

View file

@ -449,18 +449,36 @@ impl<Impl: SelectorImpl> Selector<Impl> {
}
}
/// Returns an iterator over the entire sequence of simple selectors and combinators,
/// from right to left.
/// Returns the combinator at index `index`, or panics if the component is
/// not a combinator.
pub fn combinator_at(&self, index: usize) -> Combinator {
match self.0.slice[self.0.slice.len() - index] {
Component::Combinator(c) => c,
ref other => {
panic!("Not a combinator: {:?}, {:?}, index: {}",
other, self, index)
}
}
}
/// Returns an iterator over the entire sequence of simple selectors and
/// combinators, from right to left.
pub fn iter_raw(&self) -> Rev<slice::Iter<Component<Impl>>> {
self.iter_raw_rev().rev()
}
/// Returns an iterator over the entire sequence of simple selectors and combinators,
/// from left to right.
/// Returns an iterator over the entire sequence of simple selectors and
/// combinators, from left to right.
pub fn iter_raw_rev(&self) -> slice::Iter<Component<Impl>> {
self.0.slice.iter()
}
/// Returns an iterator over the sequence of simple selectors and
/// combinators after `offset`, from left to right.
pub fn iter_raw_rev_from(&self, offset: usize) -> slice::Iter<Component<Impl>> {
self.0.slice[(self.0.slice.len() - offset)..].iter()
}
/// Creates a Selector from a vec of Components. Used in tests.
pub fn from_vec(vec: Vec<Component<Impl>>, specificity_and_flags: u32) -> Self {
let header = HeaderWithLength::new(SpecificityAndFlags(specificity_and_flags), vec.len());