Skip to content

Commit

Permalink
Fix CalcLengthOrPercentage serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
canova committed Jan 25, 2017
1 parent 2a32cf1 commit 29ac171
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 39 deletions.
27 changes: 2 additions & 25 deletions components/style/values/specified/length.rs
Expand Up @@ -761,20 +761,6 @@ impl HasViewportPercentage for CalcLengthOrPercentage {
impl ToCss for CalcLengthOrPercentage {
#[allow(unused_assignments)]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
macro_rules! count {
( $( $val:ident ),* ) => {
{
let mut count = 0;
$(
if let Some(_) = self.$val {
count += 1;
}
)*
count
}
};
}

let mut first_value = true;
macro_rules! first_value_check {
() => {
Expand All @@ -798,12 +784,7 @@ impl ToCss for CalcLengthOrPercentage {
};
}

let count = count!(ch, em, ex, absolute, rem, vh, vmax, vmin, vw, percentage);
assert!(count > 0);

if count > 1 {
try!(write!(dest, "calc("));
}
try!(write!(dest, "calc("));

serialize!(ch, em, ex, rem, vh, vmax, vmin, vw);
if let Some(val) = self.absolute {
Expand All @@ -816,11 +797,7 @@ impl ToCss for CalcLengthOrPercentage {
try!(write!(dest, "{}%", val * 100.));
}

if count > 1 {
try!(write!(dest, ")"));
}

Ok(())
write!(dest, ")")
}
}

Expand Down
29 changes: 15 additions & 14 deletions tests/wpt/mozilla/tests/mozilla/calc.html
Expand Up @@ -15,37 +15,38 @@
var div = document.getElementById('inner');

var widthTests = [
['calc(10px)', '10px', '10px'],
['calc(10px)', 'calc(10px)', '10px'],

// Basic Arithmetic
['calc(10px + 10px)', '20px', '20px'],
['calc(10px - 5px)', '5px', '5px'],
['calc(2 * 10px)', '20px', '20px'],
['calc(10px / 2)', '5px', '5px'],
['calc(10px + 10px)', 'calc(20px)', '20px'],
['calc(10px - 5px)', 'calc(5px)', '5px'],
['calc(2 * 10px)', 'calc(20px)', '20px'],
['calc(10px / 2)', 'calc(5px)', '5px'],

// Parse ok
['calc(20px/2)', '10px', '10px'],
['calc(10px*2)', '20px', '20px'],
['calc(20px/2)', 'calc(10px)', '10px'],
['calc(10px*2)', 'calc(20px)', '20px'],

// Parse errors - value left over from previous test
['calc(10px-10px)', '20px', '20px'],
['calc(5px+5px)', '20px', '20px'],
['calc(10px-10px)', 'calc(20px)', '20px'],
['calc(5px+5px)', 'calc(20px)', '20px'],
['calc()', 'calc(20px)', '20px'],

// Combining units
['calc(10px + 10em)', 'calc(10em + 10px)', '170px'],
['calc(10px + 10em - 10px)', 'calc(10em + 0px)', '160px'],

// Fold absolute units
['calc(1px + 1pt + 1pc + 1in + 1cm + 1mm)', '155.88333333333333px', '155.88333333333333px'],
['calc(1px + 1pt + 1pc + 1in + 1cm + 1mm)', 'calc(155.88333333333333px)', '155.88333333333333px'],

// Alphabetical order
['calc(0ch + 0px + 0pt + 0pc + 0in + 0cm + 0mm + 0rem + 0em + 0ex + 0% + 0vw + 0vh + 0vmin + 0vmax)',
'calc(0ch + 0em + 0ex + 0rem + 0vh + 0vmax + 0vmin + 0vw + 0px + 0%)',
'0px'],

// Simplification
['calc((2 - 1) * 10px)', '10px', '10px'],
['calc(((3 - 1) * (8 + 4)) * 10px)', '240px', '240px'],
['calc((2 - 1) * 10px)', 'calc(10px)', '10px'],
['calc(((3 - 1) * (8 + 4)) * 10px)', 'calc(240px)', '240px'],
['calc(5 * (20px / 2 + 7 * (3em + 12px/4 + (8 - 2) * 2rem)))', 'calc(105em + 420rem + 155px)', '8555px'],

];
Expand Down Expand Up @@ -75,7 +76,7 @@
lengthProperties.forEach(function(prop) {
test(function() {
div.style.setProperty(prop, 'calc(1px)');
assert_equals(div.style.getPropertyValue(prop), '1px');
assert_equals(div.style.getPropertyValue(prop), 'calc(1px)');
}, 'calc for ' + prop);
});

Expand Down Expand Up @@ -130,7 +131,7 @@

var otherProperties = [
['border-width', 'calc(1px)', '1px 1px 1px 1px'],
['border-spacing', 'calc(1px)', '1px 1px'],
['border-spacing', 'calc(1px)', 'calc(1px) calc(1px)'],
['transform-origin', 'calc(1px + 0%)', 'calc(1px + 0%) 50% 0px'],
['perspective-origin', 'calc(1px + 0%)', 'calc(1px + 0%) 50%'],
['background-size', 'calc(1px + 0%)', 'calc(1px + 0%) auto'],
Expand Down

0 comments on commit 29ac171

Please sign in to comment.