Revert "Backport several style changes from Gecko (5) (#30099)" (#30104)

This reverts commit 8e15389cae.
This commit is contained in:
Oriol Brufau 2023-08-16 08:24:42 +02:00 committed by GitHub
parent 8e15389cae
commit d6ae8dc112
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
152 changed files with 4622 additions and 5862 deletions

View file

@ -28,9 +28,8 @@ use crate::selector_parser::{PerPseudoElementMap, PseudoElement, SelectorImpl, S
use crate::shared_lock::{Locked, SharedRwLockReadGuard, StylesheetGuards};
use crate::stylesheet_set::{DataValidity, DocumentStylesheetSet, SheetRebuildKind};
use crate::stylesheet_set::{DocumentStylesheetFlusher, SheetCollectionFlusher};
use crate::stylesheets::container_rule::ContainerCondition;
use crate::stylesheets::keyframes_rule::KeyframesAnimation;
use crate::stylesheets::layer_rule::{LayerName, LayerOrder};
use crate::stylesheets::layer_rule::{LayerId, LayerName, LayerOrder};
use crate::stylesheets::viewport_rule::{self, MaybeNew, ViewportRule};
#[cfg(feature = "gecko")]
use crate::stylesheets::{
@ -40,6 +39,7 @@ use crate::stylesheets::{
CssRule, EffectiveRulesIterator, Origin, OriginSet, PageRule, PerOrigin, PerOriginIter,
};
use crate::stylesheets::{StyleRule, StylesheetContents, StylesheetInDocument};
use crate::thread_state::{self, ThreadState};
use crate::AllocErr;
use crate::{Atom, LocalName, Namespace, ShrinkIfNeeded, WeakAtom};
use fxhash::FxHashMap;
@ -49,7 +49,7 @@ use malloc_size_of::MallocUnconditionalShallowSizeOf;
use selectors::attr::{CaseSensitivity, NamespaceConstraint};
use selectors::bloom::BloomFilter;
use selectors::matching::VisitedHandlingMode;
use selectors::matching::{matches_selector, MatchingContext, MatchingMode, NeedsSelectorFlags};
use selectors::matching::{matches_selector, ElementSelectorFlags, MatchingContext, MatchingMode};
use selectors::parser::{AncestorHashes, Combinator, Component, Selector, SelectorIter};
use selectors::visitor::SelectorVisitor;
use selectors::NthIndexCache;
@ -549,47 +549,6 @@ impl From<StyleRuleInclusion> for RuleInclusion {
}
}
/// A struct containing state from ancestor rules like @layer / @import /
/// @container.
struct ContainingRuleState {
layer_name: LayerName,
layer_id: LayerId,
container_condition_id: ContainerConditionId,
}
impl Default for ContainingRuleState {
fn default() -> Self {
Self {
layer_name: LayerName::new_empty(),
layer_id: LayerId::root(),
container_condition_id: ContainerConditionId::none(),
}
}
}
struct SavedContainingRuleState {
layer_name_len: usize,
layer_id: LayerId,
container_condition_id: ContainerConditionId,
}
impl ContainingRuleState {
fn save(&self) -> SavedContainingRuleState {
SavedContainingRuleState {
layer_name_len: self.layer_name.0.len(),
layer_id: self.layer_id,
container_condition_id: self.container_condition_id,
}
}
fn restore(&mut self, saved: &SavedContainingRuleState) {
debug_assert!(self.layer_name.0.len() >= saved.layer_name_len);
self.layer_name.0.truncate(saved.layer_name_len);
self.layer_id = saved.layer_id;
self.container_condition_id = saved.container_condition_id;
}
}
impl Stylist {
/// Construct a new `Stylist`, using given `Device` and `QuirksMode`.
/// If more members are added here, think about whether they should
@ -1114,12 +1073,38 @@ impl Stylist {
{
debug_assert!(pseudo.is_lazy());
// No need to bother setting the selector flags when we're computing
// default styles.
let needs_selector_flags = if rule_inclusion == RuleInclusion::DefaultOnly {
NeedsSelectorFlags::No
} else {
NeedsSelectorFlags::Yes
// Apply the selector flags. We should be in sequential mode
// already, so we can directly apply the parent flags.
let mut set_selector_flags = |element: &E, flags: ElementSelectorFlags| {
if cfg!(feature = "servo") {
// Servo calls this function from the worker, but only for internal
// pseudos, so we should never generate selector flags here.
unreachable!("internal pseudo generated slow selector flags?");
}
// No need to bother setting the selector flags when we're computing
// default styles.
if rule_inclusion == RuleInclusion::DefaultOnly {
return;
}
// Gecko calls this from sequential mode, so we can directly apply
// the flags.
debug_assert_eq!(thread_state::get(), ThreadState::LAYOUT);
let self_flags = flags.for_self();
if !self_flags.is_empty() {
unsafe {
element.set_selector_flags(self_flags);
}
}
let parent_flags = flags.for_parent();
if !parent_flags.is_empty() {
if let Some(p) = element.parent_element() {
unsafe {
p.set_selector_flags(parent_flags);
}
}
}
};
let mut declarations = ApplicableDeclarationList::new();
@ -1128,7 +1113,6 @@ impl Stylist {
None,
None,
self.quirks_mode,
needs_selector_flags,
);
matching_context.pseudo_element_matching_fn = matching_fn;
@ -1142,6 +1126,7 @@ impl Stylist {
rule_inclusion,
&mut declarations,
&mut matching_context,
&mut set_selector_flags,
);
if declarations.is_empty() && is_probe {
@ -1159,7 +1144,6 @@ impl Stylist {
None,
VisitedHandlingMode::RelevantLinkVisited,
self.quirks_mode,
needs_selector_flags,
);
matching_context.pseudo_element_matching_fn = matching_fn;
@ -1172,6 +1156,7 @@ impl Stylist {
rule_inclusion,
&mut declarations,
&mut matching_context,
&mut set_selector_flags,
);
if !declarations.is_empty() {
let rule_node = self.rule_tree.insert_ordered_rules_with_important(
@ -1284,7 +1269,7 @@ impl Stylist {
}
/// Returns the applicable CSS declarations for the given element.
pub fn push_applicable_declarations<E>(
pub fn push_applicable_declarations<E, F>(
&self,
element: E,
pseudo_element: Option<&PseudoElement>,
@ -1294,8 +1279,10 @@ impl Stylist {
rule_inclusion: RuleInclusion,
applicable_declarations: &mut ApplicableDeclarationList,
context: &mut MatchingContext<E::Impl>,
flags_setter: &mut F,
) where
E: TElement,
F: FnMut(&E, ElementSelectorFlags),
{
RuleCollector::new(
self,
@ -1307,6 +1294,7 @@ impl Stylist {
rule_inclusion,
applicable_declarations,
context,
flags_setter,
)
.collect_all();
}
@ -1386,15 +1374,16 @@ impl Stylist {
/// Computes the match results of a given element against the set of
/// revalidation selectors.
pub fn match_revalidation_selectors<E>(
pub fn match_revalidation_selectors<E, F>(
&self,
element: E,
bloom: Option<&BloomFilter>,
nth_index_cache: &mut NthIndexCache,
needs_selector_flags: NeedsSelectorFlags,
flags_setter: &mut F,
) -> SmallBitVec
where
E: TElement,
F: FnMut(&E, ElementSelectorFlags),
{
// NB: `MatchingMode` doesn't really matter, given we don't share style
// between pseudos.
@ -1403,7 +1392,6 @@ impl Stylist {
bloom,
Some(nth_index_cache),
self.quirks_mode,
needs_selector_flags,
);
// Note that, by the time we're revalidating, we're guaranteed that the
@ -1426,6 +1414,7 @@ impl Stylist {
Some(&selector_and_hashes.hashes),
&element,
matching_context,
flags_setter,
));
true
},
@ -1448,6 +1437,7 @@ impl Stylist {
Some(&selector_and_hashes.hashes),
&element,
&mut matching_context,
flags_setter,
));
true
},
@ -2071,17 +2061,6 @@ impl PartElementAndPseudoRules {
}
}
/// The id of a given layer, a sequentially-increasing identifier.
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, PartialOrd, Ord)]
pub struct LayerId(u16);
impl LayerId {
/// The id of the root layer.
pub const fn root() -> Self {
Self(0)
}
}
#[derive(Clone, Debug, MallocSizeOf)]
struct CascadeLayer {
id: LayerId,
@ -2099,35 +2078,6 @@ impl CascadeLayer {
}
}
/// The id of a given container condition, a sequentially-increasing identifier
/// for a given style set.
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, PartialOrd, Ord)]
pub struct ContainerConditionId(u16);
impl ContainerConditionId {
/// A special id that represents no container rule all.
pub const fn none() -> Self {
Self(0)
}
}
#[derive(Clone, Debug, MallocSizeOf)]
struct ContainerConditionReference {
parent: ContainerConditionId,
#[ignore_malloc_size_of = "Arc"]
condition: Option<Arc<ContainerCondition>>,
}
impl ContainerConditionReference {
const fn none() -> Self {
Self {
parent: ContainerConditionId::none(),
condition: None,
}
}
}
/// Data resulting from performing the CSS cascade that is specific to a given
/// origin.
///
@ -2199,9 +2149,6 @@ pub struct CascadeData {
/// The list of cascade layers, indexed by their layer id.
layers: SmallVec<[CascadeLayer; 1]>,
/// The list of container conditions, indexed by their id.
container_conditions: SmallVec<[ContainerConditionReference; 1]>,
/// Effective media query results cached from the last rebuild.
effective_media_query_results: EffectiveMediaQueryResults,
@ -2244,7 +2191,6 @@ impl CascadeData {
animations: Default::default(),
layer_id: Default::default(),
layers: smallvec::smallvec![CascadeLayer::root()],
container_conditions: smallvec::smallvec![ContainerConditionReference::none()],
extra_data: ExtraStyleData::default(),
effective_media_query_results: EffectiveMediaQueryResults::new(),
rules_source_order: 0,
@ -2359,23 +2305,6 @@ impl CascadeData {
self.layers[id.0 as usize].order
}
pub(crate) fn container_condition_matches<E>(&self, mut id: ContainerConditionId, stylist: &Stylist, element: E) -> bool
where
E: TElement,
{
loop {
let condition_ref = &self.container_conditions[id.0 as usize];
let condition = match condition_ref.condition {
None => return true,
Some(ref c) => c,
};
if !condition.matches(stylist.device(), element) {
return false;
}
id = condition_ref.parent;
}
}
fn did_finish_rebuild(&mut self) {
self.shrink_maps_if_needed();
self.compute_layer_order();
@ -2497,7 +2426,8 @@ impl CascadeData {
stylesheet: &S,
guard: &SharedRwLockReadGuard,
rebuild_kind: SheetRebuildKind,
containing_rule_state: &mut ContainingRuleState,
mut current_layer: &mut LayerName,
current_layer_id: LayerId,
mut precomputed_pseudo_element_decls: Option<&mut PrecomputedPseudoElementDeclarations>,
) -> Result<(), AllocErr>
where
@ -2520,7 +2450,7 @@ impl CascadeData {
if pseudo.is_precomputed() {
debug_assert!(selector.is_universal());
debug_assert_eq!(stylesheet.contents().origin, Origin::UserAgent);
debug_assert_eq!(containing_rule_state.layer_id, LayerId::root());
debug_assert_eq!(current_layer_id, LayerId::root());
precomputed_pseudo_element_decls
.as_mut()
@ -2547,8 +2477,7 @@ impl CascadeData {
hashes,
locked.clone(),
self.rules_source_order,
containing_rule_state.layer_id,
containing_rule_state.container_condition_id,
current_layer_id,
);
if rebuild_kind.should_rebuild_invalidation() {
@ -2626,7 +2555,7 @@ impl CascadeData {
self.animations.try_insert_with(
name,
animation,
containing_rule_state.layer_id,
current_layer_id,
compare_keyframes_in_same_layer,
)?;
},
@ -2635,35 +2564,25 @@ impl CascadeData {
// Note: Bug 1733260: we may drop @scroll-timeline rule once this spec issue
// https://github.com/w3c/csswg-drafts/issues/6674 gets landed.
self.extra_data
.add_scroll_timeline(guard, rule, containing_rule_state.layer_id)?;
.add_scroll_timeline(guard, rule, current_layer_id)?;
},
#[cfg(feature = "gecko")]
CssRule::FontFace(ref rule) => {
// NOTE(emilio): We don't care about container_condition_id
// because:
//
// Global, name-defining at-rules such as @keyframes or
// @font-face or @layer that are defined inside container
// queries are not constrained by the container query
// conditions.
//
// https://drafts.csswg.org/css-contain-3/#container-rule
// (Same elsewhere)
self.extra_data.add_font_face(rule, containing_rule_state.layer_id);
self.extra_data.add_font_face(rule, current_layer_id);
},
#[cfg(feature = "gecko")]
CssRule::FontFeatureValues(ref rule) => {
self.extra_data
.add_font_feature_values(rule, containing_rule_state.layer_id);
.add_font_feature_values(rule, current_layer_id);
},
#[cfg(feature = "gecko")]
CssRule::CounterStyle(ref rule) => {
self.extra_data
.add_counter_style(guard, rule, containing_rule_state.layer_id)?;
.add_counter_style(guard, rule, current_layer_id)?;
},
#[cfg(feature = "gecko")]
CssRule::Page(ref rule) => {
self.extra_data.add_page(guard, rule, containing_rule_state.layer_id)?;
self.extra_data.add_page(guard, rule, current_layer_id)?;
},
CssRule::Viewport(..) => {},
_ => {
@ -2704,7 +2623,7 @@ impl CascadeData {
if let Some(id) = data.layer_id.get(layer) {
return *id;
}
let id = LayerId(data.layers.len() as u16);
let id = LayerId(data.layers.len() as u32);
let parent_layer_id = if layer.layer_names().len() > 1 {
let mut parent = layer.clone();
@ -2735,8 +2654,9 @@ impl CascadeData {
fn maybe_register_layers(
data: &mut CascadeData,
name: Option<&LayerName>,
containing_rule_state: &mut ContainingRuleState,
) {
current_layer: &mut LayerName,
pushed_layers: &mut usize,
) -> LayerId {
let anon_name;
let name = match name {
Some(name) => name,
@ -2745,14 +2665,19 @@ impl CascadeData {
&anon_name
},
};
let mut id = LayerId::root();
for name in name.layer_names() {
containing_rule_state.layer_name.0.push(name.clone());
containing_rule_state.layer_id = maybe_register_layer(data, &containing_rule_state.layer_name);
current_layer.0.push(name.clone());
id = maybe_register_layer(data, &current_layer);
*pushed_layers += 1;
}
debug_assert_ne!(containing_rule_state.layer_id, LayerId::root());
debug_assert_ne!(id, LayerId::root());
id
}
let saved_containing_rule_state = containing_rule_state.save();
let mut layer_names_to_pop = 0;
let mut children_layer_id = current_layer_id;
match *rule {
CssRule::Import(ref lock) => {
let import_rule = lock.read_with(guard);
@ -2761,10 +2686,11 @@ impl CascadeData {
.saw_effective(import_rule);
}
if let Some(ref layer) = import_rule.layer {
maybe_register_layers(
children_layer_id = maybe_register_layers(
self,
layer.name.as_ref(),
containing_rule_state
&mut current_layer,
&mut layer_names_to_pop,
);
}
},
@ -2776,29 +2702,25 @@ impl CascadeData {
},
CssRule::LayerBlock(ref lock) => {
let layer_rule = lock.read_with(guard);
maybe_register_layers(
children_layer_id = maybe_register_layers(
self,
layer_rule.name.as_ref(),
containing_rule_state,
&mut current_layer,
&mut layer_names_to_pop,
);
},
CssRule::LayerStatement(ref lock) => {
let layer_rule = lock.read_with(guard);
for name in &*layer_rule.names {
maybe_register_layers(self, Some(name), containing_rule_state);
// Register each layer individually.
containing_rule_state.restore(&saved_containing_rule_state);
let mut pushed = 0;
// There are no children, so we can ignore the
// return value.
maybe_register_layers(self, Some(name), &mut current_layer, &mut pushed);
for _ in 0..pushed {
current_layer.0.pop();
}
}
},
CssRule::Container(ref lock) => {
let container_rule = lock.read_with(guard);
let id = ContainerConditionId(self.container_conditions.len() as u16);
self.container_conditions.push(ContainerConditionReference {
parent: containing_rule_state.container_condition_id,
condition: Some(container_rule.condition.clone()),
});
containing_rule_state.container_condition_id = id;
},
// We don't care about any other rule.
_ => {},
}
@ -2811,12 +2733,15 @@ impl CascadeData {
stylesheet,
guard,
rebuild_kind,
containing_rule_state,
current_layer,
children_layer_id,
precomputed_pseudo_element_decls.as_deref_mut(),
)?;
}
containing_rule_state.restore(&saved_containing_rule_state);
for _ in 0..layer_names_to_pop {
current_layer.0.pop();
}
}
Ok(())
@ -2845,7 +2770,7 @@ impl CascadeData {
self.effective_media_query_results.saw_effective(contents);
}
let mut state = ContainingRuleState::default();
let mut current_layer = LayerName::new_empty();
self.add_rule_list(
contents.rules(guard).iter(),
device,
@ -2853,7 +2778,8 @@ impl CascadeData {
stylesheet,
guard,
rebuild_kind,
&mut state,
&mut current_layer,
LayerId::root(),
precomputed_pseudo_element_decls.as_deref_mut(),
)?;
@ -2901,7 +2827,6 @@ impl CascadeData {
CssRule::Style(..) |
CssRule::Namespace(..) |
CssRule::FontFace(..) |
CssRule::Container(..) |
CssRule::CounterStyle(..) |
CssRule::Supports(..) |
CssRule::Keyframes(..) |
@ -2979,8 +2904,6 @@ impl CascadeData {
self.layer_id.clear();
self.layers.clear();
self.layers.push(CascadeLayer::root());
self.container_conditions.clear();
self.container_conditions.push(ContainerConditionReference::none());
#[cfg(feature = "gecko")]
{
self.extra_data.clear();
@ -3075,9 +2998,6 @@ pub struct Rule {
/// The current layer id of this style rule.
pub layer_id: LayerId,
/// The current @container rule id.
pub container_condition_id: ContainerConditionId,
/// The actual style rule.
#[cfg_attr(
feature = "gecko",
@ -3123,7 +3043,6 @@ impl Rule {
style_rule: Arc<Locked<StyleRule>>,
source_order: u32,
layer_id: LayerId,
container_condition_id: ContainerConditionId,
) -> Self {
Rule {
selector,
@ -3131,17 +3050,10 @@ impl Rule {
style_rule,
source_order,
layer_id,
container_condition_id,
}
}
}
// The size of this is critical to performance on the bloom-basic
// microbenchmark.
// When iterating over a large Rule array, we want to be able to fast-reject
// selectors (with the inline hashes) with as few cache misses as possible.
size_of_test!(Rule, 40);
/// A function to be able to test the revalidation stuff.
pub fn needs_revalidation_for_testing(s: &Selector<SelectorImpl>) -> bool {
let mut attribute_dependencies = Default::default();