From 3acb103324d68738b773f4e16a7fe96c4e93b0d5 Mon Sep 17 00:00:00 2001 From: David Shin Date: Mon, 17 Oct 2022 17:26:12 +0000 Subject: [PATCH] style: Centralize construction of `computed::Context` For controlled construction and access of upcoming, lazily-evaluated container query size. Differential Revision: https://phabricator.services.mozilla.com/D158055 --- components/style/properties/cascade.rs | 14 ++----- components/style/stylesheets/viewport_rule.rs | 14 ++----- components/style/values/computed/mod.rs | 37 +++++++++++++++++++ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/components/style/properties/cascade.rs b/components/style/properties/cascade.rs index 73dee7ede07..54fcda7481a 100644 --- a/components/style/properties/cascade.rs +++ b/components/style/properties/cascade.rs @@ -26,7 +26,6 @@ use fxhash::FxHashMap; use servo_arc::Arc; use smallvec::SmallVec; use std::borrow::Cow; -use std::cell::RefCell; #[cfg(feature = "gecko")] use std::mem; @@ -277,11 +276,11 @@ where let is_root_element = pseudo.is_none() && element.map_or(false, |e| e.is_root()); - let mut context = computed::Context { + let mut context = computed::Context::new( // We'd really like to own the rules here to avoid refcount traffic, but // animation's usage of `apply_declarations` make this tricky. See bug // 1375525. - builder: StyleBuilder::new( + StyleBuilder::new( device, parent_style, parent_style_ignoring_first_line, @@ -290,14 +289,9 @@ where custom_properties, is_root_element, ), - cached_system_font: None, - in_media_query: false, - for_smil_animation: false, - for_non_inherited_property: None, - container_info: None, quirks_mode, - rule_cache_conditions: RefCell::new(rule_cache_conditions), - }; + rule_cache_conditions, + ); let using_cached_reset_properties; let mut cascade = Cascade::new(&mut context, cascade_mode, &referenced_properties); diff --git a/components/style/stylesheets/viewport_rule.rs b/components/style/stylesheets/viewport_rule.rs index 10e6af5715b..f112aff17ab 100644 --- a/components/style/stylesheets/viewport_rule.rs +++ b/components/style/stylesheets/viewport_rule.rs @@ -28,7 +28,6 @@ use cssparser::{parse_important, AtRuleParser, DeclarationListParser, Declaratio use euclid::Size2D; use selectors::parser::SelectorParseErrorKind; use std::borrow::Cow; -use std::cell::RefCell; use std::fmt::{self, Write}; use std::iter::Enumerate; use std::str::Chars; @@ -665,18 +664,13 @@ impl MaybeNew for ViewportConstraints { let initial_viewport = device.au_viewport_size(); let mut conditions = RuleCacheConditions::default(); - let context = Context { + let context = Context::new( // Note: DEVICE-ADAPT § 5. states that relative length values are // resolved against initial values - builder: StyleBuilder::for_inheritance(device, None, None), - cached_system_font: None, - in_media_query: false, + StyleBuilder::for_inheritance(device, None, None), quirks_mode, - container_info: None, - for_smil_animation: false, - for_non_inherited_property: None, - rule_cache_conditions: RefCell::new(&mut conditions), - }; + &mut conditions, + ); // DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom' let extend_width; diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 7a5d2a35838..af0e30c93ad 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -246,6 +246,43 @@ impl<'a> Context<'a> { f(&context) } + /// Creates a context suitable for more general cases. + pub fn new( + builder: StyleBuilder<'a>, + quirks_mode: QuirksMode, + rule_cache_conditions: &'a mut RuleCacheConditions, + ) -> Self { + Self { + builder, + cached_system_font: None, + in_media_query: false, + quirks_mode, + container_info: None, + for_smil_animation: false, + for_non_inherited_property: None, + rule_cache_conditions: RefCell::new(rule_cache_conditions), + } + } + + /// Creates a context suitable for computing animations. + pub fn new_for_animation( + builder: StyleBuilder<'a>, + for_smil_animation: bool, + quirks_mode: QuirksMode, + rule_cache_conditions: &'a mut RuleCacheConditions, + ) -> Self { + Self { + builder, + cached_system_font: None, + in_media_query: false, + quirks_mode, + container_info: None, + for_smil_animation, + for_non_inherited_property: None, + rule_cache_conditions: RefCell::new(rule_cache_conditions), + } + } + /// The current device. pub fn device(&self) -> &Device { self.builder.device