From a4c2e9dcf1a2f357a85484ad86e3162e0dbc841a Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 17 Oct 2013 17:32:33 +0100 Subject: [PATCH] CSS: rexport types and variants for computed values. --- src/components/style/common_types.rs | 48 ++++----- src/components/style/properties.rs.mako | 127 +++++++++++++++--------- src/components/style/style.rc | 9 +- 3 files changed, 109 insertions(+), 75 deletions(-) diff --git a/src/components/style/common_types.rs b/src/components/style/common_types.rs index 48cbafa51ed..60e2014f1a1 100644 --- a/src/components/style/common_types.rs +++ b/src/components/style/common_types.rs @@ -4,34 +4,34 @@ pub use servo_util::geometry::Au; -pub type Float = f64; +pub type CSSFloat = f64; pub mod specified { use std::ascii::StrAsciiExt; use cssparser::*; - use super::{Au, Float}; + use super::{Au, CSSFloat}; pub use CSSColor = cssparser::Color; #[deriving(Clone)] pub enum Length { Au_(Au), // application units - Em(Float), - Ex(Float), + Em(CSSFloat), + Ex(CSSFloat), // XXX uncomment when supported: -// Ch(Float), -// Rem(Float), -// Vw(Float), -// Vh(Float), -// Vmin(Float), -// Vmax(Float), +// Ch(CSSFloat), +// Rem(CSSFloat), +// Vw(CSSFloat), +// Vh(CSSFloat), +// Vmin(CSSFloat), +// Vmax(CSSFloat), } - static AU_PER_PX: Float = 60.; - static AU_PER_IN: Float = AU_PER_PX * 96.; - static AU_PER_CM: Float = AU_PER_IN / 2.54; - static AU_PER_MM: Float = AU_PER_IN / 25.4; - static AU_PER_PT: Float = AU_PER_IN / 72.; - static AU_PER_PC: Float = AU_PER_PT * 12.; + static AU_PER_PX: CSSFloat = 60.; + static AU_PER_IN: CSSFloat = AU_PER_PX * 96.; + static AU_PER_CM: CSSFloat = AU_PER_IN / 2.54; + static AU_PER_MM: CSSFloat = AU_PER_IN / 25.4; + static AU_PER_PT: CSSFloat = AU_PER_IN / 72.; + static AU_PER_PC: CSSFloat = AU_PER_PT * 12.; impl Length { #[inline] fn parse_internal(input: &ComponentValue, negative_ok: bool) -> Option { @@ -48,7 +48,7 @@ pub mod specified { pub fn parse_non_negative(input: &ComponentValue) -> Option { Length::parse_internal(input, /* negative_ok = */ false) } - pub fn parse_dimension(value: Float, unit: &str) -> Option { + pub fn parse_dimension(value: CSSFloat, unit: &str) -> Option { match unit.to_ascii_lower().as_slice() { "px" => Some(Length::from_px(value)), "in" => Some(Au_(Au((value * AU_PER_IN) as i32))), @@ -62,7 +62,7 @@ pub mod specified { } } #[inline] - pub fn from_px(px_value: Float) -> Length { + pub fn from_px(px_value: CSSFloat) -> Length { Au_(Au((px_value * AU_PER_PX) as i32)) } } @@ -70,7 +70,7 @@ pub mod specified { #[deriving(Clone)] pub enum LengthOrPercentage { LP_Length(Length), - LP_Percentage(Float), // [0 .. 100%] maps to [0.0 .. 1.0] + LP_Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0] } impl LengthOrPercentage { fn parse_internal(input: &ComponentValue, negative_ok: bool) @@ -97,7 +97,7 @@ pub mod specified { #[deriving(Clone)] pub enum LengthOrPercentageOrAuto { LPA_Length(Length), - LPA_Percentage(Float), // [0 .. 100%] maps to [0.0 .. 1.0] + LPA_Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0] LPA_Auto, } impl LengthOrPercentageOrAuto { @@ -135,7 +135,7 @@ pub mod computed { pub struct Context { current_color: cssparser::RGBA, font_size: Au, - font_weight: longhands::font_weight::ComputedValue, + font_weight: longhands::font_weight::computed_value::T, position: longhands::position::SpecifiedValue, float: longhands::float::SpecifiedValue, is_root_element: bool, @@ -147,7 +147,7 @@ pub mod computed { } #[inline] - fn mul(a: Au, b: Float) -> Au { Au(((*a as Float) * b) as i32) } + fn mul(a: Au, b: CSSFloat) -> Au { Au(((*a as CSSFloat) * b) as i32) } pub fn compute_Au(value: specified::Length, context: &Context) -> Au { match value { @@ -163,7 +163,7 @@ pub mod computed { #[deriving(Clone)] pub enum LengthOrPercentage { LP_Length(Au), - LP_Percentage(Float), + LP_Percentage(CSSFloat), } pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context) -> LengthOrPercentage { @@ -176,7 +176,7 @@ pub mod computed { #[deriving(Clone)] pub enum LengthOrPercentageOrAuto { LPA_Length(Au), - LPA_Percentage(Float), + LPA_Percentage(CSSFloat), LPA_Auto, } pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto, diff --git a/src/components/style/properties.rs.mako b/src/components/style/properties.rs.mako index b8ea608b212..4d7973f7f65 100644 --- a/src/components/style/properties.rs.mako +++ b/src/components/style/properties.rs.mako @@ -68,6 +68,7 @@ pub mod longhands { % if not no_super: use super::*; % endif + pub use self::computed_value::*; ${caller.body()} pub fn parse_declared(input: &[ComponentValue]) -> Option> { @@ -104,14 +105,16 @@ pub mod longhands { <%self:single_component_value name="${name}" inherited="${inherited}"> // The computed value is the same as the specified value. pub use to_computed_value = super::computed_as_specified; - #[deriving(Clone)] - pub enum SpecifiedValue { - % for value in values.split(): - ${to_rust_ident(value)}, - % endfor + pub mod computed_value { + #[deriving(Clone)] + pub enum T { + % for value in values.split(): + ${to_rust_ident(value)}, + % endfor + } } - pub type ComputedValue = SpecifiedValue; - #[inline] pub fn get_initial_value() -> ComputedValue { + pub type SpecifiedValue = computed_value::T; + #[inline] pub fn get_initial_value() -> computed_value::T { ${to_rust_ident(values.split()[0])} } pub fn from_component_value(v: &ComponentValue) -> Option { @@ -131,8 +134,10 @@ pub mod longhands { <%self:single_component_value name="${name}" inherited="${inherited}"> pub use to_computed_value = super::super::common_types::computed::compute_${type}; pub type SpecifiedValue = specified::${type}; - pub type ComputedValue = computed::${type}; - #[inline] pub fn get_initial_value() -> ComputedValue { ${initial_value} } + pub mod computed_value { + pub type T = super::super::computed::${type}; + } + #[inline] pub fn get_initial_value() -> computed_value::T { ${initial_value} } #[inline] pub fn from_component_value(v: &ComponentValue) -> Option { specified::${type}::${parse_method}(v) } @@ -169,7 +174,9 @@ pub mod longhands { <%self:longhand name="border-${side}-style", no_super="True"> pub use super::border_top_style::*; pub type SpecifiedValue = super::border_top_style::SpecifiedValue; - pub type ComputedValue = super::border_top_style::ComputedValue; + pub mod computed_value { + pub type T = super::super::border_top_style::computed_value::T; + } % endfor @@ -187,15 +194,18 @@ pub mod longhands { % for side in ["top", "right", "bottom", "left"]: <%self:longhand name="border-${side}-width"> pub type SpecifiedValue = specified::Length; - pub type ComputedValue = Au; - #[inline] pub fn get_initial_value() -> ComputedValue { + pub mod computed_value { + use super::super::Au; + pub type T = Au; + } + #[inline] pub fn get_initial_value() -> computed_value::T { Au::from_px(3) // medium } pub fn parse(input: &[ComponentValue]) -> Option { one_component_value(input).chain(parse_border_width) } pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) - -> ComputedValue { + -> computed_value::T { if context.has_border_${side} { computed::compute_Au(value, context) } else { Au(0) } } @@ -231,7 +241,7 @@ pub mod longhands { pub enum SpecifiedValue { SpecifiedNormal, SpecifiedLength(specified::Length), - SpecifiedNumber(Float), + SpecifiedNumber(CSSFloat), // percentage are the same as em. } /// normal | | | @@ -249,15 +259,18 @@ pub mod longhands { _ => None, } } - #[deriving(Clone)] - pub enum ComputedValue { - Normal, - Length(Au), - Number(Float), + pub mod computed_value { + use super::super::{Au, CSSFloat}; + #[deriving(Clone)] + pub enum T { + Normal, + Length(Au), + Number(CSSFloat), + } } - #[inline] pub fn get_initial_value() -> ComputedValue { Normal } + #[inline] pub fn get_initial_value() -> computed_value::T { Normal } pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) - -> ComputedValue { + -> computed_value::T { match value { SpecifiedNormal => Normal, SpecifiedLength(value) => Length(computed::compute_Au(value, context)), @@ -290,17 +303,20 @@ pub mod longhands { .map_move(SpecifiedLengthOrPercentage) } } - #[deriving(Clone)] - pub enum ComputedValue { - % for keyword in vertical_align_keywords: - ${to_rust_ident(keyword)}, - % endfor - Length(Au), - Percentage(Float), + pub mod computed_value { + use super::super::{Au, CSSFloat}; + #[deriving(Clone)] + pub enum T { + % for keyword in vertical_align_keywords: + ${to_rust_ident(keyword)}, + % endfor + Length(Au), + Percentage(CSSFloat), + } } - #[inline] pub fn get_initial_value() -> ComputedValue { baseline } + #[inline] pub fn get_initial_value() -> computed_value::T { baseline } pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) - -> ComputedValue { + -> computed_value::T { match value { % for keyword in vertical_align_keywords: Specified_${to_rust_ident(keyword)} => ${to_rust_ident(keyword)}, @@ -334,8 +350,10 @@ pub mod longhands { <%self:raw_longhand name="color" inherited="True"> pub use to_computed_value = super::computed_as_specified; pub type SpecifiedValue = RGBA; - pub type ComputedValue = SpecifiedValue; - #[inline] pub fn get_initial_value() -> ComputedValue { + pub mod computed_value { + pub type T = super::SpecifiedValue; + } + #[inline] pub fn get_initial_value() -> computed_value::T { RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */ } pub fn parse_specified(input: &[ComponentValue]) -> Option> { @@ -364,8 +382,10 @@ pub mod longhands { // Monospace, } pub type SpecifiedValue = ~[FontFamily]; - pub type ComputedValue = SpecifiedValue; - #[inline] pub fn get_initial_value() -> ComputedValue { ~[FamilyName(~"serif")] } + pub mod computed_value { + pub type T = super::SpecifiedValue; + } + #[inline] pub fn get_initial_value() -> computed_value::T { ~[FamilyName(~"serif")] } /// # /// = | [ + ] /// TODO: @@ -462,15 +482,17 @@ pub mod longhands { _ => None } } - #[deriving(Clone)] - pub enum ComputedValue { - % for weight in range(100, 901, 100): - Weight${weight}, - % endfor + pub mod computed_value { + #[deriving(Clone)] + pub enum T { + % for weight in range(100, 901, 100): + Weight${weight}, + % endfor + } } - #[inline] pub fn get_initial_value() -> ComputedValue { Weight400 } // normal + #[inline] pub fn get_initial_value() -> computed_value::T { Weight400 } // normal pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) - -> ComputedValue { + -> computed_value::T { match value { % for weight in range(100, 901, 100): SpecifiedWeight${weight} => Weight${weight}, @@ -504,8 +526,11 @@ pub mod longhands { <%self:single_component_value name="font-size" inherited="True"> pub use to_computed_value = super::super::common_types::computed::compute_Au; pub type SpecifiedValue = specified::Length; // Percentages are the same as em. - pub type ComputedValue = Au; - #[inline] pub fn get_initial_value() -> ComputedValue { + pub mod computed_value { + use super::super::Au; + pub type T = Au; + } + #[inline] pub fn get_initial_value() -> computed_value::T { Au::from_px(16) // medium } /// | @@ -537,8 +562,10 @@ pub mod longhands { // 'blink' is accepted in the parser but ignored. // Just not blinking the text is a conforming implementation per CSS 2.1. } - pub type ComputedValue = SpecifiedValue; - #[inline] pub fn get_initial_value() -> ComputedValue { + pub mod computed_value { + pub type T = super::SpecifiedValue; + } + #[inline] pub fn get_initial_value() -> computed_value::T { SpecifiedValue { underline: false, overline: false, line_through: false } // none } /// none | [ underline || overline || line-through || blink ] @@ -902,7 +929,7 @@ pub mod style_structs { % for name, longhands in LONGHANDS_PER_STYLE_STRUCT: pub struct ${name} { % for longhand in longhands: - ${longhand.ident}: longhands::${longhand.ident}::ComputedValue, + ${longhand.ident}: longhands::${longhand.ident}::computed_value::T, % endfor } % endfor @@ -1017,3 +1044,11 @@ pub fn cascade(applicable_declarations: &[@[PropertyDeclaration]], % endfor } } + + +// Only re-export the types for computed values. +pub mod computed_values { + % for property in LONGHANDS: + pub use ${property.ident} = super::longhands::${property.ident}::computed_value; + % endfor +} diff --git a/src/components/style/style.rc b/src/components/style/style.rc index 3527f3e9fea..553b550bd2b 100644 --- a/src/components/style/style.rc +++ b/src/components/style/style.rc @@ -18,11 +18,10 @@ extern mod servo_util (name = "util"); // The "real" public API -pub use self::selector_matching::{Stylist, StylesheetOrigin}; -pub use self::properties::cascade; -pub use self::properties::{PropertyDeclarationBlock, - parse_property_declaration_list}; // Style attributes - +pub use selector_matching::{Stylist, StylesheetOrigin}; +pub use properties::{cascade, computed_values}; +pub use properties::{PropertyDeclarationBlock, + parse_property_declaration_list}; // Style attributes // Things that need to be public to make the compiler happy pub mod stylesheets;