Skip to content

Commit

Permalink
Auto merge of #18131 - emilio:calc-serialization, r=canaltinova
Browse files Browse the repository at this point in the history
style: Multiple calc serialization fixes.

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.

<!-- 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 -->
  • Loading branch information
bors-servo committed Aug 18, 2017
2 parents 90a75d4 + 52d6838 commit 548ee68
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
12 changes: 12 additions & 0 deletions components/style/values/computed/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ impl From<LengthOrPercentageOrNone> for Option<CalcLengthOrPercentage> {

impl ToCss for CalcLengthOrPercentage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// FIXME(emilio): This should be consistent with specified calc().
//
// Also, serialisation here is kinda weird with negative percentages,
// for example, as of right now:
//
// calc(10px - 5%)
//
// would serialize as:
//
// calc(10px + -5%)
//
// Need to update and run through try.
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.),
Expand Down
8 changes: 7 additions & 1 deletion 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 components/style/values/specified/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,33 @@ impl HasViewportPercentage for CalcLengthOrPercentage {
}

impl ToCss for CalcLengthOrPercentage {
/// https://drafts.csswg.org/css-values/#calc-serialize
#[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 +125,32 @@ 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);
// FIXME(emilio): Au::abs() would be nice.
let abs = if val < Zero::zero() { -val } else { 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
10 changes: 10 additions & 0 deletions tests/wpt/mozilla/meta/MANIFEST.json
Original file line number Diff line number Diff line change
Expand Up @@ -13512,6 +13512,12 @@
{}
]
],
"css/calc-serialization.html": [
[
"/_mozilla/css/calc-serialization.html",
{}
]
],
"css/empty-keyframes.html": [
[
"/_mozilla/css/empty-keyframes.html",
Expand Down Expand Up @@ -22847,6 +22853,10 @@
"b9e77c824d0c96e51d51fb8f1e923d3ab67be027",
"testharness"
],
"css/calc-serialization.html": [
"628597eb36bf21a1ec982c7f6935ee7949c62044",
"testharness"
],
"css/canvas_as_block_element_a.html": [
"668d93da2dab9722998cc7c5785c20e2ab9a1ced",
"reftest"
Expand Down
34 changes: 34 additions & 0 deletions tests/wpt/mozilla/tests/css/calc-serialization.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<meta charset="utf-8">
<title>CSS Values and Units: calc() serialization.</title>
<link rel="author" title="Emilio Cobos Álvarez" href="mailto:emilio@mozilla.com">
<link rel="help" href="https://drafts.csswg.org/css-values/#calc-serialize">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!-- FIXME(emilio): Upstream when spec stops being under discussion -->
<div id="content"></div>
<script>

// NOTE(emilio): This intentionally doesn't test for order of percentages
// because of:
//
// https://github.com/w3c/csswg-drafts/issues/1731

test(function() {
// specified -> expected
var values = {
"calc(10px + 1vmin)": "calc(10px + 1vmin)",
"calc(10px + 1em)": "calc(1em + 10px)",
"calc(1vmin - 10px)": "calc(-10px + 1vmin)",
"calc(-10px + 1em)": "calc(1em - 10px)",
"calc(-10px)": "calc(-10px)",
};

var content = document.getElementById("content");

for (var prop in values) {
content.style.width = prop;
assert_equals(content.style.width, values[prop], "Serialization of " + prop);
}
}, "calc() serialization")
</script>

0 comments on commit 548ee68

Please sign in to comment.