diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2017-06-07 01:31:02 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2017-06-07 01:31:02 +0200 |
commit | 7d09ce0495caba59848140b4c3e771c88a2f20bc (patch) | |
tree | f73d231ecc6c3ecb1c89093207dd063c0d462a3c | |
parent | f388c0ab1e4df8cbd94d58eb7657f36baaf813fe (diff) | |
download | servo-7d09ce0495caba59848140b4c3e771c88a2f20bc.tar.gz servo-7d09ce0495caba59848140b4c3e771c88a2f20bc.zip |
Use generics for initial-letter
The former version used ComputedValueAsSpecified, which means we were storing
specified numbers and integers in the computed value.
-rw-r--r-- | components/style/properties/gecko.mako.rs | 12 | ||||
-rw-r--r-- | components/style/properties/longhand/text.mako.rs | 77 | ||||
-rw-r--r-- | components/style/values/computed/mod.rs | 2 | ||||
-rw-r--r-- | components/style/values/computed/text.rs | 9 | ||||
-rw-r--r-- | components/style/values/generics/text.rs | 43 | ||||
-rw-r--r-- | components/style/values/specified/mod.rs | 2 | ||||
-rw-r--r-- | components/style/values/specified/text.rs | 20 |
7 files changed, 83 insertions, 82 deletions
diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 3c8e359e4de..08afc1674a1 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -3898,18 +3898,18 @@ fn static_assert() { } pub fn set_initial_letter(&mut self, v: longhands::initial_letter::computed_value::T) { - use properties::longhands::initial_letter::computed_value::T; + use values::generics::text::InitialLetter; match v { - T::Normal => { + InitialLetter::Normal => { self.gecko.mInitialLetterSize = 0.; self.gecko.mInitialLetterSink = 0; }, - T::Specified(size, sink) => { - self.gecko.mInitialLetterSize = size.get(); + InitialLetter::Specified(size, sink) => { + self.gecko.mInitialLetterSize = size; if let Some(sink) = sink { - self.gecko.mInitialLetterSink = sink.value(); + self.gecko.mInitialLetterSink = sink; } else { - self.gecko.mInitialLetterSink = size.get().floor() as i32; + self.gecko.mInitialLetterSink = size.floor() as i32; } } } diff --git a/components/style/properties/longhand/text.mako.rs b/components/style/properties/longhand/text.mako.rs index 0b4257b67f6..d636ed3cb64 100644 --- a/components/style/properties/longhand/text.mako.rs +++ b/components/style/properties/longhand/text.mako.rs @@ -287,72 +287,11 @@ ${helpers.predefined_type( ignored_when_colors_disabled=True, spec="https://drafts.csswg.org/css-text-decor/#propdef-text-decoration-color")} -<%helpers:longhand name="initial-letter" - animation_value_type="none" - products="gecko" - spec="https://drafts.csswg.org/css-inline/#sizing-drop-initials"> - use std::fmt; - use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - use values::specified::{Number, Integer}; - - impl ComputedValueAsSpecified for SpecifiedValue {} - no_viewport_percentage!(SpecifiedValue); - - #[derive(PartialEq, Clone, Debug)] - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub enum SpecifiedValue { - Normal, - Specified(Number, Option<Integer>) - } - - pub mod computed_value { - pub use super::SpecifiedValue as T; - } - - impl ToCss for SpecifiedValue { - fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - SpecifiedValue::Normal => try!(dest.write_str("normal")), - SpecifiedValue::Specified(size, sink) => { - try!(size.to_css(dest)); - if let Some(sink) = sink { - try!(dest.write_str(" ")); - try!(sink.to_css(dest)); - } - } - }; - - Ok(()) - } - } - - #[inline] - pub fn get_initial_value() -> computed_value::T { - computed_value::T::Normal - } - - #[inline] - pub fn get_initial_specified_value() -> SpecifiedValue { - SpecifiedValue::Normal - } - - /// normal | <number> <integer>? - pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> { - if input.try(|input| input.expect_ident_matching("normal")).is_ok() { - return Ok(SpecifiedValue::Normal); - } - - let size = try!(Number::parse_at_least_one(context, input)); - - match input.try(|input| Integer::parse(context, input)) { - Ok(number) => { - if number.value() < 1 { - return Err(()); - } - Ok(SpecifiedValue::Specified(size, Some(number))) - } - Err(()) => Ok(SpecifiedValue::Specified(size, None)), - } - } -</%helpers:longhand> +${helpers.predefined_type( + "initial-letter", + "InitialLetter", + "computed::InitialLetter::normal()", + initial_specified_value="specified::InitialLetter::normal()", + animation_value_type="none", + products="gecko", + spec="https://drafts.csswg.org/css-inline/#sizing-drop-initials")} diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index acfc09f5c80..632a4c87751 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -41,7 +41,7 @@ pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNumber, LengthOrP pub use self::length::{LengthOrPercentageOrAutoOrContent, LengthOrPercentageOrNone, LengthOrNone}; pub use self::length::{MaxLength, MozLength}; pub use self::position::Position; -pub use self::text::{LetterSpacing, LineHeight, WordSpacing}; +pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::transform::{TimingFunction, TransformOrigin}; pub mod background; diff --git a/components/style/values/computed/text.rs b/components/style/values/computed/text.rs index d45242015c4..857a985d851 100644 --- a/components/style/values/computed/text.rs +++ b/components/style/values/computed/text.rs @@ -6,9 +6,14 @@ use app_units::Au; use properties::animated_properties::Animatable; -use values::CSSFloat; +use values::{CSSInteger, CSSFloat}; use values::computed::length::{Length, LengthOrPercentage}; -use values::generics::text::{LineHeight as GenericLineHeight, Spacing}; +use values::generics::text::InitialLetter as GenericInitialLetter; +use values::generics::text::LineHeight as GenericLineHeight; +use values::generics::text::Spacing; + +/// A computed value for the `initial-letter` property. +pub type InitialLetter = GenericInitialLetter<CSSFloat, CSSInteger>; /// A computed value for the `letter-spacing` property. pub type LetterSpacing = Spacing<Length>; diff --git a/components/style/values/generics/text.rs b/components/style/values/generics/text.rs index 2ba5ae4af98..0652fa91224 100644 --- a/components/style/values/generics/text.rs +++ b/components/style/values/generics/text.rs @@ -11,7 +11,48 @@ use properties::animated_properties::Animatable; use std::fmt; use style_traits::ToCss; -/// A generic spacing value for the `letter-spacing` and `word-spacing` properties.alloc +/// A generic value for the `initial-letter` property. +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] +pub enum InitialLetter<Number, Integer> { + /// `normal` + Normal, + /// `<number> <integer>?` + Specified(Number, Option<Integer>), +} + +impl<N, I> InitialLetter<N, I> { + /// Returns `normal`. + #[inline] + pub fn normal() -> Self { + InitialLetter::Normal + } +} + +impl<N, I> ToCss for InitialLetter<N, I> +where + N: ToCss, + I: ToCss, +{ + fn to_css<W>(&self, dest: &mut W) -> fmt::Result + where + W: fmt::Write, + { + match *self { + InitialLetter::Normal => dest.write_str("normal"), + InitialLetter::Specified(ref size, ref sink) => { + size.to_css(dest)?; + if let Some(ref sink) = *sink { + dest.write_str(" ")?; + sink.to_css(dest)?; + } + Ok(()) + }, + } + } +} + +/// A generic spacing value for the `letter-spacing` and `word-spacing` properties. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] pub enum Spacing<Value> { diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index b198295064f..db2aa538c0e 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -45,7 +45,7 @@ pub use self::length::{Percentage, LengthOrNone, LengthOrNumber, LengthOrPercent pub use self::length::{LengthOrPercentageOrNone, LengthOrPercentageOrAutoOrContent, NoCalcLength}; pub use self::length::{MaxLength, MozLength}; pub use self::position::{Position, PositionComponent}; -pub use self::text::{LetterSpacing, LineHeight, WordSpacing}; +pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::transform::{TimingFunction, TransformOrigin}; pub use super::generics::grid::GridLine; diff --git a/components/style/values/specified/text.rs b/components/style/values/specified/text.rs index cc517ad4abb..2d277e143ee 100644 --- a/components/style/values/specified/text.rs +++ b/components/style/values/specified/text.rs @@ -9,10 +9,15 @@ use parser::{Parse, ParserContext}; use std::ascii::AsciiExt; use values::computed::{Context, ToComputedValue}; use values::computed::text::LineHeight as ComputedLineHeight; -use values::generics::text::{LineHeight as GenericLineHeight, Spacing}; -use values::specified::{AllowQuirks, Number}; +use values::generics::text::InitialLetter as GenericInitialLetter; +use values::generics::text::LineHeight as GenericLineHeight; +use values::generics::text::Spacing; +use values::specified::{AllowQuirks, Integer, Number}; use values::specified::length::{FontRelativeLength, Length, LengthOrPercentage, NoCalcLength}; +/// A specified type for the `initial-letter` property. +pub type InitialLetter = GenericInitialLetter<Number, Integer>; + /// A specified value for the `letter-spacing` property. pub type LetterSpacing = Spacing<Length>; @@ -22,6 +27,17 @@ pub type WordSpacing = Spacing<LengthOrPercentage>; /// A specified value for the `line-height` property. pub type LineHeight = GenericLineHeight<Number, LengthOrPercentage>; +impl Parse for InitialLetter { + fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { + if input.try(|i| i.expect_ident_matching("normal")).is_ok() { + return Ok(GenericInitialLetter::Normal); + } + let size = Number::parse_at_least_one(context, input)?; + let sink = input.try(|i| Integer::parse_positive(context, i)).ok(); + Ok(GenericInitialLetter::Specified(size, sink)) + } +} + impl Parse for LetterSpacing { fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { Spacing::parse_with(context, input, |c, i| { |