Skip to content

Commit

Permalink
Bug 1840478 - Introduce stylist as a memeber for stylebuilder. r=emil…
Browse files Browse the repository at this point in the history
…io,zrhoffman

Differential Revision: https://phabricator.services.mozilla.com/D186615
  • Loading branch information
ziransun committed Aug 21, 2023
1 parent 7b25b99 commit a6ba06e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
25 changes: 10 additions & 15 deletions servo/components/style/properties/cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,7 +67,7 @@ pub enum FirstLineReparenting<'a> {
/// * `flags`: Various flags.
///
pub fn cascade<E>(
device: &Device,
stylist: &Stylist,
pseudo: Option<&PseudoElement>,
rule_node: &StrongRuleNode,
guards: &StylesheetGuards,
Expand All @@ -78,7 +77,6 @@ pub fn cascade<E>(
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<E>,
Expand All @@ -87,7 +85,7 @@ where
E: TElement,
{
cascade_rules(
device,
stylist,
pseudo,
rule_node,
guards,
Expand All @@ -97,7 +95,6 @@ where
first_line_reparenting,
CascadeMode::Unvisited { visited_rules },
cascade_input_flags,
quirks_mode,
rule_cache,
rule_cache_conditions,
element,
Expand Down Expand Up @@ -188,7 +185,7 @@ impl<'a> Iterator for DeclarationIterator<'a> {
}

fn cascade_rules<E>(
device: &Device,
stylist: &Stylist,
pseudo: Option<&PseudoElement>,
rule_node: &StrongRuleNode,
guards: &StylesheetGuards,
Expand All @@ -198,7 +195,6 @@ fn cascade_rules<E>(
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<E>,
Expand All @@ -207,7 +203,7 @@ where
E: TElement,
{
apply_declarations(
device,
stylist,
pseudo,
rule_node,
guards,
Expand All @@ -218,7 +214,6 @@ where
first_line_reparenting,
cascade_mode,
cascade_input_flags,
quirks_mode,
rule_cache,
rule_cache_conditions,
element,
Expand All @@ -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,
Expand All @@ -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<E>,
Expand All @@ -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();
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions servo/components/style/properties/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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()
}
Expand All @@ -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)
Expand Down Expand Up @@ -935,7 +935,7 @@ impl PropertyDeclarationBlock {
&self,
context: &Context,
) -> Option<Arc<crate::custom_properties::CustomPropertiesMap>> {
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
Expand All @@ -944,9 +944,9 @@ impl PropertyDeclarationBlock {
fn cascade_custom_properties(
&self,
inherited_custom_properties: Option<&Arc<crate::custom_properties::CustomPropertiesMap>>,
device: &Device,
stylist: &Stylist,
) -> Option<Arc<crate::custom_properties::CustomPropertiesMap>> {
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 {
Expand Down
13 changes: 13 additions & 0 deletions servo/components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2975,6 +2976,7 @@ pub struct ComputedValuesInner {
${style_struct.ident}: Arc<style_structs::${style_struct.name}>,
% endfor
custom_properties: Option<Arc<crate::custom_properties::CustomPropertiesMap>>,

/// The writing mode of this computed values struct.
pub writing_mode: WritingMode,

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<StrongRuleNode>,
Expand All @@ -3654,6 +3661,7 @@ impl<'a> StyleBuilder<'a> {

StyleBuilder {
device,
stylist,
inherited_style,
reset_style,
pseudo,
Expand Down Expand Up @@ -3682,13 +3690,15 @@ 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 {
let reset_style = device.default_computed_values();
let inherited_style = parent_style.unwrap_or(reset_style);
StyleBuilder {
device,
stylist,
inherited_style,
reset_style,
pseudo: None,
Expand Down Expand Up @@ -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 {
Expand All @@ -3808,13 +3819,15 @@ impl<'a> StyleBuilder<'a> {
parent.visited_style().map(|style| {
Self::for_inheritance(
device,
stylist,
Some(style),
pseudo,
).build()
})
});
let mut ret = Self::new(
device,
stylist,
parent,
pseudo,
/* rules = */ None,
Expand Down
7 changes: 4 additions & 3 deletions servo/components/style/stylesheets/container_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -252,7 +252,7 @@ impl ContainerCondition {
/// Tries to match a container query condition for a given element.
pub(crate) fn matches<E>(
&self,
device: &Device,
stylist: &Stylist,
element: E,
originating_element_style: Option<&ComputedValues>,
invalidation_flags: &mut ComputedValueFlags,
Expand All @@ -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| {
Expand Down
8 changes: 3 additions & 5 deletions servo/components/style/stylist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ impl Stylist {
//
// FIXME(emilio): We should assert that it holds if pseudo.is_none()!
properties::cascade::<E>(
&self.device,
&self,
pseudo,
inputs.rules.as_ref().unwrap_or(self.rule_tree.root()),
guards,
Expand All @@ -1113,7 +1113,6 @@ impl Stylist {
first_line_reparenting,
visited_rules,
inputs.flags,
self.quirks_mode,
rule_cache,
rule_cache_conditions,
element,
Expand Down Expand Up @@ -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::<E, _>(
&self.device,
&self,
/* pseudo = */ None,
self.rule_tree.root(),
guards,
Expand All @@ -1510,7 +1509,6 @@ impl Stylist {
visited_rules: None,
},
Default::default(),
self.quirks_mode,
/* rule_cache = */ None,
&mut Default::default(),
/* element = */ None,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions servo/components/style/values/computed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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,
Expand All @@ -233,6 +234,7 @@ impl<'a> Context<'a> {
/// specified.
pub fn for_container_query_evaluation<F, R>(
device: &Device,
stylist: Option<&Stylist>,
container_info_and_style: Option<(ContainerInfo, Arc<ComputedValues>)>,
container_size_query: ContainerSizeQuery,
f: F,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit a6ba06e

Please sign in to comment.