Auto merge of #19817 - emilio:matching-context-visited, r=nox

style: Track the visited-handling-mode on the MatchingContext.

This fixes bugs where we're not passing the value around correctly, like from
::-moz-any.

This is a fix for https://bugzilla.mozilla.org/show_bug.cgi?id=1431539.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19817)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-01-19 12:50:00 -06:00 committed by GitHub
commit 0d7d02fca7
8 changed files with 127 additions and 93 deletions

View file

@ -105,8 +105,6 @@ where
pub bloom_filter: Option<&'a BloomFilter>,
/// An optional cache to speed up nth-index-like selectors.
pub nth_index_cache: Option<&'a mut NthIndexCache>,
/// Input that controls how matching for links is handled.
pub visited_handling: VisitedHandlingMode,
/// The element which is going to match :scope pseudo-class. It can be
/// either one :scope element, or the scoping element.
///
@ -120,11 +118,14 @@ where
/// See https://drafts.csswg.org/selectors-4/#scope-pseudo
pub scope_element: Option<OpaqueElement>,
/// Controls how matching for links is handled.
visited_handling: VisitedHandlingMode,
/// The current nesting level of selectors that we're matching.
///
/// FIXME(emilio): Move this somewhere else and make MatchingContext
/// immutable again.
pub nesting_level: usize,
/// FIXME(emilio): Consider putting the mutable stuff in a Cell, then make
/// MatchingContext immutable again.
nesting_level: usize,
/// An optional hook function for checking whether a pseudo-element
/// should match when matching_mode is ForStatelessPseudoElement.
@ -181,6 +182,12 @@ where
}
}
/// Whether we're matching a nested selector.
#[inline]
pub fn is_nested(&self) -> bool {
self.nesting_level != 0
}
/// The quirks mode of the document.
#[inline]
pub fn quirks_mode(&self) -> QuirksMode {
@ -192,4 +199,38 @@ 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
}
#[inline]
pub fn visited_handling(&self) -> VisitedHandlingMode {
self.visited_handling
}
/// Runs F with a different VisitedHandlingMode.
#[inline]
pub fn with_visited_handling_mode<F, R>(
&mut self,
handling_mode: VisitedHandlingMode,
f: F,
) -> R
where
F: FnOnce(&mut Self) -> R,
{
let original_handling_mode = self.visited_handling;
self.visited_handling = handling_mode;
let result = f(self);
self.visited_handling = original_handling_mode;
result
}
}

View file

@ -60,8 +60,7 @@ impl ElementSelectorFlags {
/// Holds per-compound-selector data.
struct LocalMatchingContext<'a, 'b: 'a, Impl: SelectorImpl> {
shared: &'a mut MatchingContext<'b, Impl>,
matches_hover_and_active_quirk: bool,
visited_handling: VisitedHandlingMode,
matches_hover_and_active_quirk: MatchesHoverAndActiveQuirk,
}
#[inline(always)]
@ -171,6 +170,15 @@ enum SelectorMatchingResult {
NotMatchedGlobally,
}
/// Whether the :hover and :active quirk applies.
///
/// https://quirks.spec.whatwg.org/#the-active-and-hover-quirk
#[derive(Clone, Copy, Debug, PartialEq)]
enum MatchesHoverAndActiveQuirk {
Yes,
No,
}
/// Matches a selector, fast-rejecting against a bloom filter.
///
/// We accept an offset to allow consumers to represent and match against
@ -236,11 +244,9 @@ where
selector.combinator_at_parse_order(from_offset - 1); // This asserts.
}
let visited_handling = context.visited_handling;
let mut local_context = LocalMatchingContext {
shared: context,
visited_handling,
matches_hover_and_active_quirk: false,
matches_hover_and_active_quirk: MatchesHoverAndActiveQuirk::No,
};
for component in selector.iter_raw_parse_order_from(from_offset) {
@ -280,7 +286,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.is_nested() {
// Consume the pseudo.
match *iter.next().unwrap() {
Component::PseudoElement(ref pseudo) => {
@ -312,12 +318,10 @@ where
}
}
let visited_handling = context.visited_handling;
let result = matches_complex_selector_internal(
iter,
element,
context,
visited_handling,
flags_setter,
Rightmost::Yes,
);
@ -333,23 +337,23 @@ fn matches_hover_and_active_quirk<Impl: SelectorImpl>(
selector_iter: &SelectorIter<Impl>,
context: &MatchingContext<Impl>,
rightmost: Rightmost,
) -> bool {
) -> MatchesHoverAndActiveQuirk {
if context.quirks_mode() != QuirksMode::Quirks {
return false;
return MatchesHoverAndActiveQuirk::No;
}
if context.nesting_level != 0 {
return false;
if context.is_nested() {
return MatchesHoverAndActiveQuirk::No;
}
// This compound selector had a pseudo-element to the right that we
// intentionally skipped.
if matches!(rightmost, Rightmost::Yes) &&
if rightmost == Rightmost::Yes &&
context.matching_mode == MatchingMode::ForStatelessPseudoElement {
return false;
return MatchesHoverAndActiveQuirk::No;
}
selector_iter.clone().all(|simple| {
let all_match = selector_iter.clone().all(|simple| {
match *simple {
Component::LocalName(_) |
Component::AttributeInNoNamespaceExists { .. } |
@ -375,9 +379,16 @@ fn matches_hover_and_active_quirk<Impl: SelectorImpl>(
},
_ => true,
}
})
});
if all_match {
MatchesHoverAndActiveQuirk::Yes
} else {
MatchesHoverAndActiveQuirk::No
}
}
#[derive(Clone, Copy, PartialEq)]
enum Rightmost {
Yes,
No,
@ -417,7 +428,6 @@ fn matches_complex_selector_internal<E, F>(
mut selector_iter: SelectorIter<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
visited_handling: VisitedHandlingMode,
flags_setter: &mut F,
rightmost: Rightmost,
) -> SelectorMatchingResult
@ -431,7 +441,6 @@ where
&mut selector_iter,
element,
context,
visited_handling,
flags_setter,
rightmost
);
@ -467,12 +476,11 @@ where
// Stop matching :visited as soon as we find a link, or a combinator for
// something that isn't an ancestor.
let mut visited_handling =
if element.is_link() || combinator.is_sibling() {
VisitedHandlingMode::AllLinksUnvisited
} else {
visited_handling
};
let mut visited_handling = if element.is_link() || combinator.is_sibling() {
VisitedHandlingMode::AllLinksUnvisited
} else {
context.visited_handling()
};
loop {
let element = match next_element {
@ -480,14 +488,16 @@ where
Some(next_element) => next_element,
};
let result = matches_complex_selector_internal(
selector_iter.clone(),
&element,
context,
visited_handling,
flags_setter,
Rightmost::No,
);
let result =
context.with_visited_handling_mode(visited_handling, |context| {
matches_complex_selector_internal(
selector_iter.clone(),
&element,
context,
flags_setter,
Rightmost::No,
)
});
match (result, combinator) {
// Return the status immediately.
@ -521,12 +531,9 @@ where
_ => {},
}
visited_handling =
if element.is_link() || combinator.is_sibling() {
VisitedHandlingMode::AllLinksUnvisited
} else {
visited_handling
};
if element.is_link() || combinator.is_sibling() {
visited_handling = VisitedHandlingMode::AllLinksUnvisited;
}
next_element = next_element_for_combinator(&element, combinator);
}
@ -554,7 +561,6 @@ fn matches_compound_selector<E, F>(
selector_iter: &mut SelectorIter<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
visited_handling: VisitedHandlingMode,
flags_setter: &mut F,
rightmost: Rightmost,
) -> bool
@ -596,7 +602,6 @@ where
let mut local_context =
LocalMatchingContext {
shared: context,
visited_handling,
matches_hover_and_active_quirk,
};
iter::once(selector).chain(selector_iter).all(|simple| {
@ -623,17 +628,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)
@ -714,17 +717,17 @@ where
)
}
Component::NonTSPseudoClass(ref pc) => {
if context.matches_hover_and_active_quirk &&
context.shared.nesting_level == 0 &&
if context.matches_hover_and_active_quirk == MatchesHoverAndActiveQuirk::Yes &&
!context.shared.is_nested() &&
E::Impl::is_active_or_hover(pc) &&
!element.is_link() {
!element.is_link()
{
return false;
}
element.match_non_ts_pseudo_class(
pc,
&mut context.shared,
context.visited_handling,
flags_setter
)
}
@ -774,17 +777,20 @@ 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
context.shared.nest(|context| {
let mut local_context = LocalMatchingContext {
matches_hover_and_active_quirk: MatchesHoverAndActiveQuirk::No,
shared: context,
};
!negated.iter().all(|ss| {
matches_simple_selector(
ss,
element,
&mut local_context,
flags_setter,
)
})
})
}
}
}

View file

@ -6,7 +6,6 @@
//! between layout and style.
use attr::{AttrSelectorOperation, NamespaceConstraint, CaseSensitivity};
use context::VisitedHandlingMode;
use matching::{ElementSelectorFlags, MatchingContext};
use parser::SelectorImpl;
use servo_arc::NonZeroPtrMut;
@ -69,7 +68,6 @@ pub trait Element: Sized + Clone + Debug {
&self,
pc: &<Self::Impl as SelectorImpl>::NonTSPseudoClass,
context: &mut MatchingContext<Self::Impl>,
visited_handling: VisitedHandlingMode,
flags_setter: &mut F,
) -> bool
where