Refactor the cascade

Converting the specified value of some properties into a computed value
depends on the value of other properties. For example, the `em` unit
of any length depends on the `font-size` property.

Previously, we would do a first pass over applicable declarations
to build up a `values::computed::Context` struct with a number of fields
for each such piece of data from other properties.

This simplies the struct by instead having it contain the
set of computed values (for a given element) that is being populated
and classify properties into "early" and "other",
such that the only dependencies can be from "other" to "early".
We iterate applicable_declarations twice, first cascading "early" properties
then "other".
Unfortunately, it’s not easy to check that this classification is correct.
This commit is contained in:
Simon Sapin 2016-03-09 00:33:01 +01:00
parent bab28e5070
commit 529164e4a5
3 changed files with 213 additions and 339 deletions

View file

@ -33,7 +33,7 @@ use selectors::matching::DeclarationBlock;
use stylesheets::Origin; use stylesheets::Origin;
use values::AuExtensionMethods; use values::AuExtensionMethods;
use values::computed::{self, ToComputedValue}; use values::computed::{self, ToComputedValue};
use values::specified::{Length, BorderStyle}; use values::specified::BorderStyle;
use self::property_bit_field::PropertyBitField; use self::property_bit_field::PropertyBitField;
@ -151,9 +151,8 @@ pub mod longhands {
${caller.body()} ${caller.body()}
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn cascade_property(declaration: &PropertyDeclaration, pub fn cascade_property(declaration: &PropertyDeclaration,
style: &mut ComputedValues,
inherited_style: &ComputedValues, inherited_style: &ComputedValues,
context: &computed::Context, context: &mut computed::Context,
seen: &mut PropertyBitField, seen: &mut PropertyBitField,
cacheable: &mut bool, cacheable: &mut bool,
error_reporter: &mut Box<ParseErrorReporter + Send>) { error_reporter: &mut Box<ParseErrorReporter + Send>) {
@ -169,7 +168,7 @@ pub mod longhands {
} }
seen.set_${property.ident}(); seen.set_${property.ident}();
let computed_value = ::properties::substitute_variables_${property.ident}( let computed_value = ::properties::substitute_variables_${property.ident}(
declared_value, &style.custom_properties, |value| match *value { declared_value, &context.style.custom_properties, |value| match *value {
DeclaredValue::Value(ref specified_value) => { DeclaredValue::Value(ref specified_value) => {
specified_value.to_computed_value(&context) specified_value.to_computed_value(&context)
} }
@ -187,13 +186,12 @@ pub mod longhands {
} }
}, error_reporter }, error_reporter
); );
Arc::make_mut(&mut style.${THIS_STYLE_STRUCT.ident}).${property.ident} = Arc::make_mut(&mut context.style.${THIS_STYLE_STRUCT.ident}).${property.ident} =
computed_value; computed_value;
% if custom_cascade: % if custom_cascade:
cascade_property_custom(&computed_value, cascade_property_custom(&computed_value,
declaration, declaration,
style,
inherited_style, inherited_style,
context, context,
seen, seen,
@ -364,11 +362,7 @@ pub mod longhands {
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T { fn to_computed_value(&self, context: &Context) -> computed_value::T {
if !context.border_${side}_present { self.0.to_computed_value(context)
Au(0)
} else {
self.0.to_computed_value(context)
}
} }
} }
</%self:longhand> </%self:longhand>
@ -428,11 +422,7 @@ pub mod longhands {
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T { fn to_computed_value(&self, context: &Context) -> computed_value::T {
if !context.outline_style_present { self.0.to_computed_value(context)
Au(0)
} else {
self.0.to_computed_value(context)
}
} }
} }
</%self:longhand> </%self:longhand>
@ -462,7 +452,7 @@ pub mod longhands {
experimental_values = set("flex".split()) experimental_values = set("flex".split())
%> %>
pub use self::computed_value::T as SpecifiedValue; pub use self::computed_value::T as SpecifiedValue;
use values::computed::Context; use values::computed::{Context, ComputedValueAsSpecified};
pub mod computed_value { pub mod computed_value {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
@ -506,47 +496,19 @@ pub mod longhands {
} }
} }
impl ToComputedValue for SpecifiedValue { impl ComputedValueAsSpecified for SpecifiedValue {}
type ComputedValue = computed_value::T;
#[inline] fn cascade_property_custom(_computed_value: &computed_value::T,
fn to_computed_value(&self, context: &Context) -> computed_value::T {
use self::computed_value::T;
// if context.is_root_element && value == list_item {
// return block
// }
if context.positioned || context.floated || context.is_root_element {
match *self {
T::inline_table => T::table,
T::inline | T::inline_block |
T::table_row_group | T::table_column |
T::table_column_group | T::table_header_group |
T::table_footer_group | T::table_row | T::table_cell |
T::table_caption
=> T::block,
_ => *self,
}
} else {
*self
}
}
}
fn cascade_property_custom(computed_value: &computed_value::T,
_declaration: &PropertyDeclaration, _declaration: &PropertyDeclaration,
style: &mut ComputedValues,
_inherited_style: &ComputedValues, _inherited_style: &ComputedValues,
context: &computed::Context, context: &mut computed::Context,
_seen: &mut PropertyBitField, _seen: &mut PropertyBitField,
_cacheable: &mut bool, _cacheable: &mut bool,
_error_reporter: &mut Box<ParseErrorReporter + Send>) { _error_reporter: &mut Box<ParseErrorReporter + Send>) {
Arc::make_mut(&mut style.box_)._servo_display_for_hypothetical_box = Arc::make_mut(&mut context.style.box_)._servo_display_for_hypothetical_box =
longhands::_servo_display_for_hypothetical_box::derive_from_display( longhands::_servo_display_for_hypothetical_box::derive_from_display(&context);
*computed_value, Arc::make_mut(&mut context.style.inheritedtext)._servo_text_decorations_in_effect =
&context); longhands::_servo_text_decorations_in_effect::derive_from_display(&context);
Arc::make_mut(&mut style.inheritedtext)._servo_text_decorations_in_effect =
longhands::_servo_text_decorations_in_effect::derive_from_display(*computed_value,
&context);
} }
</%self:longhand> </%self:longhand>
@ -560,7 +522,10 @@ pub mod longhands {
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T { fn to_computed_value(&self, context: &Context) -> computed_value::T {
if context.positioned { let positioned = matches!(context.style.box_.position,
longhands::position::SpecifiedValue::absolute |
longhands::position::SpecifiedValue::fixed);
if positioned {
SpecifiedValue::none SpecifiedValue::none
} else { } else {
*self *self
@ -581,14 +546,9 @@ pub mod longhands {
} }
#[inline] #[inline]
pub fn derive_from_display(computed_value: super::display::computed_value::T, pub fn derive_from_display(context: &computed::Context)
context: &computed::Context)
-> computed_value::T { -> computed_value::T {
if context.is_root_element { context.style.box_.display
computed_value
} else {
context.display
}
} }
</%self:longhand> </%self:longhand>
@ -1876,7 +1836,7 @@ pub mod longhands {
% for weight in range(100, 901, 100): % for weight in range(100, 901, 100):
SpecifiedValue::Weight${weight} => computed_value::T::Weight${weight}, SpecifiedValue::Weight${weight} => computed_value::T::Weight${weight},
% endfor % endfor
SpecifiedValue::Bolder => match context.inherited_font_weight { SpecifiedValue::Bolder => match context.inherited_style.font.font_weight {
computed_value::T::Weight100 => computed_value::T::Weight400, computed_value::T::Weight100 => computed_value::T::Weight400,
computed_value::T::Weight200 => computed_value::T::Weight400, computed_value::T::Weight200 => computed_value::T::Weight400,
computed_value::T::Weight300 => computed_value::T::Weight400, computed_value::T::Weight300 => computed_value::T::Weight400,
@ -1887,7 +1847,7 @@ pub mod longhands {
computed_value::T::Weight800 => computed_value::T::Weight900, computed_value::T::Weight800 => computed_value::T::Weight900,
computed_value::T::Weight900 => computed_value::T::Weight900, computed_value::T::Weight900 => computed_value::T::Weight900,
}, },
SpecifiedValue::Lighter => match context.inherited_font_weight { SpecifiedValue::Lighter => match context.inherited_style.font.font_weight {
computed_value::T::Weight100 => computed_value::T::Weight100, computed_value::T::Weight100 => computed_value::T::Weight100,
computed_value::T::Weight200 => computed_value::T::Weight100, computed_value::T::Weight200 => computed_value::T::Weight100,
computed_value::T::Weight300 => computed_value::T::Weight100, computed_value::T::Weight300 => computed_value::T::Weight100,
@ -1909,6 +1869,7 @@ pub mod longhands {
use std::fmt; use std::fmt;
use values::FONT_MEDIUM_PX; use values::FONT_MEDIUM_PX;
use values::computed::Context; use values::computed::Context;
use values::specified::{LengthOrPercentage, Length, Percentage};
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -1931,8 +1892,25 @@ pub mod longhands {
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T { fn to_computed_value(&self, context: &Context) -> computed_value::T {
// We already computed this element's font size; no need to compute it again. match self.0 {
return context.font_size LengthOrPercentage::Length(Length::FontRelative(value)) => {
value.to_computed_value(context.inherited_style.font.font_size,
context.style.root_font_size)
}
LengthOrPercentage::Length(Length::ServoCharacterWidth(value)) => {
value.to_computed_value(context.inherited_style.font.font_size)
}
LengthOrPercentage::Length(l) => {
l.to_computed_value(&context)
}
LengthOrPercentage::Percentage(Percentage(value)) => {
context.inherited_style.font.font_size.scale_by(value)
}
LengthOrPercentage::Calc(calc) => {
let calc = calc.to_computed_value(&context);
calc.length() + context.inherited_style.font.font_size.scale_by(calc.percentage())
}
}
} }
} }
/// <length> | <percentage> | <absolute-size> | <relative-size> /// <length> | <percentage> | <absolute-size> | <relative-size>
@ -2229,17 +2207,15 @@ pub mod longhands {
if !empty { Ok(result) } else { Err(()) } if !empty { Ok(result) } else { Err(()) }
} }
fn cascade_property_custom(computed_value: &computed_value::T, fn cascade_property_custom(_computed_value: &computed_value::T,
_declaration: &PropertyDeclaration, _declaration: &PropertyDeclaration,
style: &mut ComputedValues,
_inherited_style: &ComputedValues, _inherited_style: &ComputedValues,
context: &computed::Context, context: &mut computed::Context,
_seen: &mut PropertyBitField, _seen: &mut PropertyBitField,
_cacheable: &mut bool, _cacheable: &mut bool,
_error_reporter: &mut Box<ParseErrorReporter + Send>) { _error_reporter: &mut Box<ParseErrorReporter + Send>) {
Arc::make_mut(&mut style.inheritedtext)._servo_text_decorations_in_effect = Arc::make_mut(&mut context.style.inheritedtext)._servo_text_decorations_in_effect =
longhands::_servo_text_decorations_in_effect::derive_from_text_decoration( longhands::_servo_text_decorations_in_effect::derive_from_text_decoration(
*computed_value,
&context); &context);
} }
</%self:longhand> </%self:longhand>
@ -2284,7 +2260,7 @@ pub mod longhands {
fn maybe(flag: bool, context: &computed::Context) -> Option<RGBA> { fn maybe(flag: bool, context: &computed::Context) -> Option<RGBA> {
if flag { if flag {
Some(context.color) Some(context.style.color.color)
} else { } else {
None None
} }
@ -2293,9 +2269,9 @@ pub mod longhands {
fn derive(context: &computed::Context) -> computed_value::T { fn derive(context: &computed::Context) -> computed_value::T {
// Start with no declarations if this is a block; otherwise, start with the // Start with no declarations if this is a block; otherwise, start with the
// declarations in effect and add in the text decorations that this inline specifies. // declarations in effect and add in the text decorations that this inline specifies.
let mut result = match context.display { let mut result = match context.style.box_.display {
super::display::computed_value::T::inline => { super::display::computed_value::T::inline => {
context.inherited_text_decorations_in_effect context.inherited_style.inheritedtext._servo_text_decorations_in_effect
} }
_ => { _ => {
SpecifiedValue { SpecifiedValue {
@ -2307,27 +2283,25 @@ pub mod longhands {
}; };
if result.underline.is_none() { if result.underline.is_none() {
result.underline = maybe(context.text_decoration.underline, context) result.underline = maybe(context.style.text.text_decoration.underline, context)
} }
if result.overline.is_none() { if result.overline.is_none() {
result.overline = maybe(context.text_decoration.overline, context) result.overline = maybe(context.style.text.text_decoration.overline, context)
} }
if result.line_through.is_none() { if result.line_through.is_none() {
result.line_through = maybe(context.text_decoration.line_through, context) result.line_through = maybe(context.style.text.text_decoration.line_through, context)
} }
result result
} }
#[inline] #[inline]
pub fn derive_from_text_decoration(_: super::text_decoration::computed_value::T, pub fn derive_from_text_decoration(context: &computed::Context)
context: &computed::Context)
-> computed_value::T { -> computed_value::T {
derive(context) derive(context)
} }
#[inline] #[inline]
pub fn derive_from_display(_: super::display::computed_value::T, pub fn derive_from_display(context: &computed::Context)
context: &computed::Context)
-> computed_value::T { -> computed_value::T {
derive(context) derive(context)
} }
@ -6417,21 +6391,34 @@ lazy_static! {
/// Fast path for the function below. Only computes new inherited styles. /// Fast path for the function below. Only computes new inherited styles.
#[allow(unused_mut)] #[allow(unused_mut)]
fn cascade_with_cached_declarations( fn cascade_with_cached_declarations(
viewport_size: Size2D<Au>,
applicable_declarations: &[DeclarationBlock<Vec<PropertyDeclaration>>], applicable_declarations: &[DeclarationBlock<Vec<PropertyDeclaration>>],
shareable: bool, shareable: bool,
parent_style: &ComputedValues, parent_style: &ComputedValues,
cached_style: &ComputedValues, cached_style: &ComputedValues,
custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>, custom_properties: Option<Arc<::custom_properties::ComputedValuesMap>>,
context: &computed::Context,
mut error_reporter: Box<ParseErrorReporter + Send>) mut error_reporter: Box<ParseErrorReporter + Send>)
-> ComputedValues { -> ComputedValues {
% for style_struct in STYLE_STRUCTS: let mut context = computed::Context {
% if style_struct.inherited: is_root_element: false,
let mut style_${style_struct.ident} = parent_style.${style_struct.ident}.clone(); viewport_size: viewport_size,
% else: inherited_style: parent_style,
let mut style_${style_struct.ident} = cached_style.${style_struct.ident}.clone(); style: ComputedValues {
% endif % for style_struct in STYLE_STRUCTS:
% endfor ${style_struct.ident}:
% if style_struct.inherited:
parent_style
% else:
cached_style
% endif
.${style_struct.ident}.clone(),
% endfor
custom_properties: custom_properties,
shareable: shareable,
writing_mode: WritingMode::empty(),
root_font_size: parent_style.root_font_size,
},
};
let mut seen = PropertyBitField::new(); let mut seen = PropertyBitField::new();
// Declaration blocks are stored in increasing precedence order, // Declaration blocks are stored in increasing precedence order,
// we want them in decreasing order here. // we want them in decreasing order here.
@ -6452,9 +6439,10 @@ fn cascade_with_cached_declarations(
seen.set_${property.ident}(); seen.set_${property.ident}();
let computed_value = let computed_value =
substitute_variables_${property.ident}( substitute_variables_${property.ident}(
declared_value, &custom_properties, |value| match *value { declared_value, &context.style.custom_properties,
|value| match *value {
DeclaredValue::Value(ref specified_value) DeclaredValue::Value(ref specified_value)
=> specified_value.to_computed_value(context), => specified_value.to_computed_value(&context),
DeclaredValue::Initial DeclaredValue::Initial
=> longhands::${property.ident}::get_initial_value(), => longhands::${property.ident}::get_initial_value(),
DeclaredValue::Inherit => { DeclaredValue::Inherit => {
@ -6469,23 +6457,17 @@ fn cascade_with_cached_declarations(
DeclaredValue::WithVariables { .. } => unreachable!() DeclaredValue::WithVariables { .. } => unreachable!()
}, &mut error_reporter }, &mut error_reporter
); );
Arc::make_mut(&mut style_${style_struct.ident}) Arc::make_mut(&mut context.style.${style_struct.ident})
.${property.ident} = computed_value; .${property.ident} = computed_value;
% endif % endif
% if property.name in DERIVED_LONGHANDS: % if property.name in DERIVED_LONGHANDS:
% if not style_struct.inherited:
// Use the cached value.
let computed_value = style_${style_struct.ident}
.${property.ident}.clone();
% endif
% for derived in DERIVED_LONGHANDS[property.name]: % for derived in DERIVED_LONGHANDS[property.name]:
Arc::make_mut(&mut style_${derived.style_struct.ident}) Arc::make_mut(&mut context.style.${derived.style_struct.ident})
.${derived.ident} = .${derived.ident} =
longhands::${derived.ident} longhands::${derived.ident}
::derive_from_${property.ident}( ::derive_from_${property.ident}(
computed_value, &context);
context);
% endfor % endfor
% endif % endif
} }
@ -6503,24 +6485,15 @@ fn cascade_with_cached_declarations(
if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() || if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() ||
seen.get_font_family() { seen.get_font_family() {
compute_font_hash(&mut *Arc::make_mut(&mut style_font)) compute_font_hash(&mut *Arc::make_mut(&mut context.style.font))
} }
ComputedValues { context.style
writing_mode: get_writing_mode(&*style_inheritedbox),
% for style_struct in STYLE_STRUCTS:
${style_struct.ident}: style_${style_struct.ident},
% endfor
custom_properties: custom_properties,
shareable: shareable,
root_font_size: parent_style.root_font_size,
}
} }
type CascadePropertyFn = extern "Rust" fn(declaration: &PropertyDeclaration, type CascadePropertyFn = extern "Rust" fn(declaration: &PropertyDeclaration,
style: &mut ComputedValues,
inherited_style: &ComputedValues, inherited_style: &ComputedValues,
context: &computed::Context, context: &mut computed::Context,
seen: &mut PropertyBitField, seen: &mut PropertyBitField,
cacheable: &mut bool, cacheable: &mut bool,
error_reporter: &mut Box<ParseErrorReporter + Send>); error_reporter: &mut Box<ParseErrorReporter + Send>);
@ -6595,163 +6568,39 @@ pub fn cascade(viewport_size: Size2D<Au>,
let custom_properties = ::custom_properties::finish_cascade( let custom_properties = ::custom_properties::finish_cascade(
custom_properties, &inherited_style.custom_properties); custom_properties, &inherited_style.custom_properties);
let mut context = { if let (Some(cached_style), Some(parent_style)) = (cached_style, parent_style) {
let inherited_font_style = inherited_style.get_font(); let style = cascade_with_cached_declarations(viewport_size,
computed::Context { applicable_declarations,
is_root_element: is_root_element,
viewport_size: viewport_size,
inherited_font_weight: inherited_font_style.font_weight,
inherited_font_size: inherited_font_style.font_size,
inherited_text_decorations_in_effect:
inherited_style.get_inheritedtext()._servo_text_decorations_in_effect,
// To be overridden by applicable declarations:
font_size: inherited_font_style.font_size,
root_font_size: inherited_style.root_font_size,
display: longhands::display::get_initial_value(),
color: inherited_style.get_color().color,
text_decoration: longhands::text_decoration::get_initial_value(),
positioned: false,
floated: false,
border_top_present: false,
border_right_present: false,
border_bottom_present: false,
border_left_present: false,
outline_style_present: false,
}
};
// This assumes that the computed and specified values have the same Rust type.
macro_rules! get_specified(
($style_struct_getter: ident, $property: ident, $declared_value: expr, $error_reporter: expr) => {
concat_idents!(substitute_variables_, $property)(
$declared_value, &custom_properties, |value| match *value {
DeclaredValue::Value(specified_value) => specified_value,
DeclaredValue::Initial => longhands::$property::get_initial_value(),
DeclaredValue::Inherit => {
inherited_style.$style_struct_getter().$property.clone()
}
DeclaredValue::WithVariables { .. } => unreachable!()
}, &mut $error_reporter
)
};
);
// Initialize `context`
// Declarations blocks are already stored in increasing precedence order.
for sub_list in applicable_declarations {
use values::specified::{LengthOrPercentage, Percentage};
// Declarations are stored in reverse source order, we want them in forward order here.
for declaration in sub_list.declarations.iter().rev() {
match *declaration {
PropertyDeclaration::FontSize(ref value) => {
context.font_size = substitute_variables_font_size(
value, &custom_properties, |value| match *value {
DeclaredValue::Value(ref specified_value) => {
match specified_value.0 {
LengthOrPercentage::Length(Length::FontRelative(value)) => {
value.to_computed_value(context.inherited_font_size,
context.root_font_size)
}
LengthOrPercentage::Length(Length::ServoCharacterWidth(value)) => {
value.to_computed_value(context.inherited_font_size)
}
LengthOrPercentage::Length(l) => {
l.to_computed_value(&context)
}
LengthOrPercentage::Percentage(Percentage(value)) => {
context.inherited_font_size.scale_by(value)
}
LengthOrPercentage::Calc(calc) => {
let calc = calc.to_computed_value(&context);
calc.length() + context.inherited_font_size.scale_by(calc.percentage())
}
}
}
DeclaredValue::Initial => longhands::font_size::get_initial_value(),
DeclaredValue::Inherit => context.inherited_font_size,
DeclaredValue::WithVariables { .. } => unreachable!(),
}, &mut error_reporter
);
}
PropertyDeclaration::Color(ref value) => {
context.color = substitute_variables_color(
value, &custom_properties, |value| match *value {
DeclaredValue::Value(ref specified_value) => {
specified_value.parsed
}
DeclaredValue::Initial => longhands::color::get_initial_value(),
DeclaredValue::Inherit => inherited_style.get_color().color.clone(),
DeclaredValue::WithVariables { .. } => unreachable!(),
}, &mut error_reporter
);
}
PropertyDeclaration::Display(ref value) => {
context.display = get_specified!(get_box, display, value, error_reporter);
}
PropertyDeclaration::Position(ref value) => {
context.positioned = match get_specified!(get_box, position, value, error_reporter) {
longhands::position::SpecifiedValue::absolute |
longhands::position::SpecifiedValue::fixed => true,
_ => false,
}
}
PropertyDeclaration::Float(ref value) => {
context.floated = get_specified!(get_box, float, value, error_reporter)
!= longhands::float::SpecifiedValue::none;
}
PropertyDeclaration::TextDecoration(ref value) => {
context.text_decoration = get_specified!(get_text, text_decoration, value, error_reporter);
}
PropertyDeclaration::OutlineStyle(ref value) => {
context.outline_style_present =
match get_specified!(get_outline, outline_style, value, error_reporter) {
BorderStyle::none => false,
_ => true,
};
}
% for side in ["top", "right", "bottom", "left"]:
PropertyDeclaration::Border${side.capitalize()}Style(ref value) => {
context.border_${side}_present =
match get_specified!(get_border, border_${side}_style, value, error_reporter) {
BorderStyle::none | BorderStyle::hidden => false,
_ => true,
};
}
% endfor
_ => {}
}
}
}
match (cached_style, parent_style) {
(Some(cached_style), Some(parent_style)) => {
return (cascade_with_cached_declarations(applicable_declarations,
shareable, shareable,
parent_style, parent_style,
cached_style, cached_style,
custom_properties, custom_properties,
&context, error_reporter);
error_reporter), false) return (style, false)
}
(_, _) => {}
} }
// Set computed values, overwriting earlier declarations for the same property. let mut context = computed::Context {
let mut style = ComputedValues { is_root_element: is_root_element,
% for style_struct in STYLE_STRUCTS: viewport_size: viewport_size,
${style_struct.ident}: inherited_style: inherited_style,
% if style_struct.inherited: style: ComputedValues {
inherited_style % for style_struct in STYLE_STRUCTS:
% else: ${style_struct.ident}:
initial_values % if style_struct.inherited:
% endif inherited_style
.${style_struct.ident}.clone(), % else:
% endfor initial_values
custom_properties: custom_properties, % endif
shareable: shareable, .${style_struct.ident}.clone(),
writing_mode: WritingMode::empty(), % endfor
root_font_size: context.root_font_size, custom_properties: custom_properties,
shareable: shareable,
writing_mode: WritingMode::empty(),
root_font_size: inherited_style.root_font_size,
},
}; };
// Set computed values, overwriting earlier declarations for the same property.
let mut cacheable = true; let mut cacheable = true;
let mut seen = PropertyBitField::new(); let mut seen = PropertyBitField::new();
// Declaration blocks are stored in increasing precedence order, we want them in decreasing // Declaration blocks are stored in increasing precedence order, we want them in decreasing
@ -6761,26 +6610,83 @@ pub fn cascade(viewport_size: Size2D<Au>,
// of compiled code! To improve i-cache behavior, we outline the individual functions and use // of compiled code! To improve i-cache behavior, we outline the individual functions and use
// virtual dispatch instead. // virtual dispatch instead.
CASCADE_PROPERTY.with(|cascade_property| { CASCADE_PROPERTY.with(|cascade_property| {
for sub_list in applicable_declarations.iter().rev() { % for category_to_cascade_now in ["early", "other"]:
// Declarations are already stored in reverse order. for sub_list in applicable_declarations.iter().rev() {
for declaration in sub_list.declarations.iter() { // Declarations are already stored in reverse order.
if let PropertyDeclaration::Custom(..) = *declaration { for declaration in sub_list.declarations.iter() {
continue if let PropertyDeclaration::Custom(..) = *declaration {
continue
}
// The computed value of some properties depends on the (sometimes computed)
// value of *other* properties.
// So we classify properties into "early" and "other",
// such that the only dependencies can be from "other" to "early".
// We iterate applicable_declarations twice, first cascading "early" properties
// then "other".
// Unfortunately, its not easy to check that this classification is correct.
let is_early_property = matches!(*declaration,
PropertyDeclaration::FontSize(_) |
PropertyDeclaration::Color(_) |
PropertyDeclaration::Position(_) |
PropertyDeclaration::Float(_) |
PropertyDeclaration::TextDecoration(_)
);
if
% if category_to_cascade_now == "early":
!
% endif
is_early_property
{
continue
}
let discriminant = unsafe {
intrinsics::discriminant_value(declaration) as usize
};
(cascade_property[discriminant].unwrap())(declaration,
inherited_style,
&mut context,
&mut seen,
&mut cacheable,
&mut error_reporter);
} }
let discriminant = unsafe {
intrinsics::discriminant_value(declaration) as usize
};
(cascade_property[discriminant].unwrap())(declaration,
&mut style,
inherited_style,
&context,
&mut seen,
&mut cacheable,
&mut error_reporter);
} }
} % endfor
}); });
let mut style = context.style;
let positioned = matches!(style.box_.position,
longhands::position::SpecifiedValue::absolute |
longhands::position::SpecifiedValue::fixed);
let floated = style.box_.float != longhands::float::SpecifiedValue::none;
if positioned || floated || is_root_element {
use computed_values::display::T;
let specified_display = style.box_.display;
let computed_display = match specified_display {
T::inline_table => {
Some(T::table)
}
T::inline | T::inline_block |
T::table_row_group | T::table_column |
T::table_column_group | T::table_header_group |
T::table_footer_group | T::table_row | T::table_cell |
T::table_caption => {
Some(T::block)
}
_ => None
};
if let Some(computed_display) = computed_display {
let box_ = Arc::make_mut(&mut style.box_);
box_.display = computed_display;
box_._servo_display_for_hypothetical_box = if is_root_element {
computed_display
} else {
specified_display
};
}
}
{ {
use computed_values::overflow_x::T as overflow; use computed_values::overflow_x::T as overflow;
use computed_values::overflow_y; use computed_values::overflow_y;
@ -6797,35 +6703,21 @@ pub fn cascade(viewport_size: Size2D<Au>,
} }
// The initial value of border-*-width may be changed at computed value time. // The initial value of border-*-width may be changed at computed value time.
{ % for side in ["top", "right", "bottom", "left"]:
let border = Arc::make_mut(&mut style.border); // Like calling to_computed_value, which wouldn't type check.
% for side in ["top", "right", "bottom", "left"]: if style.border.border_${side}_style.none_or_hidden() &&
// Like calling to_computed_value, which wouldn't type check. style.border.border_${side}_width != Au(0) {
if !context.border_${side}_present { Arc::make_mut(&mut style.border).border_${side}_width = Au(0);
border.border_${side}_width = Au(0); }
} % endfor
% endfor
}
// The initial value of display may be changed at computed value time.
if !seen.get_display() {
let box_ = Arc::make_mut(&mut style.box_);
let computed_value = box_.display.to_computed_value(&context);
box_.display = computed_value;
box_._servo_display_for_hypothetical_box =
longhands::_servo_display_for_hypothetical_box::derive_from_display(
computed_value,
&context);
}
// The initial value of outline width may be changed at computed value time. // The initial value of outline width may be changed at computed value time.
if !context.outline_style_present { if style.outline.outline_style.none_or_hidden() && style.outline.outline_width != Au(0) {
let outline = Arc::make_mut(&mut style.outline); Arc::make_mut(&mut style.outline).outline_width = Au(0);
outline.outline_width = Au(0);
} }
if is_root_element { if is_root_element {
style.root_font_size = context.font_size; style.root_font_size = style.font.font_size;
} }
if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() || if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() ||

View file

@ -1377,6 +1377,12 @@ pub mod specified {
"outset" => outset = 2, "outset" => outset = 2,
} }
impl BorderStyle {
pub fn none_or_hidden(&self) -> bool {
matches!(*self, BorderStyle::none | BorderStyle::hidden)
}
}
/// A time in seconds according to CSS-VALUES § 6.2. /// A time in seconds according to CSS-VALUES § 6.2.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, HeapSizeOf)] #[derive(Clone, Copy, Debug, PartialEq, PartialOrd, HeapSizeOf)]
pub struct Time(pub CSSFloat); pub struct Time(pub CSSFloat);
@ -1431,7 +1437,7 @@ pub mod specified {
pub mod computed { pub mod computed {
use app_units::Au; use app_units::Au;
use euclid::size::Size2D; use euclid::size::Size2D;
use properties::longhands; use properties::ComputedValues;
use std::fmt; use std::fmt;
use super::AuExtensionMethods; use super::AuExtensionMethods;
use super::specified::AngleOrCorner; use super::specified::AngleOrCorner;
@ -1440,26 +1446,14 @@ pub mod computed {
pub use cssparser::Color as CSSColor; pub use cssparser::Color as CSSColor;
pub use super::specified::{Angle, BorderStyle, Time}; pub use super::specified::{Angle, BorderStyle, Time};
pub struct Context { pub struct Context<'a> {
pub inherited_font_weight: longhands::font_weight::computed_value::T,
pub inherited_font_size: longhands::font_size::computed_value::T,
pub inherited_text_decorations_in_effect:
longhands::_servo_text_decorations_in_effect::computed_value::T,
pub color: longhands::color::computed_value::T,
pub text_decoration: longhands::text_decoration::computed_value::T,
pub font_size: longhands::font_size::computed_value::T,
pub root_font_size: longhands::font_size::computed_value::T,
pub display: longhands::display::computed_value::T,
pub positioned: bool,
pub floated: bool,
pub border_top_present: bool,
pub border_right_present: bool,
pub border_bottom_present: bool,
pub border_left_present: bool,
pub is_root_element: bool, pub is_root_element: bool,
pub viewport_size: Size2D<Au>, pub viewport_size: Size2D<Au>,
pub outline_style_present: bool, pub inherited_style: &'a ComputedValues,
// TODO, as needed: viewport size, etc.
/// Values access through this need to be in the properties "computed early":
/// color, text-decoration, font-size, display, position, float, border-*-style, outline-style
pub style: ComputedValues,
} }
pub trait ToComputedValue { pub trait ToComputedValue {
@ -1500,11 +1494,12 @@ pub mod computed {
specified::Length::Absolute(length) => length, specified::Length::Absolute(length) => length,
specified::Length::Calc(calc) => calc.to_computed_value(context).length(), specified::Length::Calc(calc) => calc.to_computed_value(context).length(),
specified::Length::FontRelative(length) => specified::Length::FontRelative(length) =>
length.to_computed_value(context.font_size, context.root_font_size), length.to_computed_value(context.style.get_font().font_size,
context.style.root_font_size),
specified::Length::ViewportPercentage(length) => specified::Length::ViewportPercentage(length) =>
length.to_computed_value(context.viewport_size), length.to_computed_value(context.viewport_size),
specified::Length::ServoCharacterWidth(length) => specified::Length::ServoCharacterWidth(length) =>
length.to_computed_value(context.font_size) length.to_computed_value(context.style.get_font().font_size)
} }
} }
} }
@ -1603,8 +1598,8 @@ pub mod computed {
} }
for val in &[self.ch, self.em, self.ex, self.rem] { for val in &[self.ch, self.em, self.ex, self.rem] {
if let Some(val) = *val { if let Some(val) = *val {
length = Some(length.unwrap_or(Au(0)) + length = Some(length.unwrap_or(Au(0)) + val.to_computed_value(
val.to_computed_value(context.font_size, context.root_font_size)); context.style.get_font().font_size, context.style.root_font_size));
} }
} }

View file

@ -8,7 +8,7 @@ use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser,
use euclid::scale_factor::ScaleFactor; use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D}; use euclid::size::{Size2D, TypedSize2D};
use parser::{ParserContext, log_css_error}; use parser::{ParserContext, log_css_error};
use properties::longhands; use properties::INITIAL_VALUES;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::collections::hash_map::{Entry, HashMap}; use std::collections::hash_map::{Entry, HashMap};
use std::fmt; use std::fmt;
@ -594,21 +594,8 @@ impl MaybeNew for ViewportConstraints {
let context = Context { let context = Context {
is_root_element: false, is_root_element: false,
viewport_size: initial_viewport, viewport_size: initial_viewport,
inherited_font_weight: longhands::font_weight::get_initial_value(), inherited_style: &*INITIAL_VALUES,
inherited_font_size: longhands::font_size::get_initial_value(), style: INITIAL_VALUES.clone(),
inherited_text_decorations_in_effect: longhands::_servo_text_decorations_in_effect::get_initial_value(),
font_size: longhands::font_size::get_initial_value(),
root_font_size: longhands::font_size::get_initial_value(),
display: longhands::display::get_initial_value(),
color: longhands::color::get_initial_value(),
text_decoration: longhands::text_decoration::get_initial_value(),
positioned: false,
floated: false,
border_top_present: false,
border_right_present: false,
border_bottom_present: false,
border_left_present: false,
outline_style_present: false,
}; };
// DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom' // DEVICE-ADAPT § 9.3 Resolving 'extend-to-zoom'