CSS: rexport types and variants for computed values.

This commit is contained in:
Simon Sapin 2013-10-17 17:32:33 +01:00
parent 6db57e6f72
commit a4c2e9dcf1
3 changed files with 109 additions and 75 deletions

View file

@ -4,34 +4,34 @@
pub use servo_util::geometry::Au; pub use servo_util::geometry::Au;
pub type Float = f64; pub type CSSFloat = f64;
pub mod specified { pub mod specified {
use std::ascii::StrAsciiExt; use std::ascii::StrAsciiExt;
use cssparser::*; use cssparser::*;
use super::{Au, Float}; use super::{Au, CSSFloat};
pub use CSSColor = cssparser::Color; pub use CSSColor = cssparser::Color;
#[deriving(Clone)] #[deriving(Clone)]
pub enum Length { pub enum Length {
Au_(Au), // application units Au_(Au), // application units
Em(Float), Em(CSSFloat),
Ex(Float), Ex(CSSFloat),
// XXX uncomment when supported: // XXX uncomment when supported:
// Ch(Float), // Ch(CSSFloat),
// Rem(Float), // Rem(CSSFloat),
// Vw(Float), // Vw(CSSFloat),
// Vh(Float), // Vh(CSSFloat),
// Vmin(Float), // Vmin(CSSFloat),
// Vmax(Float), // Vmax(CSSFloat),
} }
static AU_PER_PX: Float = 60.; static AU_PER_PX: CSSFloat = 60.;
static AU_PER_IN: Float = AU_PER_PX * 96.; static AU_PER_IN: CSSFloat = AU_PER_PX * 96.;
static AU_PER_CM: Float = AU_PER_IN / 2.54; static AU_PER_CM: CSSFloat = AU_PER_IN / 2.54;
static AU_PER_MM: Float = AU_PER_IN / 25.4; static AU_PER_MM: CSSFloat = AU_PER_IN / 25.4;
static AU_PER_PT: Float = AU_PER_IN / 72.; static AU_PER_PT: CSSFloat = AU_PER_IN / 72.;
static AU_PER_PC: Float = AU_PER_PT * 12.; static AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
impl Length { impl Length {
#[inline] #[inline]
fn parse_internal(input: &ComponentValue, negative_ok: bool) -> Option<Length> { fn parse_internal(input: &ComponentValue, negative_ok: bool) -> Option<Length> {
@ -48,7 +48,7 @@ pub mod specified {
pub fn parse_non_negative(input: &ComponentValue) -> Option<Length> { pub fn parse_non_negative(input: &ComponentValue) -> Option<Length> {
Length::parse_internal(input, /* negative_ok = */ false) Length::parse_internal(input, /* negative_ok = */ false)
} }
pub fn parse_dimension(value: Float, unit: &str) -> Option<Length> { pub fn parse_dimension(value: CSSFloat, unit: &str) -> Option<Length> {
match unit.to_ascii_lower().as_slice() { match unit.to_ascii_lower().as_slice() {
"px" => Some(Length::from_px(value)), "px" => Some(Length::from_px(value)),
"in" => Some(Au_(Au((value * AU_PER_IN) as i32))), "in" => Some(Au_(Au((value * AU_PER_IN) as i32))),
@ -62,7 +62,7 @@ pub mod specified {
} }
} }
#[inline] #[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)) Au_(Au((px_value * AU_PER_PX) as i32))
} }
} }
@ -70,7 +70,7 @@ pub mod specified {
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentage { pub enum LengthOrPercentage {
LP_Length(Length), 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 { impl LengthOrPercentage {
fn parse_internal(input: &ComponentValue, negative_ok: bool) fn parse_internal(input: &ComponentValue, negative_ok: bool)
@ -97,7 +97,7 @@ pub mod specified {
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentageOrAuto { pub enum LengthOrPercentageOrAuto {
LPA_Length(Length), 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, LPA_Auto,
} }
impl LengthOrPercentageOrAuto { impl LengthOrPercentageOrAuto {
@ -135,7 +135,7 @@ pub mod computed {
pub struct Context { pub struct Context {
current_color: cssparser::RGBA, current_color: cssparser::RGBA,
font_size: Au, font_size: Au,
font_weight: longhands::font_weight::ComputedValue, font_weight: longhands::font_weight::computed_value::T,
position: longhands::position::SpecifiedValue, position: longhands::position::SpecifiedValue,
float: longhands::float::SpecifiedValue, float: longhands::float::SpecifiedValue,
is_root_element: bool, is_root_element: bool,
@ -147,7 +147,7 @@ pub mod computed {
} }
#[inline] #[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 { pub fn compute_Au(value: specified::Length, context: &Context) -> Au {
match value { match value {
@ -163,7 +163,7 @@ pub mod computed {
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentage { pub enum LengthOrPercentage {
LP_Length(Au), LP_Length(Au),
LP_Percentage(Float), LP_Percentage(CSSFloat),
} }
pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context) pub fn compute_LengthOrPercentage(value: specified::LengthOrPercentage, context: &Context)
-> LengthOrPercentage { -> LengthOrPercentage {
@ -176,7 +176,7 @@ pub mod computed {
#[deriving(Clone)] #[deriving(Clone)]
pub enum LengthOrPercentageOrAuto { pub enum LengthOrPercentageOrAuto {
LPA_Length(Au), LPA_Length(Au),
LPA_Percentage(Float), LPA_Percentage(CSSFloat),
LPA_Auto, LPA_Auto,
} }
pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto, pub fn compute_LengthOrPercentageOrAuto(value: specified::LengthOrPercentageOrAuto,

View file

@ -68,6 +68,7 @@ pub mod longhands {
% if not no_super: % if not no_super:
use super::*; use super::*;
% endif % endif
pub use self::computed_value::*;
${caller.body()} ${caller.body()}
pub fn parse_declared(input: &[ComponentValue]) pub fn parse_declared(input: &[ComponentValue])
-> Option<DeclaredValue<SpecifiedValue>> { -> Option<DeclaredValue<SpecifiedValue>> {
@ -104,14 +105,16 @@ pub mod longhands {
<%self:single_component_value name="${name}" inherited="${inherited}"> <%self:single_component_value name="${name}" inherited="${inherited}">
// The computed value is the same as the specified value. // The computed value is the same as the specified value.
pub use to_computed_value = super::computed_as_specified; pub use to_computed_value = super::computed_as_specified;
#[deriving(Clone)] pub mod computed_value {
pub enum SpecifiedValue { #[deriving(Clone)]
% for value in values.split(): pub enum T {
${to_rust_ident(value)}, % for value in values.split():
% endfor ${to_rust_ident(value)},
% endfor
}
} }
pub type ComputedValue = SpecifiedValue; pub type SpecifiedValue = computed_value::T;
#[inline] pub fn get_initial_value() -> ComputedValue { #[inline] pub fn get_initial_value() -> computed_value::T {
${to_rust_ident(values.split()[0])} ${to_rust_ident(values.split()[0])}
} }
pub fn from_component_value(v: &ComponentValue) -> Option<SpecifiedValue> { pub fn from_component_value(v: &ComponentValue) -> Option<SpecifiedValue> {
@ -131,8 +134,10 @@ pub mod longhands {
<%self:single_component_value name="${name}" inherited="${inherited}"> <%self:single_component_value name="${name}" inherited="${inherited}">
pub use to_computed_value = super::super::common_types::computed::compute_${type}; pub use to_computed_value = super::super::common_types::computed::compute_${type};
pub type SpecifiedValue = specified::${type}; pub type SpecifiedValue = specified::${type};
pub type ComputedValue = computed::${type}; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { ${initial_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<SpecifiedValue> { #[inline] pub fn from_component_value(v: &ComponentValue) -> Option<SpecifiedValue> {
specified::${type}::${parse_method}(v) specified::${type}::${parse_method}(v)
} }
@ -169,7 +174,9 @@ pub mod longhands {
<%self:longhand name="border-${side}-style", no_super="True"> <%self:longhand name="border-${side}-style", no_super="True">
pub use super::border_top_style::*; pub use super::border_top_style::*;
pub type SpecifiedValue = super::border_top_style::SpecifiedValue; 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;
}
</%self:longhand> </%self:longhand>
% endfor % endfor
@ -187,15 +194,18 @@ pub mod longhands {
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
<%self:longhand name="border-${side}-width"> <%self:longhand name="border-${side}-width">
pub type SpecifiedValue = specified::Length; pub type SpecifiedValue = specified::Length;
pub type ComputedValue = Au; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { use super::super::Au;
pub type T = Au;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
Au::from_px(3) // medium Au::from_px(3) // medium
} }
pub fn parse(input: &[ComponentValue]) -> Option<SpecifiedValue> { pub fn parse(input: &[ComponentValue]) -> Option<SpecifiedValue> {
one_component_value(input).chain(parse_border_width) one_component_value(input).chain(parse_border_width)
} }
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context) pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> ComputedValue { -> computed_value::T {
if context.has_border_${side} { computed::compute_Au(value, context) } if context.has_border_${side} { computed::compute_Au(value, context) }
else { Au(0) } else { Au(0) }
} }
@ -231,7 +241,7 @@ pub mod longhands {
pub enum SpecifiedValue { pub enum SpecifiedValue {
SpecifiedNormal, SpecifiedNormal,
SpecifiedLength(specified::Length), SpecifiedLength(specified::Length),
SpecifiedNumber(Float), SpecifiedNumber(CSSFloat),
// percentage are the same as em. // percentage are the same as em.
} }
/// normal | <number> | <length> | <percentage> /// normal | <number> | <length> | <percentage>
@ -249,15 +259,18 @@ pub mod longhands {
_ => None, _ => None,
} }
} }
#[deriving(Clone)] pub mod computed_value {
pub enum ComputedValue { use super::super::{Au, CSSFloat};
Normal, #[deriving(Clone)]
Length(Au), pub enum T {
Number(Float), 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) pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> ComputedValue { -> computed_value::T {
match value { match value {
SpecifiedNormal => Normal, SpecifiedNormal => Normal,
SpecifiedLength(value) => Length(computed::compute_Au(value, context)), SpecifiedLength(value) => Length(computed::compute_Au(value, context)),
@ -290,17 +303,20 @@ pub mod longhands {
.map_move(SpecifiedLengthOrPercentage) .map_move(SpecifiedLengthOrPercentage)
} }
} }
#[deriving(Clone)] pub mod computed_value {
pub enum ComputedValue { use super::super::{Au, CSSFloat};
% for keyword in vertical_align_keywords: #[deriving(Clone)]
${to_rust_ident(keyword)}, pub enum T {
% endfor % for keyword in vertical_align_keywords:
Length(Au), ${to_rust_ident(keyword)},
Percentage(Float), % 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) pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> ComputedValue { -> computed_value::T {
match value { match value {
% for keyword in vertical_align_keywords: % for keyword in vertical_align_keywords:
Specified_${to_rust_ident(keyword)} => ${to_rust_ident(keyword)}, Specified_${to_rust_ident(keyword)} => ${to_rust_ident(keyword)},
@ -334,8 +350,10 @@ pub mod longhands {
<%self:raw_longhand name="color" inherited="True"> <%self:raw_longhand name="color" inherited="True">
pub use to_computed_value = super::computed_as_specified; pub use to_computed_value = super::computed_as_specified;
pub type SpecifiedValue = RGBA; pub type SpecifiedValue = RGBA;
pub type ComputedValue = SpecifiedValue; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { pub type T = super::SpecifiedValue;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */ RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */
} }
pub fn parse_specified(input: &[ComponentValue]) -> Option<DeclaredValue<SpecifiedValue>> { pub fn parse_specified(input: &[ComponentValue]) -> Option<DeclaredValue<SpecifiedValue>> {
@ -364,8 +382,10 @@ pub mod longhands {
// Monospace, // Monospace,
} }
pub type SpecifiedValue = ~[FontFamily]; pub type SpecifiedValue = ~[FontFamily];
pub type ComputedValue = SpecifiedValue; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { ~[FamilyName(~"serif")] } pub type T = super::SpecifiedValue;
}
#[inline] pub fn get_initial_value() -> computed_value::T { ~[FamilyName(~"serif")] }
/// <familiy-name># /// <familiy-name>#
/// <familiy-name> = <string> | [ <ident>+ ] /// <familiy-name> = <string> | [ <ident>+ ]
/// TODO: <generic-familiy> /// TODO: <generic-familiy>
@ -462,15 +482,17 @@ pub mod longhands {
_ => None _ => None
} }
} }
#[deriving(Clone)] pub mod computed_value {
pub enum ComputedValue { #[deriving(Clone)]
% for weight in range(100, 901, 100): pub enum T {
Weight${weight}, % for weight in range(100, 901, 100):
% endfor 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) pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> ComputedValue { -> computed_value::T {
match value { match value {
% for weight in range(100, 901, 100): % for weight in range(100, 901, 100):
SpecifiedWeight${weight} => Weight${weight}, SpecifiedWeight${weight} => Weight${weight},
@ -504,8 +526,11 @@ pub mod longhands {
<%self:single_component_value name="font-size" inherited="True"> <%self:single_component_value name="font-size" inherited="True">
pub use to_computed_value = super::super::common_types::computed::compute_Au; 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 SpecifiedValue = specified::Length; // Percentages are the same as em.
pub type ComputedValue = Au; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { use super::super::Au;
pub type T = Au;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
Au::from_px(16) // medium Au::from_px(16) // medium
} }
/// <length> | <percentage> /// <length> | <percentage>
@ -537,8 +562,10 @@ pub mod longhands {
// 'blink' is accepted in the parser but ignored. // 'blink' is accepted in the parser but ignored.
// Just not blinking the text is a conforming implementation per CSS 2.1. // Just not blinking the text is a conforming implementation per CSS 2.1.
} }
pub type ComputedValue = SpecifiedValue; pub mod computed_value {
#[inline] pub fn get_initial_value() -> ComputedValue { pub type T = super::SpecifiedValue;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
SpecifiedValue { underline: false, overline: false, line_through: false } // none SpecifiedValue { underline: false, overline: false, line_through: false } // none
} }
/// none | [ underline || overline || line-through || blink ] /// none | [ underline || overline || line-through || blink ]
@ -902,7 +929,7 @@ pub mod style_structs {
% for name, longhands in LONGHANDS_PER_STYLE_STRUCT: % for name, longhands in LONGHANDS_PER_STYLE_STRUCT:
pub struct ${name} { pub struct ${name} {
% for longhand in longhands: % for longhand in longhands:
${longhand.ident}: longhands::${longhand.ident}::ComputedValue, ${longhand.ident}: longhands::${longhand.ident}::computed_value::T,
% endfor % endfor
} }
% endfor % endfor
@ -1017,3 +1044,11 @@ pub fn cascade(applicable_declarations: &[@[PropertyDeclaration]],
% endfor % 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
}

View file

@ -18,11 +18,10 @@ extern mod servo_util (name = "util");
// The "real" public API // The "real" public API
pub use self::selector_matching::{Stylist, StylesheetOrigin}; pub use selector_matching::{Stylist, StylesheetOrigin};
pub use self::properties::cascade; pub use properties::{cascade, computed_values};
pub use self::properties::{PropertyDeclarationBlock, pub use properties::{PropertyDeclarationBlock,
parse_property_declaration_list}; // Style attributes parse_property_declaration_list}; // Style attributes
// Things that need to be public to make the compiler happy // Things that need to be public to make the compiler happy
pub mod stylesheets; pub mod stylesheets;