Add to_computed method for CalcLengthOrPercentage

* Add to_computed method to calculate `calc()` value with parent size,
if parent container size is `None`, the result will be `None`.
* Add from_option method for `MaybeAuto`, to construct from
`Option<Au>`.
* Update some test case.
This commit is contained in:
石博文 2017-04-20 11:27:19 +08:00
parent 5062c4b117
commit 22f99c71b9
No known key found for this signature in database
GPG key ID: FC9610D6400A173C
8 changed files with 47 additions and 32 deletions

View file

@ -76,6 +76,16 @@ impl CalcLengthOrPercentage {
pub fn percentage(&self) -> CSSFloat {
self.percentage.unwrap_or(0.)
}
/// If there are special rules for computing percentages in a value (e.g. the height property),
/// they apply whenever a calc() expression contains percentages.
pub fn to_computed(&self, container_len: Option<Au>) -> Option<Au> {
match (container_len, self.percentage) {
(Some(len), Some(percent)) => Some(self.length + len.scale_by(percent)),
(_, None) => Some(self.length),
_ => None,
}
}
}
impl From<LengthOrPercentage> for CalcLengthOrPercentage {