Skip to content

Commit

Permalink
servo: Merge #18131 - style: Multiple calc serialization fixes (from …
Browse files Browse the repository at this point in the history
…emilio:calc-serialization); r=canaltinova

This puts us in line with the spec as written, except for caveat in w3c/csswg-drafts#1731.

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

Source-Repo: https://github.com/servo/servo
Source-Revision: 92176d1152257e57e81dcd9204d59348ce9ff519
  • Loading branch information
emilio committed Aug 18, 2017
1 parent e573ec1 commit 5caf3de
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
26 changes: 13 additions & 13 deletions servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion servo/components/style/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ servo = ["serde", "heapsize", "heapsize_derive",
gecko_debug = ["nsstring_vendor/gecko_debug"]

[dependencies]
app_units = "0.5.2"
app_units = "0.5.3"
arrayvec = "0.3.20"
arraydeque = "0.2.3"
atomic_refcell = "0.1"
Expand Down
20 changes: 15 additions & 5 deletions servo/components/style/values/computed/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(")")
}
}

Expand Down
8 changes: 7 additions & 1 deletion servo/components/style/values/computed/percentage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
44 changes: 30 additions & 14 deletions servo/components/style/values/specified/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,35 @@ impl HasViewportPercentage for CalcLengthOrPercentage {
}

impl ToCss for CalcLengthOrPercentage {
/// https://drafts.csswg.org/css-values/#calc-serialize
///
/// FIXME(emilio): Should this simplify away zeros?
#[allow(unused_assignments)]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use num_traits::Zero;

let mut first_value = true;
macro_rules! first_value_check {
() => {
($val:expr) => {
if !first_value {
dest.write_str(" + ")?;
} else {
first_value = false;
dest.write_str(if $val < Zero::zero() {
" - "
} else {
" + "
})?;
} else if $val < Zero::zero() {
dest.write_str("-")?;
}
first_value = false;
};
}

macro_rules! serialize {
( $( $val:ident ),* ) => {
$(
if let Some(val) = self.$val {
first_value_check!();
val.to_css(dest)?;
first_value_check!(val);
val.abs().to_css(dest)?;
dest.write_str(stringify!($val))?;
}
)*
Expand All @@ -117,24 +127,30 @@ impl ToCss for CalcLengthOrPercentage {

dest.write_str("calc(")?;

serialize!(ch, em, ex, rem, vh, vmax, vmin, vw);
// NOTE(emilio): Percentages first because of web-compat problems, see:
// https://github.com/w3c/csswg-drafts/issues/1731
if let Some(val) = self.percentage {
first_value_check!(val.0);
val.abs().to_css(dest)?;
}

// NOTE(emilio): The order here it's very intentional, and alphabetic
// per the spec linked above.
serialize!(ch, em, ex);

#[cfg(feature = "gecko")]
{
serialize!(mozmm);
}

if let Some(val) = self.absolute {
first_value_check!();
val.to_css(dest)?;
first_value_check!(val);
val.abs().to_css(dest)?;
}

if let Some(val) = self.percentage {
first_value_check!();
val.to_css(dest)?;
}
serialize!(rem, vh, vmax, vmin, vw);

write!(dest, ")")
dest.write_str(")")
}
}

Expand Down

0 comments on commit 5caf3de

Please sign in to comment.