Preserve unit in specified time value

This commit is contained in:
Xidorn Quan 2017-08-04 09:57:46 +10:00
parent 7cee7d7fcc
commit 35d8f98fdd

View file

@ -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. /// 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))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Time { pub struct Time {
seconds: CSSFloat, seconds: CSSFloat,
unit: TimeUnit,
was_calc: bool, was_calc: bool,
} }
@ -324,6 +335,7 @@ impl Time {
pub fn from_seconds(seconds: CSSFloat) -> Self { pub fn from_seconds(seconds: CSSFloat) -> Self {
Time { Time {
seconds: seconds, seconds: seconds,
unit: TimeUnit::Second,
was_calc: false, was_calc: false,
} }
} }
@ -345,14 +357,15 @@ impl Time {
from_calc: bool) from_calc: bool)
-> Result<Time, ()> -> Result<Time, ()>
{ {
let seconds = match_ignore_ascii_case! { unit, let (seconds, unit) = match_ignore_ascii_case! { unit,
"s" => value, "s" => (value, TimeUnit::Second),
"ms" => value / 1000.0, "ms" => (value / 1000.0, TimeUnit::Millisecond),
_ => return Err(()) _ => return Err(())
}; };
Ok(Time { Ok(Time {
seconds: seconds, seconds: seconds,
unit: unit,
was_calc: from_calc, was_calc: from_calc,
}) })
} }
@ -361,6 +374,7 @@ impl Time {
pub fn from_calc(seconds: CSSFloat) -> Self { pub fn from_calc(seconds: CSSFloat) -> Self {
Time { Time {
seconds: seconds, seconds: seconds,
unit: TimeUnit::Second,
was_calc: true, was_calc: true,
} }
} }
@ -409,6 +423,7 @@ impl ToComputedValue for Time {
fn from_computed_value(computed: &Self::ComputedValue) -> Self { fn from_computed_value(computed: &Self::ComputedValue) -> Self {
Time { Time {
seconds: computed.seconds(), seconds: computed.seconds(),
unit: TimeUnit::Second,
was_calc: false, was_calc: false,
} }
} }
@ -425,7 +440,16 @@ impl ToCss for Time {
if self.was_calc { if self.was_calc {
dest.write_str("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 { if self.was_calc {
dest.write_str(")")?; dest.write_str(")")?;
} }