style: Hide LocalMatchingContext.

This type is a lot of complexity related to a very specific thing such as the
hover and active quirk.

Instead of that, move `nesting_level` to `MatchingContext`, and simplify all
this computing whether the quirk applies upfront, for each complex selector we
test.

This is less error-prone, and also allows simplifying more stuff in a bit.
This commit is contained in:
Emilio Cobos Álvarez 2017-10-14 13:13:26 +02:00
parent 48c85151dc
commit 11485edc9e
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
7 changed files with 175 additions and 183 deletions

View file

@ -54,109 +54,10 @@ impl ElementSelectorFlags {
}
}
/// Holds per-selector data alongside a pointer to MatchingContext.
pub struct LocalMatchingContext<'a, 'b: 'a, Impl: SelectorImpl> {
/// Shared `MatchingContext`.
pub shared: &'a mut MatchingContext<'b>,
/// A reference to the base selector we're matching against.
pub selector: &'a Selector<Impl>,
/// The offset of the current compound selector being matched, kept up to
/// date by the callees when the iterator is advanced. This, in conjunction
/// with the selector reference above, allows callees to synthesize an
/// iterator for the current compound selector on-demand. This is necessary
/// because the primary iterator may already have been advanced partway
/// through the current compound selector, and the callee may need the whole
/// thing.
offset: usize,
/// The level of nesting for the selector being matched.
pub nesting_level: usize,
/// Holds a bool flag to see whether :active and :hover quirk should try to
/// match or not. This flag can only be true in the case PseudoElements are
/// encountered when matching mode is ForStatelessPseudoElement.
pub hover_active_quirk_disabled: bool,
}
impl<'a, 'b, Impl> LocalMatchingContext<'a, 'b, Impl>
where Impl: SelectorImpl
{
/// Constructs a new `LocalMatchingContext`.
pub fn new(shared: &'a mut MatchingContext<'b>,
selector: &'a Selector<Impl>) -> Self {
Self {
shared: shared,
selector: selector,
offset: 0,
nesting_level: 0,
// We flip this off once third sequence is reached.
hover_active_quirk_disabled: selector.has_pseudo_element(),
}
}
/// Updates offset of Selector to show new compound selector.
/// To be able to correctly re-synthesize main SelectorIter.
fn note_position(&mut self, selector_iter: &SelectorIter<Impl>) {
if let QuirksMode::Quirks = self.shared.quirks_mode() {
if self.selector.has_pseudo_element() && self.offset != 0 {
// This is the _second_ call to note_position,
// which means we've moved past the compound
// selector adjacent to the pseudo-element.
self.hover_active_quirk_disabled = false;
}
self.offset = self.selector.len() - selector_iter.selector_length();
}
}
/// Returns true if current compound selector matches :active and :hover quirk.
/// https://quirks.spec.whatwg.org/#the-active-and-hover-quirk
pub fn active_hover_quirk_matches(&self) -> bool {
if self.shared.quirks_mode() != QuirksMode::Quirks {
return false;
}
// Don't allow it in recursive selectors such as :not and :-moz-any.
if self.nesting_level != 0 {
return false;
}
if self.hover_active_quirk_disabled {
return false;
}
let mut iter = if self.offset == 0 {
self.selector.iter()
} else {
self.selector.iter_from(self.offset)
};
return iter.all(|simple| {
match *simple {
Component::LocalName(_) |
Component::AttributeInNoNamespaceExists { .. } |
Component::AttributeInNoNamespace { .. } |
Component::AttributeOther(_) |
Component::ID(_) |
Component::Class(_) |
Component::PseudoElement(_) |
Component::Negation(_) |
Component::FirstChild |
Component::LastChild |
Component::OnlyChild |
Component::Empty |
Component::NthChild(_, _) |
Component::NthLastChild(_, _) |
Component::NthOfType(_, _) |
Component::NthLastOfType(_, _) |
Component::FirstOfType |
Component::LastOfType |
Component::OnlyOfType => false,
Component::NonTSPseudoClass(ref pseudo_class) => {
Impl::is_active_or_hover(pseudo_class)
},
_ => true,
}
});
}
/// Holds per-compound-selector data.
struct LocalMatchingContext<'a, 'b: 'a> {
shared: &'a mut MatchingContext<'b>,
matches_hover_and_active_quirk: bool,
}
pub fn matches_selector_list<E>(
@ -392,13 +293,7 @@ where
}
}
let mut local_context = LocalMatchingContext::new(context, selector);
let iter = if offset == 0 {
selector.iter()
} else {
selector.iter_from(offset)
};
matches_complex_selector(iter, element, &mut local_context, flags_setter)
matches_complex_selector(selector.iter_from(offset), element, context, flags_setter)
}
/// Whether a compound selector matched, and whether it was the rightmost
@ -434,7 +329,11 @@ where
selector.combinator_at(from_offset); // This asserts.
}
let mut local_context = LocalMatchingContext::new(context, selector);
let mut local_context = LocalMatchingContext {
shared: context,
matches_hover_and_active_quirk: false,
};
for component in selector.iter_raw_parse_order_from(from_offset - 1) {
if matches!(*component, Component::Combinator(..)) {
return CompoundSelectorMatchingResult::Matched {
@ -463,7 +362,7 @@ where
pub fn matches_complex_selector<E, F>(
mut iter: SelectorIter<E::Impl>,
element: &E,
context: &mut LocalMatchingContext<E::Impl>,
context: &mut MatchingContext,
flags_setter: &mut F,
) -> bool
where
@ -473,7 +372,7 @@ where
// If this is the special pseudo-element mode, consume the ::pseudo-element
// before proceeding, since the caller has already handled that part.
if context.nesting_level == 0 &&
context.shared.matching_mode == MatchingMode::ForStatelessPseudoElement {
context.matching_mode == MatchingMode::ForStatelessPseudoElement {
// Consume the pseudo.
let pseudo = iter.next().unwrap();
debug_assert!(matches!(*pseudo, Component::PseudoElement(..)),
@ -487,12 +386,11 @@ where
return false;
}
// Advance to the non-pseudo-element part of the selector, and inform
// the context.
// Advance to the non-pseudo-element part of the selector, but let the
// context note that .
if iter.next_sequence().is_none() {
return true;
}
context.note_position(&iter);
}
let result = matches_complex_selector_internal(
@ -501,6 +399,7 @@ where
context,
&mut RelevantLinkStatus::Looking,
flags_setter,
Rightmost::Yes,
);
match result {
@ -509,25 +408,97 @@ where
}
}
#[inline]
fn matches_hover_and_active_quirk<Impl: SelectorImpl>(
selector_iter: &SelectorIter<Impl>,
context: &MatchingContext,
rightmost: Rightmost,
) -> bool {
if context.quirks_mode() != QuirksMode::Quirks {
return false;
}
if context.nesting_level != 0 {
return false;
}
// This compound selector had a pseudo-element to the right that we
// intentionally skipped.
if matches!(rightmost, Rightmost::Yes) &&
context.matching_mode == MatchingMode::ForStatelessPseudoElement {
return false;
}
selector_iter.clone().all(|simple| {
match *simple {
Component::LocalName(_) |
Component::AttributeInNoNamespaceExists { .. } |
Component::AttributeInNoNamespace { .. } |
Component::AttributeOther(_) |
Component::ID(_) |
Component::Class(_) |
Component::PseudoElement(_) |
Component::Negation(_) |
Component::FirstChild |
Component::LastChild |
Component::OnlyChild |
Component::Empty |
Component::NthChild(_, _) |
Component::NthLastChild(_, _) |
Component::NthOfType(_, _) |
Component::NthLastOfType(_, _) |
Component::FirstOfType |
Component::LastOfType |
Component::OnlyOfType => false,
Component::NonTSPseudoClass(ref pseudo_class) => {
Impl::is_active_or_hover(pseudo_class)
},
_ => true,
}
})
}
enum Rightmost {
Yes,
No,
}
fn matches_complex_selector_internal<E, F>(
mut selector_iter: SelectorIter<E::Impl>,
element: &E,
context: &mut LocalMatchingContext<E::Impl>,
context: &mut MatchingContext,
relevant_link: &mut RelevantLinkStatus,
flags_setter: &mut F
flags_setter: &mut F,
rightmost: Rightmost,
) -> SelectorMatchingResult
where
E: Element,
F: FnMut(&E, ElementSelectorFlags),
{
*relevant_link = relevant_link.examine_potential_link(element, &mut context.shared);
*relevant_link = relevant_link.examine_potential_link(element, context);
debug!("Matching complex selector {:?} for {:?}, relevant link {:?}",
selector_iter, element, relevant_link);
let matches_all_simple_selectors = selector_iter.all(|simple| {
matches_simple_selector(simple, element, context, &relevant_link, flags_setter)
});
let matches_all_simple_selectors = {
let matches_hover_and_active_quirk =
matches_hover_and_active_quirk(&selector_iter, context, rightmost);
let mut local_context =
LocalMatchingContext {
shared: context,
matches_hover_and_active_quirk,
};
selector_iter.all(|simple| {
matches_simple_selector(
simple,
element,
&mut local_context,
&relevant_link,
flags_setter,
)
})
};
let combinator = selector_iter.next_sequence();
let siblings = combinator.map_or(false, |c| c.is_sibling());
@ -569,13 +540,14 @@ where
None => return candidate_not_found,
Some(next_element) => next_element,
};
// Note in which compound selector are we currently.
context.note_position(&selector_iter);
let result = matches_complex_selector_internal(selector_iter.clone(),
&element,
context,
relevant_link,
flags_setter);
let result = matches_complex_selector_internal(
selector_iter.clone(),
&element,
context,
relevant_link,
flags_setter,
Rightmost::No,
);
match (result, c) {
// Return the status immediately.
(SelectorMatchingResult::Matched, _) => return result,
@ -618,7 +590,7 @@ where
fn matches_simple_selector<E, F>(
selector: &Component<E::Impl>,
element: &E,
context: &mut LocalMatchingContext<E::Impl>,
context: &mut LocalMatchingContext,
relevant_link: &RelevantLinkStatus,
flags_setter: &mut F,
) -> bool
@ -708,7 +680,14 @@ where
)
}
Component::NonTSPseudoClass(ref pc) => {
element.match_non_ts_pseudo_class(pc, context, relevant_link, flags_setter)
if context.matches_hover_and_active_quirk &&
context.shared.nesting_level == 0 &&
E::Impl::is_active_or_hover(pc) &&
!element.is_link() {
return false;
}
element.match_non_ts_pseudo_class(pc, &mut context.shared, relevant_link, flags_setter)
}
Component::FirstChild => {
matches_first_child(element, flags_setter)
@ -756,12 +735,17 @@ where
matches_generic_nth_child(element, context, 0, 1, true, true, flags_setter)
}
Component::Negation(ref negated) => {
context.nesting_level += 1;
context.shared.nesting_level += 1;
let result = !negated.iter().all(|ss| {
matches_simple_selector(ss, element, context,
relevant_link, flags_setter)
matches_simple_selector(
ss,
element,
context,
relevant_link,
flags_setter,
)
});
context.nesting_level -= 1;
context.shared.nesting_level -= 1;
result
}
}
@ -778,7 +762,7 @@ fn select_name<'a, T>(is_html: bool, local_name: &'a T, local_name_lower: &'a T)
#[inline]
fn matches_generic_nth_child<E, F>(
element: &E,
context: &mut LocalMatchingContext<E::Impl>,
context: &mut LocalMatchingContext,
a: i32,
b: i32,
is_of_type: bool,