/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Specified time values.
usecrate::parser::{Parse, ParserContext}; usecrate::values::computed::time::Time as ComputedTime; usecrate::values::computed::{Context, ToComputedValue}; usecrate::values::specified::calc::CalcNode; usecrate::values::CSSFloat; usecrate::Zero; use cssparser::{Parser, Token}; use std::fmt::{self, Write}; use style_traits::values::specified::AllowedNumericType; use style_traits::{CssWriter, ParseError, SpecifiedValueInfo, StyleParseErrorKind, ToCss};
/// A time value according to CSS-VALUES § 6.2. #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToShmem)] pubstruct Time {
seconds: CSSFloat,
unit: TimeUnit,
calc_clamping_mode: Option<AllowedNumericType>,
}
/// A time unit. #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)] pubenum TimeUnit { /// `s`
Second, /// `ms`
Millisecond,
}
impl Time { /// Returns a time value that represents `seconds` seconds. pubfn from_seconds_with_calc_clamping_mode(
seconds: CSSFloat,
calc_clamping_mode: Option<AllowedNumericType>,
) -> Self {
Time {
seconds,
unit: TimeUnit::Second,
calc_clamping_mode,
}
}
/// Returns a time value that represents `seconds` seconds. pubfn from_seconds(seconds: CSSFloat) -> Self { Self::from_seconds_with_calc_clamping_mode(seconds, None)
}
/// Returns the time in fractional seconds. pubfn seconds(self) -> CSSFloat { self.seconds
}
/// Returns the unit of the time. #[inline] pubfn unit(&self) -> &'static str { matchself.unit {
TimeUnit::Second => "s",
TimeUnit::Millisecond => "ms",
}
}
let location = input.current_source_location(); match *input.next()? { // Note that we generally pass ParserContext to is_ok() to check // that the ParserMode of the ParserContext allows all numeric // values for SMIL regardless of clamping_mode, but in this Time // value case, the value does not animate for SMIL at all, so we use // ParsingMode::DEFAULT directly.
Token::Dimension {
value, ref unit, ..
} if clamping_mode.is_ok(ParsingMode::DEFAULT, value) => {
Time::parse_dimension(value, unit)
.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
},
Token::Function(ref name) => { let function = CalcNode::math_function(context, name, location)?;
CalcNode::parse_time(context, input, clamping_mode, function)
}, ref t => return Err(location.new_unexpected_token_error(t.clone())),
}
}
impl Zero for Time { #[inline] fn zero() -> Self { Self::from_seconds(0.0)
}
#[inline] fn is_zero(&self) -> bool { // The unit doesn't matter, i.e. `s` and `ms` are the same for zero. self.seconds == 0.0 && self.calc_clamping_mode.is_none()
}
}
impl ToComputedValue for Time { type ComputedValue = ComputedTime;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.