Introduce an NthIndexCache type and pipe it from ThreadLocalStyleContext to MatchingContext.

Some future refactoring here to pass fewer things as parameters would be nice.
This commit is contained in:
Bobby Holley 2017-09-20 12:43:29 -07:00
parent 05c03d5104
commit 48466bf876
11 changed files with 83 additions and 24 deletions

View file

@ -69,15 +69,22 @@ impl QuirksMode {
}
}
/// A cache to speed up matching of nth-index-like selectors.
///
/// NB: This is just a dummy type right now, it will be fleshed out in later patches.
#[derive(Default)]
pub struct NthIndexCache(usize);
/// Data associated with the matching process for a element. This context is
/// used across many selectors for an element, so it's not appropriate for
/// transient data that applies to only a single selector.
#[derive(Clone)]
pub struct MatchingContext<'a> {
/// Input with the matching mode we should use when matching selectors.
pub matching_mode: MatchingMode,
/// Input with the bloom filter used to fast-reject selectors.
pub bloom_filter: Option<&'a BloomFilter>,
/// An optional cache to speed up nth-index-like selectors.
nth_index_cache: Option<&'a mut NthIndexCache>,
/// Input that controls how matching for links is handled.
pub visited_handling: VisitedHandlingMode,
/// Output that records whether we encountered a "relevant link" while
@ -94,12 +101,14 @@ impl<'a> MatchingContext<'a> {
/// Constructs a new `MatchingContext`.
pub fn new(matching_mode: MatchingMode,
bloom_filter: Option<&'a BloomFilter>,
nth_index_cache: Option<&'a mut NthIndexCache>,
quirks_mode: QuirksMode)
-> Self
{
Self {
matching_mode: matching_mode,
bloom_filter: bloom_filter,
nth_index_cache: nth_index_cache,
visited_handling: VisitedHandlingMode::AllLinksUnvisited,
relevant_link_found: false,
quirks_mode: quirks_mode,
@ -110,6 +119,7 @@ impl<'a> MatchingContext<'a> {
/// Constructs a new `MatchingContext` for use in visited matching.
pub fn new_for_visited(matching_mode: MatchingMode,
bloom_filter: Option<&'a BloomFilter>,
nth_index_cache: Option<&'a mut NthIndexCache>,
visited_handling: VisitedHandlingMode,
quirks_mode: QuirksMode)
-> Self
@ -119,6 +129,7 @@ impl<'a> MatchingContext<'a> {
bloom_filter: bloom_filter,
visited_handling: visited_handling,
relevant_link_found: false,
nth_index_cache: nth_index_cache,
quirks_mode: quirks_mode,
classes_and_ids_case_sensitivity: quirks_mode.classes_and_ids_case_sensitivity(),
}

View file

@ -724,26 +724,26 @@ fn matches_simple_selector<E, F>(
element.is_empty()
}
Component::NthChild(a, b) => {
matches_generic_nth_child(element, a, b, false, false, flags_setter)
matches_generic_nth_child(element, context, a, b, false, false, flags_setter)
}
Component::NthLastChild(a, b) => {
matches_generic_nth_child(element, a, b, false, true, flags_setter)
matches_generic_nth_child(element, context, a, b, false, true, flags_setter)
}
Component::NthOfType(a, b) => {
matches_generic_nth_child(element, a, b, true, false, flags_setter)
matches_generic_nth_child(element, context, a, b, true, false, flags_setter)
}
Component::NthLastOfType(a, b) => {
matches_generic_nth_child(element, a, b, true, true, flags_setter)
matches_generic_nth_child(element, context, a, b, true, true, flags_setter)
}
Component::FirstOfType => {
matches_generic_nth_child(element, 0, 1, true, false, flags_setter)
matches_generic_nth_child(element, context, 0, 1, true, false, flags_setter)
}
Component::LastOfType => {
matches_generic_nth_child(element, 0, 1, true, true, flags_setter)
matches_generic_nth_child(element, context, 0, 1, true, true, flags_setter)
}
Component::OnlyOfType => {
matches_generic_nth_child(element, 0, 1, true, false, flags_setter) &&
matches_generic_nth_child(element, 0, 1, true, true, flags_setter)
matches_generic_nth_child(element, context, 0, 1, true, false, flags_setter) &&
matches_generic_nth_child(element, context, 0, 1, true, true, flags_setter)
}
Component::Negation(ref negated) => {
context.nesting_level += 1;
@ -767,6 +767,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>,
a: i32,
b: i32,
is_of_type: bool,