/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
usecrate::attr::{
AttrSelectorOperation, AttrSelectorWithOptionalNamespace, CaseSensitivity, NamespaceConstraint,
ParsedAttrSelectorOperation, ParsedCaseSensitivity,
}; usecrate::bloom::{BloomFilter, BLOOM_HASH_MASK}; usecrate::kleene_value::KleeneValue; usecrate::parser::{
AncestorHashes, Combinator, Component, LocalName, MatchesFeaturelessHost, NthSelectorData,
RelativeSelectorMatchHint,
}; usecrate::parser::{
NonTSPseudoClass, RelativeSelector, Selector, SelectorImpl, SelectorIter, SelectorList,
}; usecrate::relative_selector::cache::RelativeSelectorCachedMatch; usecrate::tree::Element; use bitflags::bitflags; use debug_unreachable::debug_unreachable; use log::debug; use smallvec::SmallVec; use std::borrow::Borrow;
pubusecrate::context::*;
// The bloom filter for descendant CSS selectors will have a <1% false // positive rate until it has this many selectors in it, then it will // rapidly increase. pubstatic RECOMMENDED_SELECTOR_BLOOM_FILTER_SIZE: usize = 4096;
bitflags! { /// Set of flags that are set on either the element or its parent (depending /// on the flag) if the element could potentially match a selector. #[derive(Clone, Copy)] pubstruct ElementSelectorFlags: usize { /// When a child is added or removed from the parent, all the children /// must be restyled, because they may match :nth-last-child, /// :last-of-type, :nth-last-of-type, or :only-of-type. const HAS_SLOW_SELECTOR = 1 << 0;
/// When a child is added or removed from the parent, any later /// children must be restyled, because they may match :nth-child, /// :first-of-type, or :nth-of-type. const HAS_SLOW_SELECTOR_LATER_SIBLINGS = 1 << 1;
/// HAS_SLOW_SELECTOR* was set by the presence of :nth (But not of). const HAS_SLOW_SELECTOR_NTH = 1 << 2;
/// When a DOM mutation occurs on a child that might be matched by /// :nth-last-child(.. of <selector list>), earlier children must be /// restyled, and HAS_SLOW_SELECTOR will be set (which normally /// indicates that all children will be restyled). /// /// Similarly, when a DOM mutation occurs on a child that might be /// matched by :nth-child(.. of <selector list>), later children must be /// restyled, and HAS_SLOW_SELECTOR_LATER_SIBLINGS will be set. const HAS_SLOW_SELECTOR_NTH_OF = 1 << 3;
/// When a child is added or removed from the parent, the first and /// last children must be restyled, because they may match :first-child, /// :last-child, or :only-child. const HAS_EDGE_CHILD_SELECTOR = 1 << 4;
/// The element has an empty selector, so when a child is appended we /// might need to restyle the parent completely. const HAS_EMPTY_SELECTOR = 1 << 5;
/// The element may anchor a relative selector. const ANCHORS_RELATIVE_SELECTOR = 1 << 6;
/// The element may anchor a relative selector that is not the subject /// of the whole selector. const ANCHORS_RELATIVE_SELECTOR_NON_SUBJECT = 1 << 7;
/// The element is reached by a relative selector search in the sibling direction. const RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING = 1 << 8;
/// The element is reached by a relative selector search in the ancestor direction. const RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR = 1 << 9;
// The element is reached by a relative selector search in both sibling and ancestor directions. const RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR_SIBLING = Self::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING.bits() | Self::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR.bits();
/// A child of this element may be using sibling-index() or sibling-count(), /// and must be recascaded if other children are added or removed. const MAY_HAVE_TREE_COUNTING_FUNCTION = 1 << 11;
}
}
impl ElementSelectorFlags { /// Returns the subset of flags that apply to the element. pubfn for_self(self) -> ElementSelectorFlags { self & (ElementSelectorFlags::HAS_EMPTY_SELECTOR
| ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR
| ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR_NON_SUBJECT
| ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING
| ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR)
}
/// Returns the subset of flags that apply to the parent. pubfn for_parent(self) -> ElementSelectorFlags { self & (ElementSelectorFlags::HAS_SLOW_SELECTOR
| ElementSelectorFlags::HAS_SLOW_SELECTOR_LATER_SIBLINGS
| ElementSelectorFlags::HAS_SLOW_SELECTOR_NTH
| ElementSelectorFlags::HAS_SLOW_SELECTOR_NTH_OF
| ElementSelectorFlags::HAS_EDGE_CHILD_SELECTOR
| ElementSelectorFlags::MAY_HAVE_TREE_COUNTING_FUNCTION)
}
}
#[inline(always)] pubfn matches_selector_list<E>(
selector_list: &SelectorList<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
) -> bool where
E: Element,
{ // This is pretty much any(..) but manually inlined because the compiler // refuses to do so from querySelector / querySelectorAll. for selector in selector_list.slice() { let matches = matches_selector(selector, 0, None, element, context); if matches { returntrue;
}
}
false
}
/// Given the ancestor hashes from a selector, see if the current element, /// represented by the bloom filter, has a chance of matching at all. #[inline(always)] pubfn selector_may_match(hashes: &AncestorHashes, bf: &BloomFilter) -> bool { // Check the first three hashes. Note that we can check for zero before // masking off the high bits, since if any of the first three hashes is // zero the fourth will be as well. We also take care to avoid the // special-case complexity of the fourth hash until we actually reach it, // because we usually don't. // // To be clear: this is all extremely hot. for i in0..3 { let packed = hashes.packed_hashes[i]; if packed == 0 { // No more hashes left - unable to fast-reject. returntrue;
}
if !bf.might_contain_hash(packed & BLOOM_HASH_MASK) { // Hooray! We fast-rejected on this hash. returnfalse;
}
}
// Now do the slighty-more-complex work of synthesizing the fourth hash, // and check it against the filter if it exists. let fourth = hashes.fourth_hash();
fourth == 0 || bf.might_contain_hash(fourth)
}
/// A result of selector matching, includes 3 failure types, /// /// NotMatchedAndRestartFromClosestLaterSibling /// NotMatchedAndRestartFromClosestDescendant /// NotMatchedGlobally /// /// When NotMatchedGlobally appears, stop selector matching completely since /// the succeeding selectors never matches. /// It is raised when /// Child combinator cannot find the candidate element. /// Descendant combinator cannot find the candidate element. /// /// When NotMatchedAndRestartFromClosestDescendant appears, the selector /// matching does backtracking and restarts from the closest Descendant /// combinator. /// It is raised when /// NextSibling combinator cannot find the candidate element. /// LaterSibling combinator cannot find the candidate element. /// Child combinator doesn't match on the found element. /// /// When NotMatchedAndRestartFromClosestLaterSibling appears, the selector /// matching does backtracking and restarts from the closest LaterSibling /// combinator. /// It is raised when /// NextSibling combinator doesn't match on the found element. /// /// For example, when the selector "d1 d2 a" is provided and we cannot *find* /// an appropriate ancestor element for "d1", this selector matching raises /// NotMatchedGlobally since even if "d2" is moved to more upper element, the /// candidates for "d1" becomes less than before and d1 . /// /// The next example is siblings. When the selector "b1 + b2 ~ d1 a" is /// provided and we cannot *find* an appropriate brother element for b1, /// the selector matching raises NotMatchedAndRestartFromClosestDescendant. /// The selectors ("b1 + b2 ~") doesn't match and matching restart from "d1". /// /// The additional example is child and sibling. When the selector /// "b1 + c1 > b2 ~ d1 a" is provided and the selector "b1" doesn't match on /// the element, this "b1" raises NotMatchedAndRestartFromClosestLaterSibling. /// However since the selector "c1" raises /// NotMatchedAndRestartFromClosestDescendant. So the selector /// "b1 + c1 > b2 ~ " doesn't match and restart matching from "d1". /// /// There is also the unknown result, which is used during invalidation when /// specific selector is being tested for before/after comparison. More specifically, /// selectors that are too expensive to correctly compute during invalidation may /// return unknown, as the computation will be thrown away and only to be recomputed /// during styling. For most cases, the unknown result can be treated as matching. /// This is because a compound of selectors acts like &&, and unknown && matched /// == matched and unknown && not-matched == not-matched. However, some selectors, /// like `:is()`, behave like || i.e. `:is(.a, .b)` == a || b. Treating unknown /// == matching then causes these selectors to always return matching, which undesired /// for before/after comparison. Coercing to not-matched doesn't work since each /// inner selector may have compounds: e.g. Toggling `.foo` in `:is(.foo:has(..))` /// with coersion to not-matched would result in an invalid before/after comparison /// of not-matched/not-matched. #[derive(Clone, Copy, Eq, PartialEq)] enum SelectorMatchingResult {
Matched,
NotMatchedAndRestartFromClosestLaterSibling,
NotMatchedAndRestartFromClosestDescendant,
NotMatchedGlobally,
Unknown,
}
/// Matches a selector, fast-rejecting against a bloom filter. /// /// We accept an offset to allow consumers to represent and match against /// partial selectors (indexed from the right). We use this API design, rather /// than having the callers pass a SelectorIter, because creating a SelectorIter /// requires dereferencing the selector to get the length, which adds an /// unnecessary cache miss for cases when we can fast-reject with AncestorHashes /// (which the caller can store inline with the selector pointer). #[inline(always)] pubfn matches_selector<E>(
selector: &Selector<E::Impl>,
offset: usize,
hashes: Option<&AncestorHashes>,
element: &E,
context: &mut MatchingContext<E::Impl>,
) -> bool where
E: Element,
{ let result = matches_selector_kleene(selector, offset, hashes, element, context); if cfg!(debug_assertions) && result == KleeneValue::Unknown {
debug_assert!(
context
.matching_for_invalidation_comparison()
.unwrap_or(false), "How did we return unknown?"
);
}
result.to_bool(true)
}
/// Same as matches_selector, but returns the Kleene value as-is. #[inline(always)] pubfn matches_selector_kleene<E>(
selector: &Selector<E::Impl>,
offset: usize,
hashes: Option<&AncestorHashes>,
element: &E,
context: &mut MatchingContext<E::Impl>,
) -> KleeneValue where
E: Element,
{ // Use the bloom filter to fast-reject. iflet Some(hashes) = hashes { iflet Some(filter) = context.bloom_filter { if !selector_may_match(hashes, filter) { return KleeneValue::False;
}
}
}
matches_complex_selector(
selector.iter_from(offset),
element,
context, if selector.is_rightmost(offset) {
SubjectOrPseudoElement::Yes
} else {
SubjectOrPseudoElement::No
},
)
}
/// Whether a compound selector matched, and whether it was the rightmost /// selector inside the complex selector. pubenum CompoundSelectorMatchingResult { /// The selector was fully matched.
FullyMatched, /// The compound selector matched, and the next combinator offset is /// `next_combinator_offset`.
Matched { next_combinator_offset: usize }, /// The selector didn't match.
NotMatched,
}
/// Returns true if this compound would not match the given element by due /// to a local name selector (If one exists). pubfn early_reject_by_local_name<E: Element>(
selector: &Selector<E::Impl>,
from_offset: usize,
element: &E,
) -> bool { let iter = selector.iter_from(from_offset); for component in iter { ifmatch component {
Component::LocalName(name) => !matches_local_name(element, name),
Component::Is(list) | Component::Where(list) => {
complex_selector_early_reject_by_local_name(list, element)
},
_ => continue,
} { returntrue;
}
} false
}
/// Matches a compound selector belonging to `selector`, starting at offset /// `from_offset`, matching left to right. /// /// Requires that `from_offset` points to a `Combinator`. /// /// NOTE(emilio): This doesn't allow to match in the leftmost sequence of the /// complex selector, but it happens to be the case we don't need it. pubfn matches_compound_selector_from<E>(
selector: &Selector<E::Impl>, mut from_offset: usize,
context: &mut MatchingContext<E::Impl>,
element: &E,
) -> CompoundSelectorMatchingResult where
E: Element,
{
debug_assert!(
!context
.matching_for_invalidation_comparison()
.unwrap_or(false), "CompoundSelectorMatchingResult doesn't support unknown"
); if cfg!(debug_assertions) && from_offset != 0 {
selector.combinator_at_parse_order(from_offset - 1); // This asserts.
}
letmut local_context = LocalMatchingContext {
shared: context, // We have no info if this is an outer selector. This function is called in // an invalidation context, which only calls this for non-subject (i.e. // Non-rightmost) positions.
rightmost: SubjectOrPseudoElement::No,
quirks_data: None,
};
// Find the end of the selector or the next combinator, then match // backwards, so that we match in the same order as // matches_complex_selector, which is usually faster. let start_offset = from_offset; for component in selector.iter_raw_parse_order_from(from_offset) { if matches!(*component, Component::Combinator(..)) {
debug_assert_ne!(from_offset, 0, "Selector started with a combinator?"); break;
}
let iter = selector.iter_from(selector.len() - from_offset);
debug_assert!(
iter.clone().next().is_some() || from_offset != selector.len(), "Got the math wrong: {:?} | {:?} | {} {}",
selector,
selector.iter_raw_match_order().as_slice(),
from_offset,
start_offset
);
debug_assert!(
!local_context.shared.featureless(), "Invalidating featureless element somehow?"
);
for component in iter { let result = matches_simple_selector(component, element, &mut local_context);
debug_assert!(
result != KleeneValue::Unknown, "Returned unknown in non invalidation context?"
); if !result.to_bool(true) { return CompoundSelectorMatchingResult::NotMatched;
}
}
/// Matches a complex selector. #[inline(always)] fn matches_complex_selector<E>( mut iter: SelectorIter<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> KleeneValue where
E: Element,
{ // If this is the special pseudo-element mode, consume the ::pseudo-element // before proceeding, since the caller has already handled that part. if context.matching_mode() == MatchingMode::ForStatelessPseudoElement && !context.is_nested() { // Consume the pseudo. match *iter.next().unwrap() {
Component::PseudoElement(ref pseudo) => { iflet Some(ref f) = context.pseudo_element_matching_fn { if !f(pseudo) { return KleeneValue::False;
}
}
}, ref other => {
debug_assert!( false, "Used MatchingMode::ForStatelessPseudoElement \ in a non-pseudo selector {:?}",
other
); return KleeneValue::False;
},
}
if !iter.matches_for_stateless_pseudo_element() { return KleeneValue::False;
}
// Advance to the non-pseudo-element part of the selector. let next_sequence = iter.next_sequence().unwrap();
debug_assert_eq!(next_sequence, Combinator::PseudoElement);
}
/// Matches each selector of a list as a complex selector fn matches_complex_selector_list<E: Element>(
list: &[Selector<E::Impl>],
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> KleeneValue {
KleeneValue::any(list.iter(), |selector| {
matches_complex_selector(selector.iter(), element, context, rightmost)
})
}
fn matches_relative_selector<E: Element>(
relative_selector: &RelativeSelector<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> bool { // Overall, we want to mark the path that we've traversed so that when an element // is invalidated, we early-reject unnecessary relative selector invalidations. if relative_selector.match_hint.is_descendant_direction() { if context.needs_selector_flags() {
element.apply_selector_flags(
ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR,
);
} letmut next_element = element.first_element_child(); whilelet Some(el) = next_element { if context.needs_selector_flags() {
el.apply_selector_flags(
ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR,
);
} letmut matched = matches_complex_selector(
relative_selector.selector.iter(),
&el,
context,
rightmost,
)
.to_bool(true); if !matched && relative_selector.match_hint.is_subtree() {
matched = matches_relative_selector_subtree(
&relative_selector.selector,
&el,
context,
rightmost,
);
} if matched { returntrue;
}
next_element = el.next_sibling_element();
}
} else {
debug_assert!(
matches!(
relative_selector.match_hint,
RelativeSelectorMatchHint::InNextSibling
| RelativeSelectorMatchHint::InNextSiblingSubtree
| RelativeSelectorMatchHint::InSibling
| RelativeSelectorMatchHint::InSiblingSubtree
), "Not descendant direction, but also not sibling direction?"
); if context.needs_selector_flags() {
element.apply_selector_flags(
ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING,
);
} let sibling_flag = if relative_selector.match_hint.is_subtree() {
ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR_SIBLING
} else {
ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING
}; letmut next_element = element.next_sibling_element(); whilelet Some(el) = next_element { if context.needs_selector_flags() {
el.apply_selector_flags(sibling_flag);
} let matched = if relative_selector.match_hint.is_subtree() {
matches_relative_selector_subtree(
&relative_selector.selector,
&el,
context,
rightmost,
)
} else {
matches_complex_selector(relative_selector.selector.iter(), &el, context, rightmost)
.to_bool(true)
}; if matched { returntrue;
} if relative_selector.match_hint.is_next_sibling() { break;
}
next_element = el.next_sibling_element();
}
} returnfalse;
}
fn relative_selector_match_early<E: Element>(
selector: &RelativeSelector<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
) -> Option<bool> { // See if we can return a cached result. iflet Some(cached) = context
.selector_caches
.relative_selector
.lookup(element.opaque(), selector)
{ return Some(cached.matched());
} // See if we can fast-reject. if context
.selector_caches
.relative_selector_filter_map
.fast_reject(element, selector, context.quirks_mode())
{ // Alright, add as unmatched to cache.
context.selector_caches.relative_selector.add(
element.opaque(),
selector,
RelativeSelectorCachedMatch::NotMatched,
); return Some(false);
}
None
}
fn match_relative_selectors<E: Element>(
selectors: &[RelativeSelector<E::Impl>],
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> KleeneValue { if context.relative_selector_anchor().is_some() { // FIXME(emilio): This currently can happen with nesting, and it's not fully // correct, arguably. But the ideal solution isn't super-clear either. For now, // cope with it and explicitly reject it at match time. See [1] for discussion. // // [1]: https://github.com/w3c/csswg-drafts/issues/9600 return KleeneValue::False;
} iflet Some(may_return_unknown) = context.matching_for_invalidation_comparison() { // In the context of invalidation, :has is expensive, especially because we // can't use caching/filtering due to now/then matches. DOM structure also // may have changed. returnif may_return_unknown {
KleeneValue::Unknown
} else {
KleeneValue::from(!context.in_negation())
};
}
context
.nest_for_relative_selector(element.opaque(), |context| {
do_match_relative_selectors(selectors, element, context, rightmost)
})
.into()
}
/// Matches a relative selector in a list of relative selectors. fn do_match_relative_selectors<E: Element>(
selectors: &[RelativeSelector<E::Impl>],
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> bool { // Due to style sharing implications (See style sharing code), we mark the current styling context // to mark elements considered for :has matching. Additionally, we want to mark the elements themselves, // since we don't want to indiscriminately invalidate every element as a potential anchor. if rightmost == SubjectOrPseudoElement::Yes { if context.needs_selector_flags() {
element.apply_selector_flags(ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR);
}
} else { if context.needs_selector_flags() {
element
.apply_selector_flags(ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR_NON_SUBJECT);
}
}
for relative_selector in selectors.iter() { iflet Some(result) = relative_selector_match_early(relative_selector, element, context) { if result { returntrue;
} // Early return indicates no match, continue to next selector. continue;
}
let matched = matches_relative_selector(relative_selector, element, context, rightmost);
context.selector_caches.relative_selector.add(
element.opaque(),
relative_selector, if matched {
RelativeSelectorCachedMatch::Matched
} else {
RelativeSelectorCachedMatch::NotMatched
},
); if matched { returntrue;
}
}
// This compound selector had a pseudo-element to the right that we // intentionally skipped. if rightmost == SubjectOrPseudoElement::Yes
&& context.matching_mode() == MatchingMode::ForStatelessPseudoElement
{ returnfalse;
}
let is_pseudo_combinator = combinator.is_pseudo_element(); if context.featureless() && !is_pseudo_combinator { // A featureless element shouldn't match any further combinator. // TODO(emilio): Maybe we could avoid the compound matching more eagerly. return SelectorMatchingResult::NotMatchedGlobally;
}
let is_sibling_combinator = combinator.is_sibling(); if is_sibling_combinator && context.needs_selector_flags() { // We need the flags even if we don't match.
element.apply_selector_flags(ElementSelectorFlags::HAS_SLOW_SELECTOR_LATER_SIBLINGS);
}
if matches_compound_selector == KleeneValue::False { // We don't short circuit unknown here, since the rest of the selector // to the left of this compound may still return false. return SelectorMatchingResult::NotMatchedAndRestartFromClosestLaterSibling;
}
if !is_pseudo_combinator {
rightmost = SubjectOrPseudoElement::No;
first_subject_compound = SubjectOrPseudoElement::No;
}
// Stop matching :visited as soon as we find a link, or a combinator for // something that isn't an ancestor. letmut visited_handling = if is_sibling_combinator {
VisitedHandlingMode::AllLinksUnvisited
} else {
context.visited_handling()
};
let candidate_not_found = if is_sibling_combinator {
SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant
} else {
SelectorMatchingResult::NotMatchedGlobally
};
letmut element = element.clone(); loop { if element.is_link() {
visited_handling = VisitedHandlingMode::AllLinksUnvisited;
}
let NextElement {
next_element,
featureless,
} = next_element_for_combinator(&element, combinator, &context);
element = match next_element {
None => return candidate_not_found,
Some(e) => e,
};
let result = context.with_visited_handling_mode(visited_handling, |context| {
context.with_featureless(featureless, |context| {
matches_complex_selector_internal(
selector_iter.clone(),
&element,
context,
rightmost,
first_subject_compound,
)
})
});
// Return the status immediately if it is one of the global states. match result {
SelectorMatchingResult::Matched => {
debug_assert!(
matches_compound_selector.to_bool(true), "Compound didn't match?"
); if !matches_compound_selector.to_bool(false) { return SelectorMatchingResult::Unknown;
} return result;
},
SelectorMatchingResult::Unknown | SelectorMatchingResult::NotMatchedGlobally => { return result
},
_ => {},
}
match combinator {
Combinator::Descendant => { // The Descendant combinator and the status is // NotMatchedAndRestartFromClosestLaterSibling or // NotMatchedAndRestartFromClosestDescendant, or the LaterSibling combinator and // the status is NotMatchedAndRestartFromClosestDescendant, we can continue to // matching on the next candidate element.
},
Combinator::Child => { // Upgrade the failure status to NotMatchedAndRestartFromClosestDescendant. return SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant;
},
Combinator::LaterSibling => { // If the failure status is NotMatchedAndRestartFromClosestDescendant and combinator is // LaterSibling, give up this LaterSibling matching and restart from the closest // descendant combinator. if matches!(
result,
SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant
) { return result;
}
},
Combinator::NextSibling
| Combinator::PseudoElement
| Combinator::Part
| Combinator::SlotAssignment => { // NOTE(emilio): Conceptually, PseudoElement / Part / SlotAssignment should return // `candidate_not_found`, but it doesn't matter in practice since they don't have // sibling / descendant combinators to the right of them. This hopefully saves one // branch. return result;
},
}
if featureless { // A featureless element didn't match the selector, we can stop matching now rather // than looking at following elements for our combinator. return candidate_not_found;
}
}
}
#[inline] fn matches_local_name<E>(element: &E, local_name: &LocalName<E::Impl>) -> bool where
E: Element,
{ let name = select_name(element, &local_name.name, &local_name.lower_name).borrow();
element.has_local_name(name)
}
let current_host = context.current_host; if current_host != Some(host.opaque()) { loop { let outer_host = host.containing_shadow_host(); if outer_host.as_ref().map(|h| h.opaque()) == current_host { break;
} let outer_host = match outer_host {
Some(h) => h,
None => returnfalse,
}; // TODO(emilio): if worth it, we could early return if // host doesn't have the exportparts attribute.
hosts.push(host);
host = outer_host;
}
}
// Translate the part into the right scope.
parts.iter().all(|part| { letmut part = part.clone(); for host in hosts.iter().rev() {
part = match host.imported_part(&part) {
Some(p) => p,
None => returnfalse,
};
}
element.is_part(&part)
})
}
/// There are relatively few selectors in a given compound that may match a featureless element. /// Instead of adding a check to every selector that may not match, we handle it here in an out of /// line path. pub(crate) fn compound_matches_featureless_host<Impl: SelectorImpl>(
iter: &mut SelectorIter<Impl>,
scope_matches_featureless_host: bool,
) -> MatchesFeaturelessHost { letmut matches = MatchesFeaturelessHost::Only; for component in iter { match component {
Component::Scope | Component::ImplicitScope if scope_matches_featureless_host => {}, // :host only matches featureless elements.
Component::Host(..) => {}, // Pseudo-elements are allowed to match as well.
Component::PseudoElement(..) => {}, // We allow logical pseudo-classes, but we'll fail matching of the inner selectors if // necessary.
Component::Is(ref l) | Component::Where(ref l) => { letmut any_yes = false; letmut any_no = false; for selector in l.slice() { match selector.matches_featureless_host(scope_matches_featureless_host) {
MatchesFeaturelessHost::Never => {
any_no = true;
},
MatchesFeaturelessHost::Yes => {
any_yes = true;
any_no = true;
},
MatchesFeaturelessHost::Only => {
any_yes = true;
},
}
} if !any_yes { return MatchesFeaturelessHost::Never;
} if any_no { // Potentially downgrade since we might match non-featureless elements too.
matches = MatchesFeaturelessHost::Yes;
}
},
Component::Negation(ref l) => { // For now preserving behavior, see // https://github.com/w3c/csswg-drafts/issues/10179 for existing resolutions that // tweak this behavior. for selector in l.slice() { if selector.matches_featureless_host(scope_matches_featureless_host)
!= MatchesFeaturelessHost::Only
{ return MatchesFeaturelessHost::Never;
}
}
}, // Other components don't match the host scope.
_ => return MatchesFeaturelessHost::Never,
}
}
matches
}
/// Determines whether the given element matches the given compound selector. #[inline] fn matches_compound_selector<E>(
selector_iter: &mut SelectorIter<E::Impl>,
element: &E,
context: &mut MatchingContext<E::Impl>,
rightmost: SubjectOrPseudoElement,
) -> KleeneValue where
E: Element,
{ if context.featureless()
&& compound_matches_featureless_host(
&mut selector_iter.clone(), /* scope_matches_featureless_host = */ true,
) == MatchesFeaturelessHost::Never
{ return KleeneValue::False;
} let quirks_data = if context.quirks_mode() == QuirksMode::Quirks {
Some(selector_iter.clone())
} else {
None
}; letmut local_context = LocalMatchingContext {
shared: context,
rightmost,
quirks_data,
};
KleeneValue::any_false(selector_iter, |simple| {
matches_simple_selector(simple, element, &mut local_context)
})
}
let NthSelectorData { ty, an_plus_b, .. } = *nth_data; let is_of_type = ty.is_of_type(); if ty.is_only() {
debug_assert!(
!has_selectors, ":only-child and :only-of-type cannot have a selector list!"
); return KleeneValue::from(
matches_generic_nth_child(
element,
context,
&NthSelectorData::first(is_of_type),
selectors,
rightmost,
)
.to_bool(true)
&& matches_generic_nth_child(
element,
context,
&NthSelectorData::last(is_of_type),
selectors,
rightmost,
)
.to_bool(true),
);
}
let is_from_end = ty.is_from_end();
// It's useful to know whether this can only select the first/last element // child for optimization purposes, see the `HAS_EDGE_CHILD_SELECTOR` flag. let is_edge_child_selector = nth_data.is_simple_edge() && !has_selectors;
if !selectors_match { return KleeneValue::False;
}
// :first/last-child are rather trivial to match, don't bother with the // cache. if is_edge_child_selector { returnif is_from_end {
element.next_sibling_element()
} else {
element.prev_sibling_element()
}
.is_none()
.into();
}
// Lookup or compute the index. let index = iflet Some(i) = context
.nth_index_cache(is_of_type, is_from_end, selectors)
.lookup(element.opaque())
{
i
} else { let i = nth_child_index(
element,
context,
selectors,
is_of_type,
is_from_end, /* check_cache = */ true,
rightmost,
);
context
.nth_index_cache(is_of_type, is_from_end, selectors)
.insert(element.opaque(), i);
i
};
debug_assert_eq!(
index,
nth_child_index(
element,
context,
selectors,
is_of_type,
is_from_end, /* check_cache = */ false,
rightmost,
), "invalid cache"
);
an_plus_b.matches_index(index).into()
}
#[inline] fn nth_child_index<E>(
element: &E,
context: &mut MatchingContext<E::Impl>,
selectors: &[Selector<E::Impl>],
is_of_type: bool,
is_from_end: bool,
check_cache: bool,
rightmost: SubjectOrPseudoElement,
) -> i32 where
E: Element,
{ // The traversal mostly processes siblings left to right. So when we walk // siblings to the right when computing NthLast/NthLastOfType we're unlikely // to get cache hits along the way. As such, we take the hit of walking the // siblings to the left checking the cache in the is_from_end case (this // matches what Gecko does). The indices-from-the-left is handled during the // regular look further below. if check_cache
&& is_from_end
&& !context
.nth_index_cache(is_of_type, is_from_end, selectors)
.is_empty()
{ letmut index: i32 = 1; letmut curr = element.clone(); whilelet Some(e) = curr.prev_sibling_element() {
curr = e; let matches = if is_of_type {
element.is_same_type(&curr)
} elseif !selectors.is_empty() {
matches_complex_selector_list(selectors, &curr, context, rightmost).to_bool(true)
} else { true
}; if !matches { continue;
} iflet Some(i) = context
.nth_index_cache(is_of_type, is_from_end, selectors)
.lookup(curr.opaque())
{ return i - index;
}
index += 1;
}
}
letmut index: i32 = 1; letmut curr = element.clone(); let next = |e: E| { if is_from_end {
e.next_sibling_element()
} else {
e.prev_sibling_element()
}
}; whilelet Some(e) = next(curr) {
curr = e; let matches = if is_of_type {
element.is_same_type(&curr)
} elseif !selectors.is_empty() {
matches_complex_selector_list(selectors, &curr, context, rightmost).to_bool(true)
} else { true
}; if !matches { continue;
} // If we're computing indices from the left, check each element in the // cache. We handle the indices-from-the-right case at the top of this // function. if !is_from_end && check_cache { iflet Some(i) = context
.nth_index_cache(is_of_type, is_from_end, selectors)
.lookup(curr.opaque())
{ return i + index;
}
}
index += 1;
}
index
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.27 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.