mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
selectors: Add a MatchingContext::nest function, make nesting_level private.
This commit is contained in:
parent
88d2982e23
commit
e4f08ee2bb
4 changed files with 52 additions and 33 deletions
|
@ -122,9 +122,9 @@ where
|
|||
|
||||
/// The current nesting level of selectors that we're matching.
|
||||
///
|
||||
/// FIXME(emilio): Move this somewhere else and make MatchingContext
|
||||
/// FIXME(emilio): Consider putting the mutable stuff in a Cell.
|
||||
/// immutable again.
|
||||
pub nesting_level: usize,
|
||||
nesting_level: usize,
|
||||
|
||||
/// An optional hook function for checking whether a pseudo-element
|
||||
/// should match when matching_mode is ForStatelessPseudoElement.
|
||||
|
@ -181,6 +181,12 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// How many times deep are we in a selector.
|
||||
#[inline]
|
||||
pub fn nesting_level(&self) -> usize {
|
||||
self.nesting_level
|
||||
}
|
||||
|
||||
/// The quirks mode of the document.
|
||||
#[inline]
|
||||
pub fn quirks_mode(&self) -> QuirksMode {
|
||||
|
@ -192,4 +198,16 @@ where
|
|||
pub fn classes_and_ids_case_sensitivity(&self) -> CaseSensitivity {
|
||||
self.classes_and_ids_case_sensitivity
|
||||
}
|
||||
|
||||
/// Runs F with a deeper nesting level.
|
||||
#[inline]
|
||||
pub fn nest<F, R>(&mut self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut Self) -> R,
|
||||
{
|
||||
self.nesting_level += 1;
|
||||
let result = f(self);
|
||||
self.nesting_level -= 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -289,7 +289,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.matching_mode == MatchingMode::ForStatelessPseudoElement &&
|
||||
context.nesting_level == 0 {
|
||||
context.nesting_level() == 0 {
|
||||
// Consume the pseudo.
|
||||
match *iter.next().unwrap() {
|
||||
Component::PseudoElement(ref pseudo) => {
|
||||
|
@ -347,7 +347,7 @@ fn matches_hover_and_active_quirk<Impl: SelectorImpl>(
|
|||
return MatchesHoverAndActiveQuirk::No;
|
||||
}
|
||||
|
||||
if context.nesting_level != 0 {
|
||||
if context.nesting_level() != 0 {
|
||||
return MatchesHoverAndActiveQuirk::No;
|
||||
}
|
||||
|
||||
|
@ -639,17 +639,15 @@ where
|
|||
match *selector {
|
||||
Component::Combinator(_) => unreachable!(),
|
||||
Component::Slotted(ref selector) => {
|
||||
context.shared.nesting_level += 1;
|
||||
let result =
|
||||
context.shared.nest(|context| {
|
||||
element.assigned_slot().is_some() &&
|
||||
matches_complex_selector(
|
||||
selector.iter(),
|
||||
element,
|
||||
context.shared,
|
||||
context,
|
||||
flags_setter,
|
||||
);
|
||||
context.shared.nesting_level -= 1;
|
||||
result
|
||||
)
|
||||
})
|
||||
}
|
||||
Component::PseudoElement(ref pseudo) => {
|
||||
element.match_pseudo_element(pseudo, context.shared)
|
||||
|
@ -731,7 +729,7 @@ where
|
|||
}
|
||||
Component::NonTSPseudoClass(ref pc) => {
|
||||
if context.matches_hover_and_active_quirk == MatchesHoverAndActiveQuirk::Yes &&
|
||||
context.shared.nesting_level == 0 &&
|
||||
context.shared.nesting_level() == 0 &&
|
||||
E::Impl::is_active_or_hover(pc) &&
|
||||
!element.is_link() {
|
||||
return false;
|
||||
|
@ -790,17 +788,22 @@ where
|
|||
matches_generic_nth_child(element, context, 0, 1, true, true, flags_setter)
|
||||
}
|
||||
Component::Negation(ref negated) => {
|
||||
context.shared.nesting_level += 1;
|
||||
let result = !negated.iter().all(|ss| {
|
||||
matches_simple_selector(
|
||||
ss,
|
||||
element,
|
||||
context,
|
||||
flags_setter,
|
||||
)
|
||||
});
|
||||
context.shared.nesting_level -= 1;
|
||||
result
|
||||
let visited_handling = context.visited_handling;
|
||||
context.shared.nest(|context| {
|
||||
let mut local_context = LocalMatchingContext {
|
||||
visited_handling,
|
||||
matches_hover_and_active_quirk: MatchesHoverAndActiveQuirk::No,
|
||||
shared: context,
|
||||
};
|
||||
!negated.iter().all(|ss| {
|
||||
matches_simple_selector(
|
||||
ss,
|
||||
element,
|
||||
&mut local_context,
|
||||
flags_setter,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue