Auto merge of #18131 - emilio:calc-serialization, r=canaltinova

style: Multiple calc serialization fixes.

This puts us in line with the spec as written, except for caveat in https://github.com/w3c/csswg-drafts/issues/1731.

Need to fix computed calc() too, I'm less confident that we won't need to change test expectations.

<!-- 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/18131)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-08-18 04:42:12 -05:00 committed by GitHub
commit 92176d1152
8 changed files with 122 additions and 46 deletions

View file

@ -195,11 +195,21 @@ impl From<LengthOrPercentageOrNone> for Option<CalcLengthOrPercentage> {
impl ToCss for CalcLengthOrPercentage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match (self.length, self.percentage) {
(l, Some(p)) if l == Au(0) => p.to_css(dest),
(l, Some(p)) => write!(dest, "calc({}px + {}%)", Au::to_px(l), p.0 * 100.),
(l, None) => write!(dest, "{}px", Au::to_px(l)),
}
use num_traits::Zero;
let (length, percentage) = match (self.length, self.percentage) {
(l, None) => return l.to_css(dest),
(l, Some(p)) if l == Au(0) => return p.to_css(dest),
(l, Some(p)) => (l, p),
};
dest.write_str("calc(")?;
percentage.to_css(dest)?;
dest.write_str(if length < Zero::zero() { " - " } else { " + " })?;
length.abs().to_css(dest)?;
dest.write_str(")")
}
}

View file

@ -11,7 +11,7 @@ use values::CSSFloat;
use values::animated::ToAnimatedZero;
/// A computed percentage.
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default, HasViewportPercentage, PartialEq)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default, HasViewportPercentage, PartialEq, PartialOrd)]
#[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))]
pub struct Percentage(pub CSSFloat);
@ -27,6 +27,12 @@ impl Percentage {
pub fn hundred() -> Self {
Percentage(1.)
}
/// Returns the absolute value for this percentage.
#[inline]
pub fn abs(&self) -> Self {
Percentage(self.0.abs())
}
}
/// https://drafts.csswg.org/css-transitions/#animtype-percentage