Skip to content

Commit

Permalink
Bug 1857674 - Reuse inherited custom properties from style builder r=…
Browse files Browse the repository at this point in the history
…emilio

Depends on D191160

Differential Revision: https://phabricator.services.mozilla.com/D191161
  • Loading branch information
zrhoffman committed Oct 23, 2023
1 parent e8fb520 commit c93e4c2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
33 changes: 17 additions & 16 deletions servo/components/style/custom_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,6 @@ pub struct CustomPropertiesBuilder<'a, 'b: 'a> {
seen: PrecomputedHashSet<&'a Name>,
may_have_cycles: bool,
custom_properties: ComputedCustomProperties,
inherited: &'a ComputedCustomProperties,
reverted: PrecomputedHashMap<&'a Name, (CascadePriority, bool)>,
stylist: &'a Stylist,
computed_context: &'a computed::Context<'b>,
Expand All @@ -800,11 +799,11 @@ pub struct CustomPropertiesBuilder<'a, 'b: 'a> {
impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
/// Create a new builder, inheriting from a given custom properties map.
pub fn new(
inherited: &'a ComputedCustomProperties,
stylist: &'a Stylist,
computed_context: &'a computed::Context<'b>,
is_root_element: bool,
) -> Self {
let inherited = computed_context.inherited_custom_properties();
let initial_values = stylist.get_custom_property_initial_values();
Self {
seen: PrecomputedHashSet::default(),
Expand All @@ -819,7 +818,6 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
},
non_inherited: initial_values.non_inherited.clone(),
},
inherited,
stylist,
computed_context,
is_root_element,
Expand Down Expand Up @@ -865,7 +863,6 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
name,
unparsed_value,
map,
self.inherited,
self.stylist,
self.computed_context,
self.is_root_element,
Expand Down Expand Up @@ -919,7 +916,8 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
"Should've been handled earlier"
);
if let Some(inherited_value) = self
.inherited
.computed_context
.inherited_custom_properties()
.non_inherited
.as_ref()
.and_then(|m| m.get(name))
Expand Down Expand Up @@ -999,7 +997,8 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
// Don't bother adding it to self.custom_properties.non_inherited
// if the key is also absent from self.inherited.non_inherited.
if self
.inherited
.computed_context
.inherited_custom_properties()
.non_inherited
.as_ref()
.map_or(true, |m| !m.contains_key(name))
Expand Down Expand Up @@ -1031,7 +1030,6 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
if self.may_have_cycles {
substitute_all(
&mut self.custom_properties,
self.inherited,
&self.seen,
self.stylist,
self.computed_context,
Expand All @@ -1047,8 +1045,15 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
// map in that case.
let initial_values = self.stylist.get_custom_property_initial_values();
ComputedCustomProperties {
inherited: if self.inherited.inherited_equal(&self.custom_properties) {
self.inherited.inherited.clone()
inherited: if self
.computed_context
.inherited_custom_properties()
.inherited_equal(&self.custom_properties)
{
self.computed_context
.inherited_custom_properties()
.inherited
.clone()
} else {
self.custom_properties.inherited.take()
},
Expand All @@ -1067,7 +1072,6 @@ impl<'a, 'b: 'a> CustomPropertiesBuilder<'a, 'b> {
/// It does cycle dependencies removal at the same time as substitution.
fn substitute_all(
custom_properties_map: &mut ComputedCustomProperties,
inherited: &ComputedCustomProperties,
seen: &PrecomputedHashSet<&Name>,
stylist: &Stylist,
computed_context: &computed::Context,
Expand Down Expand Up @@ -1107,12 +1111,11 @@ fn substitute_all(
/// all unfinished strong connected components.
stack: SmallVec<[usize; 5]>,
map: &'a mut ComputedCustomProperties,
/// The inherited custom properties to handle wide keywords.
inherited: &'a ComputedCustomProperties,
/// The stylist is used to get registered properties, and to resolve the environment to
/// substitute `env()` variables.
stylist: &'a Stylist,
/// The computed context is used to compute registered custom properties.
/// The computed context is used to get inherited custom
/// properties and compute registered custom properties.
computed_context: &'a computed::Context<'b>,
/// Whether this is the root element.
is_root_element: bool,
Expand Down Expand Up @@ -1259,7 +1262,6 @@ fn substitute_all(
&name,
&value,
&mut context.map,
context.inherited,
context.stylist,
context.computed_context,
context.is_root_element,
Expand All @@ -1279,7 +1281,6 @@ fn substitute_all(
stack: SmallVec::new(),
var_info: SmallVec::new(),
map: custom_properties_map,
inherited,
stylist,
computed_context,
is_root_element,
Expand Down Expand Up @@ -1326,13 +1327,13 @@ fn substitute_references_in_value_and_apply(
name: &Name,
value: &VariableValue,
custom_properties: &mut ComputedCustomProperties,
inherited: &ComputedCustomProperties,
stylist: &Stylist,
computed_context: &computed::Context,
is_root_element: bool,
) {
debug_assert!(value.has_references());

let inherited = computed_context.inherited_custom_properties();
let custom_registration = stylist.get_custom_property_registration(&name);
let mut computed_value = ComputedValue::empty();

Expand Down
8 changes: 2 additions & 6 deletions servo/components/style/properties/cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,8 @@ where
},
CascadeMode::Unvisited { visited_rules } => {
cascade.context.builder.custom_properties = {
let mut builder = CustomPropertiesBuilder::new(
inherited_style.custom_properties(),
stylist,
cascade.context,
is_root_element,
);
let mut builder =
CustomPropertiesBuilder::new(stylist, cascade.context, is_root_element);
iter_declarations(iter, &mut declarations, Some(&mut builder));
builder.build()
};
Expand Down
5 changes: 1 addition & 4 deletions servo/components/style/properties/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,10 +956,7 @@ impl PropertyDeclarationBlock {
stylist: &Stylist,
context: &Context,
) -> ComputedCustomProperties {
let inherited_custom_properties = context.builder.custom_properties().clone();

let mut builder =
CustomPropertiesBuilder::new(&inherited_custom_properties, stylist, context, false);
let mut builder = CustomPropertiesBuilder::new(stylist, context, false);

for declaration in self.normal_declaration_iter() {
if let PropertyDeclaration::Custom(ref declaration) = *declaration {
Expand Down
6 changes: 6 additions & 0 deletions servo/components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4013,6 +4013,12 @@ impl<'a> StyleBuilder<'a> {
&self.custom_properties
}


/// Get the inherited custom properties map.
pub fn inherited_custom_properties(&self) -> &crate::custom_properties::ComputedCustomProperties {
&self.inherited_style.custom_properties
}

/// Access to various information about our inherited styles. We don't
/// expose an inherited ComputedValues directly, because in the
/// ::first-line case some of the inherited information needs to come from
Expand Down
6 changes: 6 additions & 0 deletions servo/components/style/values/computed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::specified;
use super::{CSSFloat, CSSInteger};
use crate::computed_value_flags::ComputedValueFlags;
use crate::context::QuirksMode;
use crate::custom_properties::ComputedCustomProperties;
use crate::font_metrics::{FontMetrics, FontMetricsOrientation};
use crate::media_queries::Device;
#[cfg(feature = "gecko")]
Expand Down Expand Up @@ -315,6 +316,11 @@ impl<'a> Context<'a> {
self.builder.device
}

/// Get the inherited custom properties map.
pub fn inherited_custom_properties(&self) -> &ComputedCustomProperties {
&self.builder.inherited_custom_properties()
}

/// Queries font metrics.
pub fn query_font_metrics(
&self,
Expand Down

0 comments on commit c93e4c2

Please sign in to comment.