diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-08-04 05:35:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-04 05:35:41 -0500 |
commit | 7b299caf9b547412390512ff07a119a47d0cc1bf (patch) | |
tree | 4d821ee050a369fca6a9a26c50401199ac1a6803 | |
parent | 187254bf26fc9974c65d061a146bbbbfb484fbd3 (diff) | |
parent | 35d8f98fdd1895f66ad53cb1b5b426775545a1a7 (diff) | |
download | servo-7b299caf9b547412390512ff07a119a47d0cc1bf.tar.gz servo-7b299caf9b547412390512ff07a119a47d0cc1bf.zip |
Auto merge of #17970 - upsuper:time-unit, r=emilio
Preserve unit in specified time value
This fixes #15346.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17970)
<!-- Reviewable:end -->
-rw-r--r-- | components/style/values/specified/mod.rs | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 1728d4113d4..02ad9ac32f0 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -311,11 +311,22 @@ impl BorderStyle { } } +/// Time unit. +#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, Eq)] +#[cfg_attr(feature = "servo", derive(HeapSizeOf))] +pub enum TimeUnit { + /// `s` + Second, + /// `ms` + Millisecond, +} + /// A time in seconds according to CSS-VALUES § 6.2. -#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct Time { seconds: CSSFloat, + unit: TimeUnit, was_calc: bool, } @@ -324,6 +335,7 @@ impl Time { pub fn from_seconds(seconds: CSSFloat) -> Self { Time { seconds: seconds, + unit: TimeUnit::Second, was_calc: false, } } @@ -345,14 +357,15 @@ impl Time { from_calc: bool) -> Result<Time, ()> { - let seconds = match_ignore_ascii_case! { unit, - "s" => value, - "ms" => value / 1000.0, + let (seconds, unit) = match_ignore_ascii_case! { unit, + "s" => (value, TimeUnit::Second), + "ms" => (value / 1000.0, TimeUnit::Millisecond), _ => return Err(()) }; Ok(Time { seconds: seconds, + unit: unit, was_calc: from_calc, }) } @@ -361,6 +374,7 @@ impl Time { pub fn from_calc(seconds: CSSFloat) -> Self { Time { seconds: seconds, + unit: TimeUnit::Second, was_calc: true, } } @@ -409,6 +423,7 @@ impl ToComputedValue for Time { fn from_computed_value(computed: &Self::ComputedValue) -> Self { Time { seconds: computed.seconds(), + unit: TimeUnit::Second, was_calc: false, } } @@ -425,7 +440,16 @@ impl ToCss for Time { if self.was_calc { dest.write_str("calc(")?; } - write!(dest, "{}s", self.seconds)?; + match self.unit { + TimeUnit::Second => { + self.seconds.to_css(dest)?; + dest.write_str("s")?; + } + TimeUnit::Millisecond => { + (self.seconds * 1000.).to_css(dest)?; + dest.write_str("ms")?; + } + } if self.was_calc { dest.write_str(")")?; } |