style: Implement selector matching for :nth-child(An+B of selector list) and :nth-last-child(An+B of selector list)

Since we have been using a single hash map to cache all :nth-child
indices (with no selector list), each different selector will need its
own cache.

As a side note, this patch does not address invalidation.

Differential Revision: https://phabricator.services.mozilla.com/D166266
This commit is contained in:
Zach Hoffman 2023-01-16 11:26:41 +00:00 committed by Martin Robinson
parent a973371cf2
commit 3076481c52
8 changed files with 223 additions and 71 deletions

View file

@ -14,7 +14,7 @@ use crate::values::AtomIdent;
use selectors::attr::CaseSensitivity;
use selectors::matching::{self, MatchingContext, MatchingMode, NeedsSelectorFlags};
use selectors::parser::{Combinator, Component, LocalName, SelectorImpl};
use selectors::{Element, NthIndexCache, SelectorList};
use selectors::{Element, SelectorList};
use smallvec::SmallVec;
/// <https://dom.spec.whatwg.org/#dom-element-matches>
@ -26,10 +26,12 @@ pub fn element_matches<E>(
where
E: Element,
{
let mut nth_index_cache = Default::default();
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
None,
&mut nth_index_cache,
quirks_mode,
NeedsSelectorFlags::No,
);
@ -47,12 +49,12 @@ pub fn element_closest<E>(
where
E: Element,
{
let mut nth_index_cache = NthIndexCache::default();
let mut nth_index_cache = Default::default();
let mut context = MatchingContext::new(
MatchingMode::Normal,
None,
Some(&mut nth_index_cache),
&mut nth_index_cache,
quirks_mode,
NeedsSelectorFlags::No,
);
@ -621,13 +623,13 @@ pub fn query_selector<E, Q>(
{
use crate::invalidation::element::invalidator::TreeStyleInvalidator;
let mut nth_index_cache = Default::default();
let quirks_mode = root.owner_doc().quirks_mode();
let mut nth_index_cache = NthIndexCache::default();
let mut matching_context = MatchingContext::new(
MatchingMode::Normal,
None,
Some(&mut nth_index_cache),
&mut nth_index_cache,
quirks_mode,
NeedsSelectorFlags::No,
);

View file

@ -13,6 +13,7 @@ use crate::stylist::CascadeData;
use selectors::matching::{
MatchingContext, MatchingMode, NeedsSelectorFlags, QuirksMode, VisitedHandlingMode,
};
use selectors::NthIndexCache;
use style_traits::dom::DocumentState;
/// A struct holding the members necessary to invalidate document state
@ -43,11 +44,16 @@ pub struct DocumentStateInvalidationProcessor<'a, E: TElement, I> {
impl<'a, E: TElement, I> DocumentStateInvalidationProcessor<'a, E, I> {
/// Creates a new DocumentStateInvalidationProcessor.
#[inline]
pub fn new(rules: I, document_states_changed: DocumentState, quirks_mode: QuirksMode) -> Self {
pub fn new(
rules: I,
document_states_changed: DocumentState,
nth_index_cache: &'a mut NthIndexCache,
quirks_mode: QuirksMode,
) -> Self {
let mut matching_context = MatchingContext::<'a, E::Impl>::new_for_visited(
MatchingMode::Normal,
None,
None,
nth_index_cache,
VisitedHandlingMode::AllLinksVisitedAndUnvisited,
quirks_mode,
NeedsSelectorFlags::No,

View file

@ -65,7 +65,7 @@ impl<'a, 'b: 'a, E: TElement + 'b> StateAndAttrInvalidationProcessor<'a, 'b, E>
let matching_context = MatchingContext::new_for_visited(
MatchingMode::Normal,
None,
Some(nth_index_cache),
nth_index_cache,
VisitedHandlingMode::AllLinksVisitedAndUnvisited,
shared_context.quirks_mode(),
NeedsSelectorFlags::No,

View file

@ -462,7 +462,7 @@ where
let mut matching_context = MatchingContext::new_for_visited(
MatchingMode::Normal,
Some(bloom_filter),
Some(nth_index_cache),
nth_index_cache,
visited_handling,
self.context.shared.quirks_mode(),
NeedsSelectorFlags::Yes,
@ -538,7 +538,7 @@ where
let mut matching_context = MatchingContext::<'_, E::Impl>::new_for_visited(
MatchingMode::ForStatelessPseudoElement,
Some(bloom_filter),
Some(nth_index_cache),
nth_index_cache,
visited_handling,
self.context.shared.quirks_mode(),
NeedsSelectorFlags::Yes,

View file

@ -1125,6 +1125,7 @@ impl Stylist {
{
debug_assert!(pseudo.is_lazy());
let mut nth_index_cache = Default::default();
// No need to bother setting the selector flags when we're computing
// default styles.
let needs_selector_flags = if rule_inclusion == RuleInclusion::DefaultOnly {
@ -1137,7 +1138,7 @@ impl Stylist {
let mut matching_context = MatchingContext::<'_, E::Impl>::new(
MatchingMode::ForStatelessPseudoElement,
None,
None,
&mut nth_index_cache,
self.quirks_mode,
needs_selector_flags,
);
@ -1166,10 +1167,12 @@ impl Stylist {
let mut visited_rules = None;
if parent_style.visited_style().is_some() {
let mut declarations = ApplicableDeclarationList::new();
let mut nth_index_cache = Default::default();
let mut matching_context = MatchingContext::<'_, E::Impl>::new_for_visited(
MatchingMode::ForStatelessPseudoElement,
None,
None,
&mut nth_index_cache,
VisitedHandlingMode::RelevantLinkVisited,
self.quirks_mode,
needs_selector_flags,
@ -1417,7 +1420,7 @@ impl Stylist {
let mut matching_context = MatchingContext::new(
MatchingMode::Normal,
bloom,
Some(nth_index_cache),
nth_index_cache,
self.quirks_mode,
needs_selector_flags,
);