Struct style::selector_map::SelectorMap

source ·
pub struct SelectorMap<T: 'static> {
    pub root: SmallVec<[T; 1]>,
    pub id_hash: MaybeCaseInsensitiveHashMap<Atom, SmallVec<[T; 1]>>,
    pub class_hash: MaybeCaseInsensitiveHashMap<Atom, SmallVec<[T; 1]>>,
    pub local_name_hash: PrecomputedHashMap<LocalName, SmallVec<[T; 1]>>,
    pub attribute_hash: PrecomputedHashMap<LocalName, SmallVec<[T; 1]>>,
    pub namespace_hash: PrecomputedHashMap<Namespace, SmallVec<[T; 1]>>,
    pub rare_pseudo_classes: SmallVec<[T; 1]>,
    pub other: SmallVec<[T; 1]>,
    pub count: usize,
}
Expand description

Map element data to selector-providing objects for which the last simple selector starts with them.

e.g., “p > img” would go into the set of selectors corresponding to the element “img” “a .foo .bar.baz” would go into the set of selectors corresponding to the class “bar”

Because we match selectors right-to-left (i.e., moving up the tree from an element), we need to compare the last simple selector in the selector with the element.

So, if an element has ID “id1” and classes “foo” and “bar”, then all the rules it matches will have their last simple selector starting either with “#id1” or with “.foo” or with “.bar”.

Hence, the union of the rules keyed on each of element’s classes, ID, element name, etc. will contain the Selectors that actually match that element.

We use a 1-entry SmallVec to avoid a separate heap allocation in the case where we only have one entry, which is quite common. See measurements in:

  • https://bugzilla.mozilla.org/show_bug.cgi?id=1363789#c5
  • https://bugzilla.mozilla.org/show_bug.cgi?id=681755

TODO: Tune the initial capacity of the HashMap

Fields§

§root: SmallVec<[T; 1]>

Rules that have :root selectors.

§id_hash: MaybeCaseInsensitiveHashMap<Atom, SmallVec<[T; 1]>>

A hash from an ID to rules which contain that ID selector.

§class_hash: MaybeCaseInsensitiveHashMap<Atom, SmallVec<[T; 1]>>

A hash from a class name to rules which contain that class selector.

§local_name_hash: PrecomputedHashMap<LocalName, SmallVec<[T; 1]>>

A hash from local name to rules which contain that local name selector.

§attribute_hash: PrecomputedHashMap<LocalName, SmallVec<[T; 1]>>

A hash from attributes to rules which contain that attribute selector.

§namespace_hash: PrecomputedHashMap<Namespace, SmallVec<[T; 1]>>

A hash from namespace to rules which contain that namespace selector.

§rare_pseudo_classes: SmallVec<[T; 1]>

Rules for pseudo-states that are rare but have global selectors.

§other: SmallVec<[T; 1]>

All other rules.

§count: usize

The number of entries in this map.

Implementations§

source§

impl<T> SelectorMap<T>

source

pub fn new() -> Self

Trivially constructs an empty SelectorMap.

source

pub fn shrink_if_needed(&mut self)

Shrink the capacity of the map if needed.

source

pub fn clear(&mut self)

Clears the hashmap retaining storage.

source

pub fn is_empty(&self) -> bool

Returns whether there are any entries in the map.

source

pub fn len(&self) -> usize

Returns the number of entries.

source§

impl SelectorMap<Rule>

source

pub fn get_all_matching_rules<E>( &self, element: E, rule_hash_target: E, matching_rules_list: &mut ApplicableDeclarationList, matching_context: &mut MatchingContext<'_, E::Impl>, cascade_level: CascadeLevel, cascade_data: &CascadeData, stylist: &Stylist, )
where E: TElement,

Append to rule_list all Rules in self that match element.

Extract matching rules as per element’s ID, classes, tag name, etc.. Sort the Rules at the end to maintain cascading order.

source

pub(crate) fn get_matching_rules<E>( element: E, rules: &[Rule], matching_rules: &mut ApplicableDeclarationList, matching_context: &mut MatchingContext<'_, E::Impl>, cascade_level: CascadeLevel, cascade_data: &CascadeData, stylist: &Stylist, )
where E: TElement,

Adds rules in rules that match element to the matching_rules list.

source§

impl<T: SelectorMapEntry> SelectorMap<T>

source

pub fn insert( &mut self, entry: T, quirks_mode: QuirksMode, ) -> Result<(), AllocErr>

Inserts an entry into the correct bucket(s).

source

pub fn lookup<'a, E, F>( &'a self, element: E, quirks_mode: QuirksMode, relevant_attributes: Option<&mut RelevantAttributes>, f: F, ) -> bool
where E: TElement, F: FnMut(&'a T) -> bool,

Looks up entries by id, class, local name, namespace, and other (in order).

Each entry is passed to the callback, which returns true to continue iterating entries, or false to terminate the lookup.

Returns false if the callback ever returns false.

FIXME(bholley) This overlaps with SelectorMap::get_all_matching_rules, but that function is extremely hot and I’d rather not rearrange it.

source

fn lookup_with_state<'a, E, F>( &'a self, element: E, element_state: ElementState, quirks_mode: QuirksMode, relevant_attributes: Option<&mut RelevantAttributes>, f: F, ) -> bool
where E: TElement, F: FnMut(&'a T) -> bool,

source

pub fn lookup_with_additional<'a, E, F>( &'a self, element: E, quirks_mode: QuirksMode, additional_id: Option<&WeakAtom>, additional_classes: &[Atom], additional_states: ElementState, f: F, ) -> bool
where E: TElement, F: FnMut(&'a T) -> bool,

Performs a normal lookup, and also looks up entries for the passed-in id and classes.

Each entry is passed to the callback, which returns true to continue iterating entries, or false to terminate the lookup.

Returns false if the callback ever returns false.

Trait Implementations§

source§

impl<T: Clone + 'static> Clone for SelectorMap<T>

source§

fn clone(&self) -> SelectorMap<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + 'static> Debug for SelectorMap<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: 'static> Default for SelectorMap<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> MallocSizeOf for SelectorMap<T>
where T: MallocSizeOf + 'static,

source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.

Auto Trait Implementations§

§

impl<T> Freeze for SelectorMap<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SelectorMap<T>
where T: RefUnwindSafe,

§

impl<T> Send for SelectorMap<T>
where T: Send,

§

impl<T> Sync for SelectorMap<T>
where T: Sync,

§

impl<T> Unpin for SelectorMap<T>
where T: Unpin,

§

impl<T> UnwindSafe for SelectorMap<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> MaybeBoxed<Box<T>> for T

source§

fn maybe_boxed(self) -> Box<T>

Convert
source§

impl<T> MaybeBoxed<T> for T

source§

fn maybe_boxed(self) -> T

Convert
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T