mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Properly serialize % values in calc expressions
This commit is contained in:
parent
cfa1e467f1
commit
6573e8088c
4 changed files with 50 additions and 41 deletions
|
@ -387,7 +387,7 @@ pub mod specified {
|
|||
pub em: Option<FontRelativeLength>,
|
||||
pub ex: Option<FontRelativeLength>,
|
||||
pub rem: Option<FontRelativeLength>,
|
||||
pub percentage: Option<CSSFloat>,
|
||||
pub percentage: Option<Percentage>,
|
||||
}
|
||||
impl Calc {
|
||||
fn parse_sum(input: &mut Parser) -> Result<CalcSumNode, ()> {
|
||||
|
@ -603,7 +603,7 @@ pub mod specified {
|
|||
em: em.map(FontRelativeLength::Em),
|
||||
ex: ex.map(FontRelativeLength::Ex),
|
||||
rem: rem.map(FontRelativeLength::Rem),
|
||||
percentage: percentage,
|
||||
percentage: percentage.map(Percentage),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -659,10 +659,19 @@ pub mod specified {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug, HeapSizeOf)]
|
||||
pub struct Percentage(pub CSSFloat); // [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
|
||||
impl ToCss for Percentage {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
write!(dest, "{}%", self.0 * 100.)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug, HeapSizeOf)]
|
||||
pub enum LengthOrPercentage {
|
||||
Length(Length),
|
||||
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
Percentage(Percentage),
|
||||
Calc(Calc),
|
||||
}
|
||||
|
||||
|
@ -670,8 +679,7 @@ pub mod specified {
|
|||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
match self {
|
||||
&LengthOrPercentage::Length(length) => length.to_css(dest),
|
||||
&LengthOrPercentage::Percentage(percentage)
|
||||
=> write!(dest, "{}%", percentage * 100.),
|
||||
&LengthOrPercentage::Percentage(percentage) => percentage.to_css(dest),
|
||||
&LengthOrPercentage::Calc(calc) => calc.to_css(dest),
|
||||
}
|
||||
}
|
||||
|
@ -688,7 +696,7 @@ pub mod specified {
|
|||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
Length::parse_dimension(value.value, unit).map(LengthOrPercentage::Length),
|
||||
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||
Ok(LengthOrPercentage::Percentage(value.unit_value)),
|
||||
Ok(LengthOrPercentage::Percentage(Percentage(value.unit_value))),
|
||||
Token::Number(ref value) if value.value == 0. =>
|
||||
Ok(LengthOrPercentage::Length(Length::Absolute(Au(0)))),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
|
@ -712,7 +720,7 @@ pub mod specified {
|
|||
#[derive(Clone, PartialEq, Copy, Debug, HeapSizeOf)]
|
||||
pub enum LengthOrPercentageOrAuto {
|
||||
Length(Length),
|
||||
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
Percentage(Percentage),
|
||||
Auto,
|
||||
Calc(Calc),
|
||||
}
|
||||
|
@ -721,8 +729,7 @@ pub mod specified {
|
|||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
match self {
|
||||
&LengthOrPercentageOrAuto::Length(length) => length.to_css(dest),
|
||||
&LengthOrPercentageOrAuto::Percentage(percentage)
|
||||
=> write!(dest, "{}%", percentage * 100.),
|
||||
&LengthOrPercentageOrAuto::Percentage(percentage) => percentage.to_css(dest),
|
||||
&LengthOrPercentageOrAuto::Auto => dest.write_str("auto"),
|
||||
&LengthOrPercentageOrAuto::Calc(calc) => calc.to_css(dest),
|
||||
}
|
||||
|
@ -737,7 +744,7 @@ pub mod specified {
|
|||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
Length::parse_dimension(value.value, unit).map(LengthOrPercentageOrAuto::Length),
|
||||
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||
Ok(LengthOrPercentageOrAuto::Percentage(value.unit_value)),
|
||||
Ok(LengthOrPercentageOrAuto::Percentage(Percentage(value.unit_value))),
|
||||
Token::Number(ref value) if value.value == 0. =>
|
||||
Ok(LengthOrPercentageOrAuto::Length(Length::Absolute(Au(0)))),
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
|
||||
|
@ -762,7 +769,7 @@ pub mod specified {
|
|||
#[derive(Clone, PartialEq, Copy, Debug, HeapSizeOf)]
|
||||
pub enum LengthOrPercentageOrNone {
|
||||
Length(Length),
|
||||
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
Percentage(Percentage),
|
||||
None,
|
||||
}
|
||||
|
||||
|
@ -770,8 +777,7 @@ pub mod specified {
|
|||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
match self {
|
||||
&LengthOrPercentageOrNone::Length(length) => length.to_css(dest),
|
||||
&LengthOrPercentageOrNone::Percentage(percentage) =>
|
||||
write!(dest, "{}%", percentage * 100.),
|
||||
&LengthOrPercentageOrNone::Percentage(percentage) => percentage.to_css(dest),
|
||||
&LengthOrPercentageOrNone::None => dest.write_str("none"),
|
||||
}
|
||||
}
|
||||
|
@ -784,7 +790,7 @@ pub mod specified {
|
|||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
Length::parse_dimension(value.value, unit).map(LengthOrPercentageOrNone::Length),
|
||||
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||
Ok(LengthOrPercentageOrNone::Percentage(value.unit_value)),
|
||||
Ok(LengthOrPercentageOrNone::Percentage(Percentage(value.unit_value))),
|
||||
Token::Number(ref value) if value.value == 0. =>
|
||||
Ok(LengthOrPercentageOrNone::Length(Length::Absolute(Au(0)))),
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") =>
|
||||
|
@ -846,7 +852,7 @@ pub mod specified {
|
|||
#[derive(Clone, PartialEq, Copy)]
|
||||
pub enum PositionComponent {
|
||||
Length(Length),
|
||||
Percentage(CSSFloat), // [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
Percentage(Percentage),
|
||||
Center,
|
||||
Left,
|
||||
Right,
|
||||
|
@ -861,7 +867,7 @@ pub mod specified {
|
|||
.map(PositionComponent::Length)
|
||||
}
|
||||
Token::Percentage(ref value) => {
|
||||
Ok(PositionComponent::Percentage(value.unit_value))
|
||||
Ok(PositionComponent::Percentage(Percentage(value.unit_value)))
|
||||
}
|
||||
Token::Number(ref value) if value.value == 0. => {
|
||||
Ok(PositionComponent::Length(Length::Absolute(Au(0))))
|
||||
|
@ -884,11 +890,11 @@ pub mod specified {
|
|||
match self {
|
||||
PositionComponent::Length(x) => LengthOrPercentage::Length(x),
|
||||
PositionComponent::Percentage(x) => LengthOrPercentage::Percentage(x),
|
||||
PositionComponent::Center => LengthOrPercentage::Percentage(0.5),
|
||||
PositionComponent::Center => LengthOrPercentage::Percentage(Percentage(0.5)),
|
||||
PositionComponent::Left |
|
||||
PositionComponent::Top => LengthOrPercentage::Percentage(0.0),
|
||||
PositionComponent::Top => LengthOrPercentage::Percentage(Percentage(0.0)),
|
||||
PositionComponent::Right |
|
||||
PositionComponent::Bottom => LengthOrPercentage::Percentage(1.0),
|
||||
PositionComponent::Bottom => LengthOrPercentage::Percentage(Percentage(1.0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1305,7 +1311,7 @@ pub mod computed {
|
|||
}
|
||||
}
|
||||
|
||||
Calc { length: length, percentage: self.percentage }
|
||||
Calc { length: length, percentage: self.percentage.map(|p| p.0) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1342,7 +1348,7 @@ pub mod computed {
|
|||
LengthOrPercentage::Length(value.to_computed_value(context))
|
||||
}
|
||||
specified::LengthOrPercentage::Percentage(value) => {
|
||||
LengthOrPercentage::Percentage(value)
|
||||
LengthOrPercentage::Percentage(value.0)
|
||||
}
|
||||
specified::LengthOrPercentage::Calc(calc) => {
|
||||
LengthOrPercentage::Calc(calc.to_computed_value(context))
|
||||
|
@ -1390,7 +1396,7 @@ pub mod computed {
|
|||
LengthOrPercentageOrAuto::Length(value.to_computed_value(context))
|
||||
}
|
||||
specified::LengthOrPercentageOrAuto::Percentage(value) => {
|
||||
LengthOrPercentageOrAuto::Percentage(value)
|
||||
LengthOrPercentageOrAuto::Percentage(value.0)
|
||||
}
|
||||
specified::LengthOrPercentageOrAuto::Auto => {
|
||||
LengthOrPercentageOrAuto::Auto
|
||||
|
@ -1440,7 +1446,7 @@ pub mod computed {
|
|||
LengthOrPercentageOrNone::Length(value.to_computed_value(context))
|
||||
}
|
||||
specified::LengthOrPercentageOrNone::Percentage(value) => {
|
||||
LengthOrPercentageOrNone::Percentage(value)
|
||||
LengthOrPercentageOrNone::Percentage(value.0)
|
||||
}
|
||||
specified::LengthOrPercentageOrNone::None => {
|
||||
LengthOrPercentageOrNone::None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue