mirror of
https://github.com/servo/servo.git
synced 2025-08-08 23:15:33 +01:00
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:
parent
a973371cf2
commit
3076481c52
8 changed files with 223 additions and 71 deletions
|
@ -2,7 +2,9 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use crate::tree::OpaqueElement;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::{parser::Selector, tree::OpaqueElement, SelectorImpl};
|
||||
use fxhash::FxHashMap;
|
||||
|
||||
/// A cache to speed up matching of nth-index-like selectors.
|
||||
|
@ -13,20 +15,68 @@ use fxhash::FxHashMap;
|
|||
#[derive(Default)]
|
||||
pub struct NthIndexCache {
|
||||
nth: NthIndexCacheInner,
|
||||
nth_of_selectors: NthIndexOfSelectorsCaches,
|
||||
nth_last: NthIndexCacheInner,
|
||||
nth_last_of_selectors: NthIndexOfSelectorsCaches,
|
||||
nth_of_type: NthIndexCacheInner,
|
||||
nth_last_of_type: NthIndexCacheInner,
|
||||
}
|
||||
|
||||
impl NthIndexCache {
|
||||
/// Gets the appropriate cache for the given parameters.
|
||||
pub fn get(&mut self, is_of_type: bool, is_from_end: bool) -> &mut NthIndexCacheInner {
|
||||
match (is_of_type, is_from_end) {
|
||||
(false, false) => &mut self.nth,
|
||||
(false, true) => &mut self.nth_last,
|
||||
(true, false) => &mut self.nth_of_type,
|
||||
(true, true) => &mut self.nth_last_of_type,
|
||||
pub fn get<Impl: SelectorImpl>(
|
||||
&mut self,
|
||||
is_of_type: bool,
|
||||
is_from_end: bool,
|
||||
selectors: &[Selector<Impl>],
|
||||
) -> &mut NthIndexCacheInner {
|
||||
if is_of_type {
|
||||
return if is_from_end {
|
||||
&mut self.nth_last_of_type
|
||||
} else {
|
||||
&mut self.nth_of_type
|
||||
};
|
||||
}
|
||||
if !selectors.is_empty() {
|
||||
return if is_from_end {
|
||||
self.nth_last_of_selectors.lookup(selectors)
|
||||
} else {
|
||||
self.nth_of_selectors.lookup(selectors)
|
||||
};
|
||||
}
|
||||
if is_from_end {
|
||||
&mut self.nth_last
|
||||
} else {
|
||||
&mut self.nth
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Hash, Eq, PartialEq)]
|
||||
struct SelectorListCacheKey(usize);
|
||||
|
||||
/// Use the selector list's pointer as the cache key
|
||||
impl SelectorListCacheKey {
|
||||
// :nth-child of selectors are reference-counted with `ThinArc`, so we know their pointers are stable.
|
||||
fn new<Impl: SelectorImpl>(selectors: &[Selector<Impl>]) -> Self {
|
||||
Self(selectors.as_ptr() as usize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Use a different map of cached indices per :nth-child's or :nth-last-child's selector list
|
||||
#[derive(Default)]
|
||||
pub struct NthIndexOfSelectorsCaches(FxHashMap<SelectorListCacheKey, NthIndexCacheInner>);
|
||||
|
||||
/// Get or insert a map of cached incides for the selector list of this
|
||||
/// particular :nth-child or :nth-last-child pseudoclass
|
||||
impl NthIndexOfSelectorsCaches {
|
||||
pub fn lookup<Impl: SelectorImpl>(
|
||||
&mut self,
|
||||
selectors: &[Selector<Impl>],
|
||||
) -> &mut NthIndexCacheInner {
|
||||
self.0
|
||||
.entry(SelectorListCacheKey::new(selectors))
|
||||
.or_default()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue