/* This Source Code Form is subject to the terms of the Mozilla Public * 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/. */ //! A data structure to efficiently index structs containing selectors by local //! name, ids and hash. use crate::AllocErr; use crate::applicable_declarations::{ApplicableDeclarationList, ScopeProximity}; use crate::context::QuirksMode; use crate::derives::*; use crate::dom::TElement; use crate::rule_tree::CascadeLevel; use crate::selector_parser::SelectorImpl; use crate::stylist::{CascadeData, ContainerConditionId, Rule, ScopeConditionId, Stylist}; use crate::{Atom, LocalName, Namespace, ShrinkIfNeeded, WeakAtom}; use dom::ElementState; use hashbrown::hash_map; use hashbrown::{HashMap, HashSet}; use precomputed_hash::PrecomputedHash; use selectors::matching::MatchingContext; use selectors::parser::{Combinator, Component, SelectorIter}; use smallvec::SmallVec; use std::borrow::Borrow; use std::hash::{BuildHasherDefault, Hash, Hasher}; /// A hasher implementation that doesn't hash anything, because it expects its /// input to be a suitable u64 or u32 hash. #[derive(Default)] pub struct PrecomputedHasher { hash: u64, #[cfg(debug_assertions)] initialized: bool, } /// A vector of relevant attributes, that can be useful for revalidation. pub type RelevantAttributes = thin_vec::ThinVec; /// This is a set of pseudo-classes that are both relatively-rare (they don't /// affect most elements by default) and likely or known to have global rules /// (in e.g., the UA sheets). /// /// We can avoid selector-matching those global rules for all elements without /// these pseudo-class states. const RARE_PSEUDO_CLASS_STATES: ElementState = ElementState::from_bits_retain( ElementState::FULLSCREEN.bits() | ElementState::PICTURE_IN_PICTURE.bits() | ElementState::VISITED_OR_UNVISITED.bits() | ElementState::URLTARGET.bits() | ElementState::INERT.bits() | ElementState::FOCUS.bits() | ElementState::FOCUSRING.bits() | ElementState::TOPMOST_MODAL.bits() | ElementState::SUPPRESS_FOR_PRINT_SELECTION.bits() | ElementState::ACTIVE_VIEW_TRANSITION.bits() | ElementState::HEADING_LEVEL_BITS.bits(), ); /// A simple alias for a hashmap using PrecomputedHasher. pub type PrecomputedHashMap = HashMap>; /// A simple alias for a hashset using PrecomputedHasher. pub type PrecomputedHashSet = HashSet>; impl Hasher for PrecomputedHasher { #[inline] fn write(&mut self, _: &[u8]) { unreachable!( "Called into PrecomputedHasher with something that isn't \ a u64 or u32" ) } #[inline] fn write_u32(&mut self, i: u32) { #[cfg(debug_assertions)] debug_assert!(!self.initialized); debug_assert_eq!(self.hash, 0); let extended = i as u64; self.hash = (extended << 32) | extended; #[cfg(debug_assertions)] { self.initialized = true; } } #[inline] fn write_u64(&mut self, i: u64) { #[cfg(debug_assertions)] debug_assert!(!self.initialized); debug_assert_eq!(self.hash, 0); self.hash = i; #[cfg(debug_assertions)] { self.initialized = true; } } #[inline] fn finish(&self) -> u64 { #[cfg(debug_assertions)] debug_assert!(self.initialized); self.hash } } /// A trait to abstract over a given selector map entry. pub trait SelectorMapEntry: Sized + Clone { /// Gets the selector we should use to index in the selector map. fn selector(&self) -> SelectorIter<'_, SelectorImpl>; /// Notes the bucketing decision in the entry. fn set_bucket_matches(&mut self, _: BucketMatches) {} } /// 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 #[derive(Clone, Debug, MallocSizeOf)] pub struct SelectorMap { /// Rules that have `:root` selectors. pub root: SmallVec<[T; 1]>, /// A hash from an ID to rules which contain that ID selector. pub id_hash: MaybeCaseInsensitiveHashMap>, /// A hash from a class name to rules which contain that class selector. pub class_hash: MaybeCaseInsensitiveHashMap>, /// A hash from local name to rules which contain that local name selector. pub local_name_hash: PrecomputedHashMap>, /// A hash from attributes to rules which contain that attribute selector. pub attribute_hash: PrecomputedHashMap>, /// A hash from namespace to rules which contain that namespace selector. pub namespace_hash: PrecomputedHashMap>, /// Rules for pseudo-states that are rare but have global selectors. pub rare_pseudo_classes: SmallVec<[T; 1]>, /// All other rules. pub other: SmallVec<[T; 1]>, /// The number of entries in this map. pub count: usize, } impl Default for SelectorMap { #[inline] fn default() -> Self { Self::new() } } impl SelectorMap { /// Trivially constructs an empty `SelectorMap`. pub fn new() -> Self { SelectorMap { root: SmallVec::new(), id_hash: MaybeCaseInsensitiveHashMap::new(), class_hash: MaybeCaseInsensitiveHashMap::new(), attribute_hash: HashMap::default(), local_name_hash: HashMap::default(), namespace_hash: HashMap::default(), rare_pseudo_classes: SmallVec::new(), other: SmallVec::new(), count: 0, } } /// Shrink the capacity of the map if needed. pub fn shrink_if_needed(&mut self) { self.id_hash.shrink_if_needed(); self.class_hash.shrink_if_needed(); self.attribute_hash.shrink_if_needed(); self.local_name_hash.shrink_if_needed(); self.namespace_hash.shrink_if_needed(); } /// Clears the hashmap retaining storage. pub fn clear(&mut self) { self.root.clear(); self.id_hash.clear(); self.class_hash.clear(); self.attribute_hash.clear(); self.local_name_hash.clear(); self.namespace_hash.clear(); self.rare_pseudo_classes.clear(); self.other.clear(); self.count = 0; } /// Returns whether there are any entries in the map. pub fn is_empty(&self) -> bool { self.count == 0 } /// Returns the number of entries. pub fn len(&self) -> usize { self.count } } impl SelectorMap { /// 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. pub fn get_all_matching_rules<'a, E>( &'a self, element: E, rule_hash_target: E, matching_rules_list: &mut ApplicableDeclarationList<'a>, matching_context: &mut MatchingContext, cascade_level: CascadeLevel, cascade_data: &'a CascadeData, stylist: &Stylist, ) where E: TElement, { if self.is_empty() { return; } let quirks_mode = matching_context.quirks_mode(); if rule_hash_target.is_root() { SelectorMap::get_matching_rules( element, &self.root, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ); } if let Some(id) = rule_hash_target.id() && let Some(rules) = self.id_hash.get(id, quirks_mode) { SelectorMap::get_matching_rules( element, rules, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ) } rule_hash_target.each_class(|class| { if let Some(rules) = self.class_hash.get(class, quirks_mode) { SelectorMap::get_matching_rules( element, rules, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ) } }); rule_hash_target.each_attr_name(|name| { if let Some(rules) = self.attribute_hash.get(name) { SelectorMap::get_matching_rules( element, rules, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ) } }); if let Some(rules) = self.local_name_hash.get(rule_hash_target.local_name()) { SelectorMap::get_matching_rules( element, rules, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ) } if rule_hash_target .state() .intersects(RARE_PSEUDO_CLASS_STATES) { SelectorMap::get_matching_rules( element, &self.rare_pseudo_classes, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ); } if let Some(rules) = self.namespace_hash.get(rule_hash_target.namespace()) { SelectorMap::get_matching_rules( element, rules, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ) } SelectorMap::get_matching_rules( element, &self.other, matching_rules_list, matching_context, cascade_level, cascade_data, stylist, ); } /// Adds rules in `rules` that match `element` to the `matching_rules` list. pub(crate) fn get_matching_rules<'a, E>( element: E, rules: &'a [Rule], matching_rules: &mut ApplicableDeclarationList<'a>, matching_context: &mut MatchingContext, cascade_level: CascadeLevel, cascade_data: &'a CascadeData, stylist: &Stylist, ) where E: TElement, { for rule in rules { let scope_proximity = if rule.scope_condition_id == ScopeConditionId::none() { if !rule.matches_selector(element, matching_context) { continue; } ScopeProximity::infinity() } else { let result = cascade_data.find_scope_proximity_if_matching(rule, element, matching_context); if result == ScopeProximity::infinity() { continue; } result }; if rule.container_condition_id != ContainerConditionId::none() && !cascade_data.container_condition_matches( rule.container_condition_id, stylist, element, matching_context, ) { continue; } matching_rules.push(rule.to_applicable_declaration_block( cascade_level, cascade_data, scope_proximity, )); } } } impl SelectorMap { /// Inserts an entry into the correct bucket(s). pub fn insert(&mut self, mut entry: T, quirks_mode: QuirksMode) -> Result<(), AllocErr> { self.count += 1; // NOTE(emilio): It'd be nice for this to be a separate function, but // then the compiler can't reason about the lifetime dependency between // `entry` and `bucket`, and would force us to clone the rule in the // common path. let mut bucket_matches = BucketMatches::Full; macro_rules! insert_into_bucket { ($entry:ident, $bucket:expr) => {{ let vec = match $bucket { Bucket::Root => &mut self.root, Bucket::ID(id) => { self.id_hash .try_get_or_insert_with(id, quirks_mode, Default::default)? }, Bucket::Class(class) => self.class_hash.try_get_or_insert_with( class, quirks_mode, Default::default, )?, Bucket::Attribute { name, lower_name } | Bucket::LocalName { name, lower_name } => { // If the local name in the selector isn't lowercase, // insert it into the rule hash twice. This means that, // during lookup, we can always find the rules based on // the local name of the element, regardless of whether // it's an html element in an html document (in which // case we match against lower_name) or not (in which // case we match against name). // // In the case of a non-html-element-in-html-document // with a lowercase localname and a non-lowercase // selector, the rulehash lookup may produce superfluous // selectors, but the subsequent selector matching work // will filter them out. let is_attribute = matches!($bucket, Bucket::Attribute { .. }); let hash = if is_attribute { &mut self.attribute_hash } else { &mut self.local_name_hash }; if name != lower_name { hash.try_reserve(1)?; let vec = hash.entry_ref(lower_name).or_default(); vec.try_reserve(1)?; let mut entry = $entry.clone(); entry.set_bucket_matches(bucket_matches); vec.push(entry); } hash.try_reserve(1)?; hash.entry_ref(name).or_default() }, Bucket::Namespace(url) => { self.namespace_hash.try_reserve(1)?; self.namespace_hash.entry_ref(url).or_default() }, Bucket::RarePseudoClasses => &mut self.rare_pseudo_classes, Bucket::Universal => &mut self.other, }; vec.try_reserve(1)?; $entry.set_bucket_matches(bucket_matches); vec.push($entry); }}; } let bucket = { let mut disjoint_buckets = SmallVec::new(); let bucket = find_bucket( entry.selector(), quirks_mode, &mut disjoint_buckets, &mut bucket_matches, /* nested = */ false, ); // See if inserting this selector in multiple entries in the // selector map would be worth it. Consider a case like: // // .foo:where(div, #bar) // // There, `bucket` would be `Class(foo)`, and disjoint_buckets would // be `[LocalName { div }, ID(bar)]`. // // Here we choose to insert the selector in the `.foo` bucket in // such a case, as it's likely more worth it than inserting it in // both `div` and `#bar`. // // This is specially true if there's any universal selector in the // `disjoint_selectors` set, at which point we'd just be doing // wasted work. if !disjoint_buckets.is_empty() && disjoint_buckets .iter() .all(|b| b.more_specific_than(&bucket)) { for bucket in &disjoint_buckets { let mut entry = entry.clone(); insert_into_bucket!(entry, *bucket); } return Ok(()); } bucket }; insert_into_bucket!(entry, bucket); Ok(()) } /// 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. 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, { self.lookup_with_state( element, element.state(), quirks_mode, relevant_attributes, f, ) } #[inline] fn lookup_with_state<'a, E, F>( &'a self, element: E, element_state: ElementState, quirks_mode: QuirksMode, mut relevant_attributes: Option<&mut RelevantAttributes>, mut f: F, ) -> bool where E: TElement, F: FnMut(&'a T) -> bool, { if element.is_root() { for entry in self.root.iter() { if !f(entry) { return false; } } } if let Some(id) = element.id() && let Some(v) = self.id_hash.get(id, quirks_mode) { for entry in v.iter() { if !f(entry) { return false; } } } let mut done = false; element.each_class(|class| { if done { return; } if let Some(v) = self.class_hash.get(class, quirks_mode) { for entry in v.iter() { if !f(entry) { done = true; return; } } } }); if done { return false; } element.each_attr_name(|name| { if done { return; } if let Some(v) = self.attribute_hash.get(name) { if let Some(ref mut relevant_attributes) = relevant_attributes { relevant_attributes.push(name.clone()); } for entry in v.iter() { if !f(entry) { done = true; return; } } } }); if done { return false; } if let Some(v) = self.local_name_hash.get(element.local_name()) { for entry in v.iter() { if !f(entry) { return false; } } } if let Some(v) = self.namespace_hash.get(element.namespace()) { for entry in v.iter() { if !f(entry) { return false; } } } if element_state.intersects(RARE_PSEUDO_CLASS_STATES) { for entry in self.rare_pseudo_classes.iter() { if !f(entry) { return false; } } } for entry in self.other.iter() { if !f(entry) { return false; } } true } /// 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. #[inline] 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, mut f: F, ) -> bool where E: TElement, F: FnMut(&'a T) -> bool, { // Do the normal lookup. if !self.lookup_with_state( element, element.state() | additional_states, quirks_mode, /* relevant_attributes = */ None, &mut f, ) { return false; } // Check the additional id. if let Some(id) = additional_id && let Some(v) = self.id_hash.get(id, quirks_mode) { for entry in v.iter() { if !f(entry) { return false; } } } // Check the additional classes. for class in additional_classes { if let Some(v) = self.class_hash.get(class, quirks_mode) { for entry in v.iter() { if !f(entry) { return false; } } } } true } } #[derive(PartialEq)] enum Bucket<'a> { Universal, Namespace(&'a Namespace), RarePseudoClasses, LocalName { name: &'a LocalName, lower_name: &'a LocalName, }, Attribute { name: &'a LocalName, lower_name: &'a LocalName, }, Class(&'a Atom), ID(&'a Atom), Root, } impl<'a> Bucket<'a> { /// root > id > class > local name > namespace > pseudo-classes > universal. #[inline] fn specificity(&self) -> usize { match *self { Bucket::Universal => 0, Bucket::Namespace(..) => 1, Bucket::RarePseudoClasses => 2, Bucket::LocalName { .. } => 3, Bucket::Attribute { .. } => 4, Bucket::Class(..) => 5, Bucket::ID(..) => 6, Bucket::Root => 7, } } #[inline] fn more_or_equally_specific_than(&self, other: &Self) -> bool { self.specificity() >= other.specificity() } #[inline] fn more_specific_than(&self, other: &Self) -> bool { self.specificity() > other.specificity() } } type DisjointBuckets<'a> = SmallVec<[Bucket<'a>; 5]>; /// Whether our bucket is known to match our full selector, the subject part, or nothing. #[derive(Copy, Clone, Debug, PartialEq, MallocSizeOf)] pub enum BucketMatches { /// Full selector is known-matching. Full, /// The subject is known-matching. Subject, /// Nothing is known-matching. Unknown, } fn specific_bucket_for<'a>( component: &'a Component, quirks_mode: QuirksMode, disjoint_buckets: &mut DisjointBuckets<'a>, bucket_matches: &mut BucketMatches, nested: bool, ) -> Bucket<'a> { match *component { Component::Root => Bucket::Root, Component::ID(ref id) => { if quirks_mode == QuirksMode::Quirks { // Lookup is case-insensitive, we still need to match the real thing. *bucket_matches = BucketMatches::Unknown; } Bucket::ID(id) }, Component::Class(ref class) => { if quirks_mode == QuirksMode::Quirks { // Lookup is case-insensitive, we still need to match the real thing. *bucket_matches = BucketMatches::Unknown; } Bucket::Class(class) }, Component::AttributeInNoNamespace { ref local_name, .. } => { // Depends on the attribute value, or might have namespaced attributes (ugh!). *bucket_matches = BucketMatches::Unknown; Bucket::Attribute { name: local_name, lower_name: local_name, } }, Component::AttributeInNoNamespaceExists { ref local_name, ref local_name_lower, } => { // Might have namespaced attributes (ugh!). *bucket_matches = BucketMatches::Unknown; Bucket::Attribute { name: local_name, lower_name: local_name_lower, } }, Component::AttributeOther(ref selector) => { // Depends on the attribute value, or might have namespaced attributes (ugh!). *bucket_matches = BucketMatches::Unknown; Bucket::Attribute { name: &selector.local_name, lower_name: &selector.local_name_lower, } }, Component::LocalName(ref selector) => { if selector.name != selector.lower_name { *bucket_matches = BucketMatches::Unknown; } Bucket::LocalName { name: &selector.name, lower_name: &selector.lower_name, } }, Component::Namespace(_, ref url) | Component::DefaultNamespace(ref url) => { Bucket::Namespace(url) }, // ::slotted(..) isn't a normal pseudo-element, so we can insert it on // the rule hash normally without much problem. For example, in a // selector like: // // div::slotted(span)::before // // It looks like: // // [ // LocalName(div), // Combinator(SlotAssignment), // Slotted(span), // Combinator::PseudoElement, // PseudoElement(::before), // ] // // So inserting `span` in the rule hash makes sense since we want to // match the slotted . Component::Slotted(ref selector) => { // We need to set unknown here because still shouldn't match... We could avoid // looking up slotted rules for elements instead. *bucket_matches = BucketMatches::Unknown; find_bucket( selector.iter(), quirks_mode, disjoint_buckets, bucket_matches, /* nested = */ true, ) }, Component::Host(ref selector) => { // Even tho we bucket shadow host rules in shadow trees, this rule could be in the // document. // // TODO(emilio): We could return more state during bucketing and just discard the // selector entirely, probably. *bucket_matches = BucketMatches::Unknown; if let Some(selector) = selector { find_bucket( selector.iter(), quirks_mode, disjoint_buckets, bucket_matches, /* nested = */ true, ) } else { Bucket::Universal } }, Component::Is(ref list) | Component::Where(ref list) => { if list.len() == 1 { find_bucket( list.slice()[0].iter(), quirks_mode, disjoint_buckets, bucket_matches, /* nested = */ true, ) } else { // TODO: Since the is/where() semantics are effectively OR rather than AND, this is // a bit too conservative, we could keep bucket_matches set for some of the disjoint // buckets or so... But then we also need to deal with other special-cases like // :is(:host, #not-host) or so. *bucket_matches = BucketMatches::Unknown; for selector in list.slice() { let bucket = find_bucket( selector.iter(), quirks_mode, disjoint_buckets, bucket_matches, /* nested = */ true, ); if disjoint_buckets.last() == Some(&bucket) { // It's pretty common to have selectors like: // input:is([type=foo], [type=bar], ...) // Try to prevent trivial duplicate entries for the same bucket. continue; } disjoint_buckets.push(bucket); } Bucket::Universal } }, Component::NonTSPseudoClass(ref pseudo_class) if pseudo_class .state_flag() .intersects(RARE_PSEUDO_CLASS_STATES) => { // We bucket a bunch of pseudo-classes together so we still need to do the matching to // figure out if the specific one is covered... *bucket_matches = BucketMatches::Unknown; Bucket::RarePseudoClasses }, Component::PseudoElement(ref pseudo) => { // Pseudos are covered by bucketing, unless they are functional in which case they share // a map with the other pseudos of their kind, or if they're nested (due to CSS nesting // or so) in which case they never match and we can't skip the subject part. if pseudo.has_argument() || nested { *bucket_matches = BucketMatches::Unknown; } Bucket::Universal }, Component::ExplicitUniversalType | Component::ExplicitAnyNamespace => { // The universal selectors, well, always match, so we can leave bucket_matches as-is... Bucket::Universal }, _ => { *bucket_matches = BucketMatches::Unknown; Bucket::Universal }, } } /// Searches a compound selector from left to right, and returns the appropriate /// bucket for it. /// /// It also populates disjoint_buckets with dependencies from nested selectors /// with any semantics like :is() and :where(). /// /// If the bucket is not guaranteed to cover the whole selector, it will set bucket_matches to /// either Unknown or Subject. #[inline(always)] fn find_bucket<'a>( mut iter: SelectorIter<'a, SelectorImpl>, quirks_mode: QuirksMode, disjoint_buckets: &mut DisjointBuckets<'a>, bucket_matches: &mut BucketMatches, nested: bool, ) -> Bucket<'a> { let mut current_bucket = Bucket::Universal; loop { for ss in &mut iter { let new_bucket = specific_bucket_for(ss, quirks_mode, disjoint_buckets, bucket_matches, nested); // NOTE: When presented with the choice of multiple specific selectors, use the // rightmost, on the assumption that that's less common, see bug 1829540. if current_bucket != Bucket::Universal { // Selector fits in multiple buckets so need to do selector matching. *bucket_matches = BucketMatches::Unknown; } if new_bucket.more_or_equally_specific_than(¤t_bucket) { current_bucket = new_bucket; } } // Effectively, pseudo-elements are ignored, given only state // pseudo-classes may appear before them. match iter.next_sequence() { None => break, Some(Combinator::PseudoElement) => continue, Some(..) => { // We need to match the combinator. if *bucket_matches != BucketMatches::Unknown { if nested { *bucket_matches = BucketMatches::Unknown; } else { *bucket_matches = BucketMatches::Subject; } } break; }, } } current_bucket } /// Wrapper for PrecomputedHashMap that does ASCII-case-insensitive lookup in quirks mode. #[derive(Clone, Debug, MallocSizeOf)] pub struct MaybeCaseInsensitiveHashMap(PrecomputedHashMap); impl Default for MaybeCaseInsensitiveHashMap { #[inline] fn default() -> Self { MaybeCaseInsensitiveHashMap(PrecomputedHashMap::default()) } } impl MaybeCaseInsensitiveHashMap { /// Empty map pub fn new() -> Self { Self::default() } /// Shrink the capacity of the map if needed. pub fn shrink_if_needed(&mut self) { self.0.shrink_if_needed() } /// Returns the value for `key`, inserting `default` if missing. The key is only cloned when /// it's actually inserted. pub fn try_get_or_insert_with( &mut self, key: &WeakAtom, quirks_mode: QuirksMode, default: impl FnOnce() -> V, ) -> Result<&mut V, AllocErr> { self.0.try_reserve(1)?; let lower; let key: &WeakAtom = if quirks_mode == QuirksMode::Quirks { lower = key.to_ascii_lowercase(); lower.borrow() } else { key }; Ok(self.0.entry_ref(key).or_insert_with(default)) } /// HashMap::is_empty #[inline] pub fn is_empty(&self) -> bool { self.0.is_empty() } /// HashMap::iter pub fn iter(&self) -> hash_map::Iter<'_, Atom, V> { self.0.iter() } /// HashMap::clear pub fn clear(&mut self) { self.0.clear() } /// HashMap::get pub fn get(&self, key: &WeakAtom, quirks_mode: QuirksMode) -> Option<&V> { let lower; let key: &WeakAtom = if quirks_mode == QuirksMode::Quirks { lower = key.to_ascii_lowercase(); lower.borrow() } else { key }; self.0.get(key) } } #[test] #[cfg(feature = "servo")] fn test_precomputed_hash_set() { let mut set = PrecomputedHashSet::default(); let atom = atom!(""); assert!(!set.contains(&atom)); set.insert(atom.clone()); assert!(set.contains(&atom)); }