Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions selectors/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::attr::{
AttrSelectorOperation, AttrSelectorWithOptionalNamespace, CaseSensitivity, NamespaceConstraint,
ParsedAttrSelectorOperation, ParsedCaseSensitivity,
};
use crate::bloom::{BloomFilter, BLOOM_HASH_MASK};
use crate::bloom::{BLOOM_HASH_MASK, BloomFilter};
use crate::kleene_value::KleeneValue;
use crate::parser::{
AncestorHashes, Combinator, Component, LocalName, MatchesFeaturelessHost, NthSelectorData,
Expand Down Expand Up @@ -85,6 +85,9 @@ bitflags! {
Self::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING.bits() |
Self::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR.bits();

/// A shadow :host-context() match inspected this element.
const HOST_CONTEXT_DEPENDENCY = 1 << 10;

/// 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;
Expand All @@ -97,6 +100,7 @@ impl ElementSelectorFlags {
self & (ElementSelectorFlags::HAS_EMPTY_SELECTOR
| ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR
| ElementSelectorFlags::ANCHORS_RELATIVE_SELECTOR_NON_SUBJECT
| ElementSelectorFlags::HOST_CONTEXT_DEPENDENCY
| ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_SIBLING
| ElementSelectorFlags::RELATIVE_SELECTOR_SEARCH_DIRECTION_ANCESTOR)
}
Expand Down Expand Up @@ -805,9 +809,11 @@ fn assigned_slot<E>(element: &E, context: &MatchingContext<E::Impl>) -> Option<E
where
E: Element,
{
debug_assert!(element
.assigned_slot()
.map_or(true, |s| s.is_html_slot_element()));
debug_assert!(
element
.assigned_slot()
.map_or(true, |s| s.is_html_slot_element())
);
let scope = context.current_host?;
let mut current_slot = element.assigned_slot()?;
while current_slot.containing_shadow_host().unwrap().opaque() != scope {
Expand Down Expand Up @@ -980,7 +986,7 @@ where
return result;
},
SelectorMatchingResult::Unknown | SelectorMatchingResult::NotMatchedGlobally => {
return result
return result;
},
_ => {},
}
Expand Down Expand Up @@ -1131,6 +1137,9 @@ where
let mut result = KleeneValue::False;
let mut current = Some(element.clone());
while let Some(candidate) = current {
if context.needs_selector_flags() {
candidate.apply_selector_flags(ElementSelectorFlags::HOST_CONTEXT_DEPENDENCY);
}
let matched = context.nest(|context| {
context.with_shadow_host(None::<E>, |context| {
context.with_featureless(false, |context| {
Expand Down Expand Up @@ -1410,17 +1419,17 @@ where
nth_of_data.selectors(),
rightmost,
)
})
});
},
Component::Is(ref list) | Component::Where(ref list) => {
return context.shared.nest(|context| {
matches_complex_selector_list(list.slice(), element, context, rightmost)
})
});
},
Component::Negation(ref list) => {
return context.shared.nest_for_negation(|context| {
!matches_complex_selector_list(list.slice(), element, context, rightmost)
})
});
},
Component::Has(ref relative_selectors) => {
return match_relative_selectors(
Expand Down
1 change: 1 addition & 0 deletions style/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ doctest = false

[features]
default = ["servo"]
moli = ["servo"]
gecko = [
"bindgen",
"malloc_size_of/gecko",
Expand Down
9 changes: 9 additions & 0 deletions style/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,15 @@ pub trait TElement:
/// triggering since their styles are resolved lazily during painting.
fn note_highlight_pseudo_style_invalidated(&self) {}

/// Records invalidation for a demand-driven embedder. An element's style
/// data can be borrowed when this is called, so only record the work here.
#[cfg(feature = "moli")]
fn note_style_invalidation(
&self,
_hint: crate::invalidation::element::restyle_hints::RestyleHint,
) {
}

/// The shadow root this element is a host of.
fn shadow_root(&self) -> Option<<Self::ConcreteNode as TNode>::ConcreteShadowRoot>;

Expand Down
129 changes: 7 additions & 122 deletions style/invalidation/element/invalidation_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,29 +328,8 @@ pub struct InvalidationMap {
pub document_state_selectors: Vec<DocumentStateDependency>,
/// A map of other attribute affecting selectors.
pub other_attribute_affecting_selectors: LocalNameDependencyMap,
/// A map of local-name dependencies used by Moli's external
/// invalidation consumer.
///
/// Servo's own normal restyle flow does not need this for element type
/// changes because element type is immutable. Moli consumes the
/// dependency map for child-list insertion/removal cache invalidation,
/// where the inserted/removed element's type is a useful key.
pub type_to_selector: LocalNameDependencyMap,
/// Normal dependencies for universal selectors.
///
/// Servo's own restyle flow handles universal selector dependencies through
/// its broader child-list restyle ranges. Moli consumes invalidation
/// maps as an external cache-invalidation oracle, so it needs to query
/// universal dependencies explicitly for child-list insertion/removal.
pub any_to_selector: AnyDependencyMap,
/// A map of CSS custom states
pub custom_state_affecting_selectors: CustomStateDependencyMap,
/// Whether this map contains a sibling-sensitive dependency that is not
/// keyed by id/class/attribute/state. Normal invalidation does not need
/// type/universal keys for Servo's own restyle flow, but embedders that
/// consume dependency summaries need to know that DOM child-list mutations
/// still require later-sibling fallback.
pub unkeyed_sibling_dependency: bool,
}

/// Tree-structural pseudoclasses that we care about for (Relative selector) invalidation.
Expand Down Expand Up @@ -469,10 +448,7 @@ impl InvalidationMap {
state_affecting_selectors: StateDependencyMap::new(),
document_state_selectors: Vec::new(),
other_attribute_affecting_selectors: LocalNameDependencyMap::default(),
type_to_selector: LocalNameDependencyMap::default(),
any_to_selector: SmallVec::default(),
custom_state_affecting_selectors: CustomStateDependencyMap::default(),
unkeyed_sibling_dependency: false,
}
}

Expand All @@ -484,11 +460,6 @@ impl InvalidationMap {
.other_attribute_affecting_selectors
.iter()
.fold(0, |accum, (_, v)| accum + v.len())
+ self
.type_to_selector
.iter()
.fold(0, |accum, (_, ref v)| accum + v.len())
+ self.any_to_selector.len()
+ self
.id_to_selector
.iter()
Expand All @@ -510,10 +481,7 @@ impl InvalidationMap {
self.state_affecting_selectors.clear();
self.document_state_selectors.clear();
self.other_attribute_affecting_selectors.clear();
self.type_to_selector.clear();
self.any_to_selector.clear();
self.custom_state_affecting_selectors.clear();
self.unkeyed_sibling_dependency = false;
}

/// Shrink the capacity of hash maps if needed.
Expand All @@ -522,8 +490,6 @@ impl InvalidationMap {
self.id_to_selector.shrink_if_needed();
self.state_affecting_selectors.shrink_if_needed();
self.other_attribute_affecting_selectors.shrink_if_needed();
self.type_to_selector.shrink_if_needed();
self.any_to_selector.shrink_to_fit();
self.custom_state_affecting_selectors.shrink_if_needed();
}
}
Expand Down Expand Up @@ -730,10 +696,7 @@ fn add_local_name(
}

fn on_pseudo_class<C: Collector>(pc: &NonTSPseudoClass, collector: &mut C) -> Result<(), AllocErr> {
collector.update_states(
moli_state_flag_for_pseudo_class(pc),
pc.document_state_flag(),
);
collector.update_states(pc.state_flag(), pc.document_state_flag());

let attr_name = match *pc {
#[cfg(feature = "gecko")]
Expand All @@ -758,20 +721,6 @@ fn on_pseudo_class<C: Collector>(pc: &NonTSPseudoClass, collector: &mut C) -> Re
add_attr_dependency(attr_name, collector)
}

fn moli_state_flag_for_pseudo_class(pc: &NonTSPseudoClass) -> ElementState {
match *pc {
NonTSPseudoClass::Checked => ElementState::CHECKED,
NonTSPseudoClass::Default => ElementState::DEFAULT,
NonTSPseudoClass::InRange => ElementState::INRANGE,
NonTSPseudoClass::Indeterminate => ElementState::INDETERMINATE,
NonTSPseudoClass::Invalid => ElementState::INVALID,
NonTSPseudoClass::OutOfRange => ElementState::OUTOFRANGE,
NonTSPseudoClass::PlaceholderShown => ElementState::PLACEHOLDER_SHOWN,
NonTSPseudoClass::Valid => ElementState::VALID,
_ => pc.state_flag(),
}
}

fn add_pseudo_class_dependency<C: Collector>(
element_state: ElementState,
quirks_mode: QuirksMode,
Expand Down Expand Up @@ -1007,13 +956,9 @@ impl<'a, 'b, 'c> Collector for SelectorDependencyCollector<'a, 'b, 'c> {
}

fn type_map(&mut self) -> &mut LocalNameDependencyMap {
if self.relative_inner_collector.is_none() {
&mut self.map.type_to_selector
} else {
&mut self
.additional_relative_selector_invalidation_map
.type_to_selector
}
&mut self
.additional_relative_selector_invalidation_map
.type_to_selector
}

fn ts_state_map(&mut self) -> &mut TSStateDependencyMap {
Expand All @@ -1027,13 +972,9 @@ impl<'a, 'b, 'c> Collector for SelectorDependencyCollector<'a, 'b, 'c> {
}

fn any_vec(&mut self) -> &mut AnyDependencyMap {
if self.relative_inner_collector.is_none() {
&mut self.map.any_to_selector
} else {
&mut self
.additional_relative_selector_invalidation_map
.any_to_selector
}
&mut self
.additional_relative_selector_invalidation_map
.any_to_selector
}
}

Expand Down Expand Up @@ -1073,8 +1014,6 @@ impl<'a, 'b, 'c> SelectorDependencyCollector<'a, 'b, 'c> {
return false;
}

self.note_unkeyed_sibling_dependency_if_needed();

if let Some(state) = self
.relative_inner_collector
.as_ref()
Expand Down Expand Up @@ -1107,60 +1046,6 @@ impl<'a, 'b, 'c> SelectorDependencyCollector<'a, 'b, 'c> {
}
}

impl<'a, 'b, 'c> SelectorDependencyCollector<'a, 'b, 'c> {
fn note_unkeyed_sibling_dependency_if_needed(&mut self) {
if self.relative_inner_collector.is_some() || self.compound_state.added_entry {
return;
}
let dependency = self.dependency();
if moli_dependency_is_sibling_sensitive(&dependency) {
if !self.note_type_sibling_dependency_if_possible(dependency.clone())
&& self.alloc_error.is_none()
{
self.note_universal_sibling_dependency(dependency);
}
}
}

fn note_universal_sibling_dependency(&mut self, dependency: Dependency) {
if let Err(err) = self.any_vec().try_reserve(1) {
*self.alloc_error = Some(err.into());
return;
}
self.any_vec().push(dependency);
}

fn note_type_sibling_dependency_if_possible(&mut self, dependency: Dependency) -> bool {
let mut local_names = SmallVec::<[LocalName; 2]>::new();
for ss in self.selector.iter_from(self.compound_state.offset) {
if let Component::LocalName(name) = ss {
local_names.push(name.name.clone());
if name.name != name.lower_name {
local_names.push(name.lower_name.clone());
}
break;
}
}
if local_names.is_empty() {
return false;
}
for local_name in local_names {
if let Err(err) = add_local_name(local_name, dependency.clone(), self.type_map()) {
*self.alloc_error = Some(err);
return false;
}
}
true
}
}

fn moli_dependency_is_sibling_sensitive(dependency: &Dependency) -> bool {
match dependency.invalidation_kind() {
DependencyInvalidationKind::Normal(NormalDependencyInvalidationKind::Siblings) => true,
_ => dependency.right_combinator_is_next_sibling(),
}
}

impl<'a, 'b, 'c> SelectorVisitor for SelectorDependencyCollector<'a, 'b, 'c> {
type Impl = SelectorImpl;

Expand Down
14 changes: 9 additions & 5 deletions style/invalidation/element/invalidator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use selectors::matching::matches_compound_selector_from;
use selectors::matching::{CompoundSelectorMatchingResult, MatchingContext};
use selectors::parser::{Combinator, Component, Selector, SelectorVisitor};
use selectors::{OpaqueElement, SelectorImpl};
use smallvec::{smallvec, SmallVec};
use smallvec::{SmallVec, smallvec};
use std::fmt;
use std::fmt::Write;

Expand Down Expand Up @@ -985,8 +985,7 @@ where
debug!(" > {:?}", invalidations);

let should_process = self.processor.should_process_descendants(self.element);

if !should_process {
if !should_process && !cfg!(feature = "moli") {
return false;
}

Expand All @@ -999,7 +998,12 @@ where

let mut any_descendant = false;

any_descendant |= self.invalidate_non_slotted_descendants(&invalidations.dom_descendants);
if should_process {
any_descendant |=
self.invalidate_non_slotted_descendants(&invalidations.dom_descendants);
}
// The host may retain observed parts and assigned nodes even when
// the originating element's ordinary DOM descendants are pruned.
any_descendant |= self.invalidate_slotted_elements(&invalidations.slotted_descendants);
any_descendant |= self.invalidate_parts(&invalidations.parts);

Expand Down Expand Up @@ -1250,7 +1254,7 @@ where
return ProcessInvalidationResult {
invalidated_self: false,
matched: false,
}
};
},
CompoundSelectorMatchingResult::FullyMatched => self.handle_fully_matched(invalidation),
CompoundSelectorMatchingResult::Matched {
Expand Down
Loading
Loading