Skip to content

Commit a70af60

Browse files
author
bors-servo
authored
Auto merge of #15063 - Wafflespeanut:calc, r=heycam
Cleaning up CalcLengthOrPercentage <!-- Please describe your changes on the following line: --> We don't really need enum variants in `CalcLengthOrPercentage`. Given that we already have the information (whether it's `vw`, `ch`, etc.) in the struct fields, we could just store `Option<CSSFloat>` there, and modify our `ToCss` implementation a bit. cc #15061 r? @Manishearth or anyone interested --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors <!-- Either: --> - [x] These changes do not require tests because it's a refactor <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- 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/15063) <!-- Reviewable:end -->
2 parents 363f590 + 3ac73ed commit a70af60

File tree

3 files changed

+60
-38
lines changed

3 files changed

+60
-38
lines changed

components/style/values/computed/length.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::fmt;
1010
use style_traits::ToCss;
1111
use super::{Number, ToComputedValue, Context};
1212
use values::{Auto, CSSFloat, Either, None_, Normal, specified};
13+
use values::specified::length::{FontRelativeLength, ViewportPercentageLength};
1314

1415
pub use cssparser::Color as CSSColor;
1516
pub use super::image::{EndingShape as GradientShape, Gradient, GradientKind, Image};
@@ -105,29 +106,35 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
105106
length += absolute;
106107
}
107108

108-
for val in &[self.vw, self.vh, self.vmin, self.vmax] {
109+
for val in &[self.vw.map(ViewportPercentageLength::Vw),
110+
self.vh.map(ViewportPercentageLength::Vh),
111+
self.vmin.map(ViewportPercentageLength::Vmin),
112+
self.vmax.map(ViewportPercentageLength::Vmax)] {
109113
if let Some(val) = *val {
110114
length += val.to_computed_value(context.viewport_size());
111115
}
112116
}
113117

114-
for val in &[self.ch, self.em, self.ex, self.rem] {
118+
for val in &[self.ch.map(FontRelativeLength::Ch),
119+
self.em.map(FontRelativeLength::Em),
120+
self.ex.map(FontRelativeLength::Ex),
121+
self.rem.map(FontRelativeLength::Rem)] {
115122
if let Some(val) = *val {
116123
length += val.to_computed_value(context, /* use inherited */ false);
117124
}
118125
}
119126

120127
CalcLengthOrPercentage {
121128
length: length,
122-
percentage: self.percentage.map(|p| p.0),
129+
percentage: self.percentage,
123130
}
124131
}
125132

126133
#[inline]
127134
fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
128135
specified::CalcLengthOrPercentage {
129136
absolute: Some(computed.length),
130-
percentage: computed.percentage.map(specified::Percentage),
137+
percentage: computed.percentage,
131138
..Default::default()
132139
}
133140
}

components/style/values/specified/length.rs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,15 @@ pub enum CalcUnit {
440440
#[allow(missing_docs)]
441441
pub struct CalcLengthOrPercentage {
442442
pub absolute: Option<Au>,
443-
pub vw: Option<ViewportPercentageLength>,
444-
pub vh: Option<ViewportPercentageLength>,
445-
pub vmin: Option<ViewportPercentageLength>,
446-
pub vmax: Option<ViewportPercentageLength>,
447-
pub em: Option<FontRelativeLength>,
448-
pub ex: Option<FontRelativeLength>,
449-
pub ch: Option<FontRelativeLength>,
450-
pub rem: Option<FontRelativeLength>,
451-
pub percentage: Option<Percentage>,
443+
pub vw: Option<CSSFloat>,
444+
pub vh: Option<CSSFloat>,
445+
pub vmin: Option<CSSFloat>,
446+
pub vmax: Option<CSSFloat>,
447+
pub em: Option<CSSFloat>,
448+
pub ex: Option<CSSFloat>,
449+
pub ch: Option<CSSFloat>,
450+
pub rem: Option<CSSFloat>,
451+
pub percentage: Option<CSSFloat>,
452452
}
453453

454454
impl CalcLengthOrPercentage {
@@ -671,15 +671,15 @@ impl CalcLengthOrPercentage {
671671

672672
Ok(CalcLengthOrPercentage {
673673
absolute: absolute.map(Au),
674-
vw: vw.map(ViewportPercentageLength::Vw),
675-
vh: vh.map(ViewportPercentageLength::Vh),
676-
vmax: vmax.map(ViewportPercentageLength::Vmax),
677-
vmin: vmin.map(ViewportPercentageLength::Vmin),
678-
em: em.map(FontRelativeLength::Em),
679-
ex: ex.map(FontRelativeLength::Ex),
680-
ch: ch.map(FontRelativeLength::Ch),
681-
rem: rem.map(FontRelativeLength::Rem),
682-
percentage: percentage.map(Percentage),
674+
vw: vw,
675+
vh: vh,
676+
vmax: vmax,
677+
vmin: vmin,
678+
em: em,
679+
ex: ex,
680+
ch: ch,
681+
rem: rem,
682+
percentage: percentage,
683683
})
684684
}
685685

@@ -767,21 +767,26 @@ impl ToCss for CalcLengthOrPercentage {
767767
};
768768
}
769769

770+
let mut first_value = true;
771+
macro_rules! first_value_check {
772+
() => {
773+
if !first_value {
774+
try!(dest.write_str(" + "));
775+
} else {
776+
first_value = false;
777+
}
778+
};
779+
}
780+
770781
macro_rules! serialize {
771782
( $( $val:ident ),* ) => {
772-
{
773-
let mut first_value = true;
774-
$(
775-
if let Some(val) = self.$val {
776-
if !first_value {
777-
try!(write!(dest, " + "));
778-
} else {
779-
first_value = false;
780-
}
781-
try!(val.to_css(dest));
782-
}
783-
)*
784-
}
783+
$(
784+
if let Some(val) = self.$val {
785+
first_value_check!();
786+
try!(val.to_css(dest));
787+
try!(dest.write_str(stringify!($val)));
788+
}
789+
)*
785790
};
786791
}
787792

@@ -792,11 +797,21 @@ impl ToCss for CalcLengthOrPercentage {
792797
try!(write!(dest, "calc("));
793798
}
794799

795-
serialize!(ch, em, ex, absolute, rem, vh, vmax, vmin, vw, percentage);
800+
serialize!(ch, em, ex, rem, vh, vmax, vmin, vw);
801+
if let Some(val) = self.absolute {
802+
first_value_check!();
803+
try!(val.to_css(dest));
804+
}
805+
806+
if let Some(val) = self.percentage {
807+
first_value_check!();
808+
try!(write!(dest, "{}%", val * 100.));
809+
}
796810

797811
if count > 1 {
798812
try!(write!(dest, ")"));
799813
}
814+
800815
Ok(())
801816
}
802817
}

tests/wpt/mozilla/tests/mozilla/calc.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040

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

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

5151
];
5252

0 commit comments

Comments
 (0)