From a6ba06ef27e04255ced6426b2712e294a730554b Mon Sep 17 00:00:00 2001 From: Ziran Sun Date: Mon, 21 Aug 2023 17:58:38 +0000 Subject: [PATCH] Bug 1840478 - Introduce stylist as a memeber for stylebuilder. r=emilio,zrhoffman Differential Revision: https://phabricator.services.mozilla.com/D186615 --- servo/components/style/properties/cascade.rs | 25 ++++++++----------- .../style/properties/declaration_block.rs | 14 +++++------ .../style/properties/properties.mako.rs | 13 ++++++++++ .../style/stylesheets/container_rule.rs | 7 +++--- servo/components/style/stylist.rs | 8 +++--- servo/components/style/values/computed/mod.rs | 6 +++-- servo/ports/geckolib/glue.rs | 10 ++++---- 7 files changed, 46 insertions(+), 37 deletions(-) diff --git a/servo/components/style/properties/cascade.rs b/servo/components/style/properties/cascade.rs index 2e6092db08a0..f86cee82d915 100644 --- a/servo/components/style/properties/cascade.rs +++ b/servo/components/style/properties/cascade.rs @@ -7,18 +7,17 @@ use crate::applicable_declarations::CascadePriority; use crate::color::AbsoluteColor; use crate::computed_value_flags::ComputedValueFlags; -use crate::context::QuirksMode; use crate::custom_properties::CustomPropertiesBuilder; use crate::dom::TElement; use crate::font_metrics::FontMetricsOrientation; use crate::logical_geometry::WritingMode; -use crate::media_queries::Device; use crate::properties::declaration_block::{DeclarationImportanceIterator, Importance}; use crate::properties::generated::{ CSSWideKeyword, ComputedValues, LonghandId, LonghandIdSet, PropertyDeclaration, PropertyDeclarationId, PropertyFlags, ShorthandsWithPropertyReferencesCache, StyleBuilder, CASCADE_PROPERTY, }; +use crate::stylist::Stylist; use crate::rule_cache::{RuleCache, RuleCacheConditions}; use crate::rule_tree::{CascadeLevel, StrongRuleNode}; use crate::selector_parser::PseudoElement; @@ -68,7 +67,7 @@ pub enum FirstLineReparenting<'a> { /// * `flags`: Various flags. /// pub fn cascade( - device: &Device, + stylist: &Stylist, pseudo: Option<&PseudoElement>, rule_node: &StrongRuleNode, guards: &StylesheetGuards, @@ -78,7 +77,6 @@ pub fn cascade( first_line_reparenting: FirstLineReparenting, visited_rules: Option<&StrongRuleNode>, cascade_input_flags: ComputedValueFlags, - quirks_mode: QuirksMode, rule_cache: Option<&RuleCache>, rule_cache_conditions: &mut RuleCacheConditions, element: Option, @@ -87,7 +85,7 @@ where E: TElement, { cascade_rules( - device, + stylist, pseudo, rule_node, guards, @@ -97,7 +95,6 @@ where first_line_reparenting, CascadeMode::Unvisited { visited_rules }, cascade_input_flags, - quirks_mode, rule_cache, rule_cache_conditions, element, @@ -188,7 +185,7 @@ impl<'a> Iterator for DeclarationIterator<'a> { } fn cascade_rules( - device: &Device, + stylist: &Stylist, pseudo: Option<&PseudoElement>, rule_node: &StrongRuleNode, guards: &StylesheetGuards, @@ -198,7 +195,6 @@ fn cascade_rules( first_line_reparenting: FirstLineReparenting, cascade_mode: CascadeMode, cascade_input_flags: ComputedValueFlags, - quirks_mode: QuirksMode, rule_cache: Option<&RuleCache>, rule_cache_conditions: &mut RuleCacheConditions, element: Option, @@ -207,7 +203,7 @@ where E: TElement, { apply_declarations( - device, + stylist, pseudo, rule_node, guards, @@ -218,7 +214,6 @@ where first_line_reparenting, cascade_mode, cascade_input_flags, - quirks_mode, rule_cache, rule_cache_conditions, element, @@ -244,7 +239,7 @@ pub enum CascadeMode<'a> { /// NOTE: This function expects the declaration with more priority to appear /// first. pub fn apply_declarations<'a, E, I>( - device: &Device, + stylist: &Stylist, pseudo: Option<&PseudoElement>, rules: &StrongRuleNode, guards: &StylesheetGuards, @@ -255,7 +250,6 @@ pub fn apply_declarations<'a, E, I>( first_line_reparenting: FirstLineReparenting, cascade_mode: CascadeMode, cascade_input_flags: ComputedValueFlags, - quirks_mode: QuirksMode, rule_cache: Option<&RuleCache>, rule_cache_conditions: &mut RuleCacheConditions, element: Option, @@ -269,6 +263,7 @@ where element.is_some() && pseudo.is_some() ); debug_assert!(layout_parent_style.is_none() || parent_style.is_some()); + let device = stylist.device(); let inherited_style = parent_style.unwrap_or(device.default_computed_values()); let mut declarations = SmallVec::<[(&_, CascadePriority); 32]>::new(); @@ -298,13 +293,14 @@ where // 1375525. StyleBuilder::new( device, + Some(stylist), parent_style, pseudo, Some(rules.clone()), custom_properties, is_root_element, ), - quirks_mode, + stylist.quirks_mode(), rule_cache_conditions, container_size_query, ); @@ -790,7 +786,7 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> { // We could call apply_declarations directly, but that'd cause // another instantiation of this function which is not great. let style = cascade_rules( - self.context.builder.device, + self.context.builder.stylist.unwrap(), self.context.builder.pseudo, visited_rules, guards, @@ -802,7 +798,6 @@ impl<'a, 'b: 'a> Cascade<'a, 'b> { // Cascade input flags don't matter for the visited style, they are // in the main (unvisited) style. Default::default(), - self.context.quirks_mode, // The rule cache doesn't care about caching :visited // styles, we cache the unvisited style instead. We still do // need to set the caching dependencies properly if present diff --git a/servo/components/style/properties/declaration_block.rs b/servo/components/style/properties/declaration_block.rs index e715be5edef8..7cef84eeeaf1 100644 --- a/servo/components/style/properties/declaration_block.rs +++ b/servo/components/style/properties/declaration_block.rs @@ -15,9 +15,9 @@ use crate::applicable_declarations::CascadePriority; use crate::context::QuirksMode; use crate::custom_properties::{self, CustomPropertiesBuilder}; use crate::error_reporting::{ContextualParseError, ParseErrorReporter}; -use crate::media_queries::Device; use crate::parser::ParserContext; use crate::properties::animated_properties::{AnimationValue, AnimationValueMap}; +use crate::stylist::Stylist; use crate::rule_tree::CascadeLevel; use crate::selector_map::PrecomputedHashSet; use crate::selector_parser::SelectorImpl; @@ -845,7 +845,7 @@ impl PropertyDeclarationBlock { dest: &mut CssStringWriter, computed_values: Option<&ComputedValues>, custom_properties_block: Option<&PropertyDeclarationBlock>, - device: &Device, + stylist: &Stylist, ) -> fmt::Result { if let Ok(shorthand) = property.as_shorthand() { return self.shorthand_to_css(shorthand, dest); @@ -864,7 +864,7 @@ impl PropertyDeclarationBlock { if let Some(block) = custom_properties_block { // FIXME(emilio): This is not super-efficient here, and all this // feels like a hack anyway... - block.cascade_custom_properties(cv.custom_properties(), device) + block.cascade_custom_properties(cv.custom_properties(), stylist) } else { cv.custom_properties().cloned() } @@ -888,7 +888,7 @@ impl PropertyDeclarationBlock { computed_values.writing_mode, custom_properties.as_ref(), QuirksMode::NoQuirks, - device, + stylist.device(), &mut Default::default(), ) .to_css(dest) @@ -935,7 +935,7 @@ impl PropertyDeclarationBlock { &self, context: &Context, ) -> Option> { - self.cascade_custom_properties(context.style().custom_properties(), context.device()) + self.cascade_custom_properties(context.style().custom_properties(), context.style().stylist.unwrap()) } /// Returns a custom properties map which is the result of cascading custom @@ -944,9 +944,9 @@ impl PropertyDeclarationBlock { fn cascade_custom_properties( &self, inherited_custom_properties: Option<&Arc>, - device: &Device, + stylist: &Stylist, ) -> Option> { - let mut builder = CustomPropertiesBuilder::new(inherited_custom_properties, device); + let mut builder = CustomPropertiesBuilder::new(inherited_custom_properties, stylist.device()); for declaration in self.normal_declaration_iter() { if let PropertyDeclaration::Custom(ref declaration) = *declaration { diff --git a/servo/components/style/properties/properties.mako.rs b/servo/components/style/properties/properties.mako.rs index 0d541e6a940d..1e0d02303311 100644 --- a/servo/components/style/properties/properties.mako.rs +++ b/servo/components/style/properties/properties.mako.rs @@ -31,6 +31,7 @@ use fxhash::FxHashMap; use crate::media_queries::Device; use crate::parser::ParserContext; use crate::selector_parser::PseudoElement; +use crate::stylist::Stylist; #[cfg(feature = "servo")] use servo_config::prefs; use style_traits::{CssWriter, KeywordsCollectFn, ParseError, ParsingMode}; use style_traits::{SpecifiedValueInfo, StyleParseErrorKind, ToCss}; @@ -2975,6 +2976,7 @@ pub struct ComputedValuesInner { ${style_struct.ident}: Arc, % endfor custom_properties: Option>, + /// The writing mode of this computed values struct. pub writing_mode: WritingMode, @@ -3594,6 +3596,10 @@ pub struct StyleBuilder<'a> { /// This provides access to viewport unit ratios, etc. pub device: &'a Device, + /// The stylist we're using to compute style except for media queries. + /// device is used in media queries instead. + pub stylist: Option<<&'a Stylist>, + /// The style we're inheriting from. /// /// This is effectively @@ -3641,6 +3647,7 @@ impl<'a> StyleBuilder<'a> { /// Trivially construct a `StyleBuilder`. pub(super) fn new( device: &'a Device, + stylist: Option<<&'a Stylist>, parent_style: Option<<&'a ComputedValues>, pseudo: Option<<&'a PseudoElement>, rules: Option, @@ -3654,6 +3661,7 @@ impl<'a> StyleBuilder<'a> { StyleBuilder { device, + stylist, inherited_style, reset_style, pseudo, @@ -3682,6 +3690,7 @@ impl<'a> StyleBuilder<'a> { /// used for animations. pub fn for_animation( device: &'a Device, + stylist: Option<<&'a Stylist>, style_to_derive_from: &'a ComputedValues, parent_style: Option<<&'a ComputedValues>, ) -> Self { @@ -3689,6 +3698,7 @@ impl<'a> StyleBuilder<'a> { let inherited_style = parent_style.unwrap_or(reset_style); StyleBuilder { device, + stylist, inherited_style, reset_style, pseudo: None, @@ -3796,6 +3806,7 @@ impl<'a> StyleBuilder<'a> { /// computed values that need to be provided as well. pub fn for_inheritance( device: &'a Device, + stylist: Option<<&'a Stylist>, parent: Option<<&'a ComputedValues>, pseudo: Option<<&'a PseudoElement>, ) -> Self { @@ -3808,6 +3819,7 @@ impl<'a> StyleBuilder<'a> { parent.visited_style().map(|style| { Self::for_inheritance( device, + stylist, Some(style), pseudo, ).build() @@ -3815,6 +3827,7 @@ impl<'a> StyleBuilder<'a> { }); let mut ret = Self::new( device, + stylist, parent, pseudo, /* rules = */ None, diff --git a/servo/components/style/stylesheets/container_rule.rs b/servo/components/style/stylesheets/container_rule.rs index f9d488b9b494..bf3753baa64e 100644 --- a/servo/components/style/stylesheets/container_rule.rs +++ b/servo/components/style/stylesheets/container_rule.rs @@ -9,9 +9,9 @@ use crate::computed_value_flags::ComputedValueFlags; use crate::dom::TElement; use crate::logical_geometry::{LogicalSize, WritingMode}; -use crate::media_queries::Device; use crate::parser::ParserContext; use crate::properties::ComputedValues; +use crate::stylist::Stylist; use crate::queries::condition::KleeneValue; use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription}; use crate::queries::values::Orientation; @@ -252,7 +252,7 @@ impl ContainerCondition { /// Tries to match a container query condition for a given element. pub(crate) fn matches( &self, - device: &Device, + stylist: &Stylist, element: E, originating_element_style: Option<&ComputedValues>, invalidation_flags: &mut ComputedValueFlags, @@ -268,7 +268,8 @@ impl ContainerCondition { // Set up the lookup for the container in question, as the condition may be using container query lengths. let size_query_container_lookup = ContainerSizeQuery::for_option_element(container, None); Context::for_container_query_evaluation( - device, + stylist.device(), + Some(stylist), info, size_query_container_lookup, |context| { diff --git a/servo/components/style/stylist.rs b/servo/components/style/stylist.rs index 4fc098b8c01b..23b0f5f5f87c 100644 --- a/servo/components/style/stylist.rs +++ b/servo/components/style/stylist.rs @@ -1103,7 +1103,7 @@ impl Stylist { // // FIXME(emilio): We should assert that it holds if pseudo.is_none()! properties::cascade::( - &self.device, + &self, pseudo, inputs.rules.as_ref().unwrap_or(self.rule_tree.root()), guards, @@ -1113,7 +1113,6 @@ impl Stylist { first_line_reparenting, visited_rules, inputs.flags, - self.quirks_mode, rule_cache, rule_cache_conditions, element, @@ -1489,7 +1488,7 @@ impl Stylist { // reversing this as it shouldn't be slow anymore, and should avoid // generating two instantiations of apply_declarations. properties::apply_declarations::( - &self.device, + &self, /* pseudo = */ None, self.rule_tree.root(), guards, @@ -1510,7 +1509,6 @@ impl Stylist { visited_rules: None, }, Default::default(), - self.quirks_mode, /* rule_cache = */ None, &mut Default::default(), /* element = */ None, @@ -2541,7 +2539,7 @@ impl CascadeData { }; let matches = condition .matches( - stylist.device(), + stylist, element, context.extra_data.originating_element_style, &mut context.extra_data.cascade_input_flags, diff --git a/servo/components/style/values/computed/mod.rs b/servo/components/style/values/computed/mod.rs index 1543bb81b6e2..ffb053fc94b6 100644 --- a/servo/components/style/values/computed/mod.rs +++ b/servo/components/style/values/computed/mod.rs @@ -22,6 +22,7 @@ use crate::media_queries::Device; use crate::properties; use crate::properties::{ComputedValues, StyleBuilder}; use crate::rule_cache::RuleCacheConditions; +use crate::stylist::Stylist; use crate::stylesheets::container_rule::{ ContainerInfo, ContainerSizeQuery, ContainerSizeQueryResult, }; @@ -215,7 +216,7 @@ impl<'a> Context<'a> { { let mut conditions = RuleCacheConditions::default(); let context = Context { - builder: StyleBuilder::for_inheritance(device, None, None), + builder: StyleBuilder::for_inheritance(device, None, None, None), cached_system_font: None, in_media_query: true, in_container_query: false, @@ -233,6 +234,7 @@ impl<'a> Context<'a> { /// specified. pub fn for_container_query_evaluation( device: &Device, + stylist: Option<&Stylist>, container_info_and_style: Option<(ContainerInfo, Arc)>, container_size_query: ContainerSizeQuery, f: F, @@ -250,7 +252,7 @@ impl<'a> Context<'a> { let style = style.as_ref().map(|s| &**s); let quirks_mode = device.quirks_mode(); let context = Context { - builder: StyleBuilder::for_inheritance(device, style, None), + builder: StyleBuilder::for_inheritance(device, stylist, style, None), cached_system_font: None, in_media_query: false, in_container_query: true, diff --git a/servo/ports/geckolib/glue.rs b/servo/ports/geckolib/glue.rs index fe110b501537..8791a211b77b 100644 --- a/servo/ports/geckolib/glue.rs +++ b/servo/ports/geckolib/glue.rs @@ -697,7 +697,7 @@ pub extern "C" fn Servo_AnimationValue_Serialize( buffer, None, None, /* No extra custom properties */ - &data.stylist.device(), + &data.stylist, ); debug_assert!(rv.is_ok()); } @@ -4135,7 +4135,7 @@ fn get_pseudo_style( } Some(style.unwrap_or_else(|| { - StyleBuilder::for_inheritance(stylist.device(), Some(styles.primary()), Some(pseudo)) + StyleBuilder::for_inheritance(stylist.device(), Some(stylist), Some(styles.primary()), Some(pseudo)) .build() })) } @@ -4154,7 +4154,7 @@ pub unsafe extern "C" fn Servo_ComputedValues_Inherit( debug_assert!(pseudo.is_anon_box()); let mut style = - StyleBuilder::for_inheritance(data.stylist.device(), parent_style_context, Some(&pseudo)); + StyleBuilder::for_inheritance(data.stylist.device(), Some(&data.stylist), parent_style_context, Some(&pseudo)); if for_text { StyleAdjuster::new(&mut style).adjust_for_text(); @@ -4601,7 +4601,7 @@ pub extern "C" fn Servo_DeclarationBlock_SerializeOneValue( buffer, computed_values, custom_properties, - &data.stylist.device(), + &data.stylist, ); debug_assert!(rv.is_ok()); } @@ -5937,7 +5937,7 @@ fn create_context_for_animation<'a>( container_size_query: ContainerSizeQuery<'a>, ) -> Context<'a> { Context::new_for_animation( - StyleBuilder::for_animation(per_doc_data.stylist.device(), style, parent_style), + StyleBuilder::for_animation(per_doc_data.stylist.device(), Some(&per_doc_data.stylist), style, parent_style), for_smil_animation, per_doc_data.stylist.quirks_mode(), rule_cache_conditions,