style: Use left-to-right indices in the invalidator.

This will make easier to create external invalidations, and also makes reasoning
about the invalidator a bit easier.
This commit is contained in:
Emilio Cobos Álvarez 2017-10-15 00:50:28 +02:00
parent 086c48210c
commit f1cc225e97
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 46 additions and 39 deletions

View file

@ -299,11 +299,10 @@ where
/// Whether a compound selector matched, and whether it was the rightmost
/// selector inside the complex selector.
pub enum CompoundSelectorMatchingResult {
/// The selector was fully matched.
FullyMatched,
/// The compound selector matched, and the next combinator offset is
/// `next_combinator_offset`.
///
/// If the next combinator offset is zero, it means that it's the rightmost
/// selector.
Matched { next_combinator_offset: usize, },
/// The selector didn't match.
NotMatched,
@ -325,8 +324,9 @@ pub fn matches_compound_selector<E>(
where
E: Element
{
debug_assert_ne!(from_offset, 0);
if cfg!(debug_assertions) {
selector.combinator_at(from_offset); // This asserts.
selector.combinator_at_parse_order(from_offset - 1); // This asserts.
}
let mut local_context = LocalMatchingContext {
@ -334,10 +334,11 @@ where
matches_hover_and_active_quirk: false,
};
for component in selector.iter_raw_parse_order_from(from_offset - 1) {
for component in selector.iter_raw_parse_order_from(from_offset) {
if matches!(*component, Component::Combinator(..)) {
debug_assert_ne!(from_offset, 0, "Selector started with a combinator?");
return CompoundSelectorMatchingResult::Matched {
next_combinator_offset: from_offset - 1,
next_combinator_offset: from_offset,
}
}
@ -350,12 +351,10 @@ where
return CompoundSelectorMatchingResult::NotMatched;
}
from_offset -= 1;
from_offset += 1;
}
return CompoundSelectorMatchingResult::Matched {
next_combinator_offset: 0,
}
CompoundSelectorMatchingResult::FullyMatched
}
/// Matches a complex selector.

View file

@ -440,12 +440,11 @@ impl<Impl: SelectorImpl> Selector<Impl> {
}
}
/// Returns the combinator at index `index` (one-indexed from the right),
/// Returns the combinator at index `index` (zero-indexed from the right),
/// or panics if the component is not a combinator.
///
/// FIXME(bholley): Use more intuitive indexing.
pub fn combinator_at(&self, index: usize) -> Combinator {
match self.0.slice[index - 1] {
#[inline]
pub fn combinator_at_match_order(&self, index: usize) -> Combinator {
match self.0.slice[index] {
Component::Combinator(c) => c,
ref other => {
panic!("Not a combinator: {:?}, {:?}, index: {}",
@ -460,16 +459,24 @@ impl<Impl: SelectorImpl> Selector<Impl> {
self.0.slice.iter()
}
/// Returns the combinator at index `index` (zero-indexed from the left),
/// or panics if the component is not a combinator.
#[inline]
pub fn combinator_at_parse_order(&self, index: usize) -> Combinator {
match self.0.slice[self.len() - index - 1] {
Component::Combinator(c) => c,
ref other => {
panic!("Not a combinator: {:?}, {:?}, index: {}",
other, self, index)
}
}
}
/// Returns an iterator over the sequence of simple selectors and
/// combinators, in parse order (from left to right), _starting_
/// 'offset_from_right' entries from the past-the-end sentinel on
/// the right. So "0" panics,. "1" iterates nothing, and "len"
/// iterates the entire sequence.
///
/// FIXME(bholley): This API is rather unintuive, and should really
/// be changed to accept an offset from the left. Same for combinator_at.
pub fn iter_raw_parse_order_from(&self, offset_from_right: usize) -> Rev<slice::Iter<Component<Impl>>> {
self.0.slice[..offset_from_right].iter().rev()
/// combinators, in parse order (from left to right), starting from
/// `offset`.
pub fn iter_raw_parse_order_from(&self, offset: usize) -> Rev<slice::Iter<Component<Impl>>> {
self.0.slice[..self.len() - offset].iter().rev()
}
/// Creates a Selector from a vec of Components, specified in parse order. Used in tests.