mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
style: Simplify computed::LengthOrPercentage and friends.
This is a first step to share LengthOrPercentage representation between Rust and Gecko. We need to preserve whether the value came from a calc() expression, for now at least, since we do different things depending on whether we're calc or not right now. See https://github.com/w3c/csswg-drafts/issues/3482 and dependent bugs for example. That means that the gecko conversion code needs to handle calc() in a bit of an awkward way until I change it to not be needed (patches for that incoming in the next few weeks I hope). I need to add a hack to exclude other things from the PartialEq implementation because the new conversion code is less lossy than the old one, and we relied on the lousiness in AnimationValue comparison (in order to start transitions and such, in [1] for example). I expect to remove that manual PartialEq implementation as soon as I'm done with the conversion. The less lossy conversion does fix a few serialization bugs for animation values though, like not loosing 0% values in calc() when interpolating lengths and percentages, see the two modified tests: * property-types.js * test_animation_properties.html Differential Revision: https://phabricator.services.mozilla.com/D15793
This commit is contained in:
parent
bffe2a699e
commit
ca503b4908
22 changed files with 338 additions and 644 deletions
|
@ -789,6 +789,12 @@ impl LengthOrPercentage {
|
|||
LengthOrPercentage::Length(NoCalcLength::zero())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns a `0%` value.
|
||||
pub fn zero_percent() -> LengthOrPercentage {
|
||||
LengthOrPercentage::Percentage(computed::Percentage::zero())
|
||||
}
|
||||
|
||||
fn parse_internal<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
|
@ -898,24 +904,8 @@ impl IsZeroLength for LengthOrPercentage {
|
|||
#[allow(missing_docs)]
|
||||
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss)]
|
||||
pub enum LengthOrPercentageOrAuto {
|
||||
Length(NoCalcLength),
|
||||
Percentage(computed::Percentage),
|
||||
LengthOrPercentage(LengthOrPercentage),
|
||||
Auto,
|
||||
Calc(Box<CalcLengthOrPercentage>),
|
||||
}
|
||||
|
||||
impl From<NoCalcLength> for LengthOrPercentageOrAuto {
|
||||
#[inline]
|
||||
fn from(len: NoCalcLength) -> Self {
|
||||
LengthOrPercentageOrAuto::Length(len)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<computed::Percentage> for LengthOrPercentageOrAuto {
|
||||
#[inline]
|
||||
fn from(pc: computed::Percentage) -> Self {
|
||||
LengthOrPercentageOrAuto::Percentage(pc)
|
||||
}
|
||||
}
|
||||
|
||||
impl LengthOrPercentageOrAuto {
|
||||
|
@ -925,48 +915,16 @@ impl LengthOrPercentageOrAuto {
|
|||
num_context: AllowedNumericType,
|
||||
allow_quirks: AllowQuirks,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
// FIXME: remove early returns when lifetimes are non-lexical
|
||||
{
|
||||
let location = input.current_source_location();
|
||||
let token = input.next()?;
|
||||
match *token {
|
||||
Token::Dimension {
|
||||
value, ref unit, ..
|
||||
} if num_context.is_ok(context.parsing_mode, value) => {
|
||||
return NoCalcLength::parse_dimension(context, value, unit)
|
||||
.map(LengthOrPercentageOrAuto::Length)
|
||||
.map_err(|()| location.new_unexpected_token_error(token.clone()));
|
||||
},
|
||||
Token::Percentage { unit_value, .. }
|
||||
if num_context.is_ok(context.parsing_mode, unit_value) =>
|
||||
{
|
||||
return Ok(LengthOrPercentageOrAuto::Percentage(computed::Percentage(
|
||||
unit_value,
|
||||
)));
|
||||
}
|
||||
Token::Number { value, .. } if num_context.is_ok(context.parsing_mode, value) => {
|
||||
if value != 0. &&
|
||||
!context.parsing_mode.allows_unitless_lengths() &&
|
||||
!allow_quirks.allowed(context.quirks_mode)
|
||||
{
|
||||
return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
return Ok(LengthOrPercentageOrAuto::Length(NoCalcLength::Absolute(
|
||||
AbsoluteLength::Px(value),
|
||||
)));
|
||||
},
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") => {
|
||||
return Ok(LengthOrPercentageOrAuto::Auto);
|
||||
},
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {},
|
||||
_ => return Err(location.new_unexpected_token_error(token.clone())),
|
||||
}
|
||||
if input.try(|i| i.expect_ident_matching("auto")).is_ok() {
|
||||
return Ok(LengthOrPercentageOrAuto::Auto);
|
||||
}
|
||||
|
||||
let calc = input.parse_nested_block(|i| {
|
||||
CalcNode::parse_length_or_percentage(context, i, num_context)
|
||||
})?;
|
||||
Ok(LengthOrPercentageOrAuto::Calc(Box::new(calc)))
|
||||
Ok(LengthOrPercentageOrAuto::LengthOrPercentage(LengthOrPercentage::parse_internal(
|
||||
context,
|
||||
input,
|
||||
num_context,
|
||||
allow_quirks,
|
||||
)?))
|
||||
}
|
||||
|
||||
/// Parse a non-negative length, percentage, or auto.
|
||||
|
@ -1000,13 +958,13 @@ impl LengthOrPercentageOrAuto {
|
|||
|
||||
/// Returns a value representing a `0` length.
|
||||
pub fn zero() -> Self {
|
||||
LengthOrPercentageOrAuto::Length(NoCalcLength::zero())
|
||||
LengthOrPercentageOrAuto::LengthOrPercentage(LengthOrPercentage::zero())
|
||||
}
|
||||
|
||||
/// Returns a value representing `0%`.
|
||||
#[inline]
|
||||
pub fn zero_percent() -> Self {
|
||||
LengthOrPercentageOrAuto::Percentage(computed::Percentage::zero())
|
||||
LengthOrPercentageOrAuto::LengthOrPercentage(LengthOrPercentage::zero_percent())
|
||||
}
|
||||
|
||||
/// Parses, with quirks.
|
||||
|
@ -1076,9 +1034,7 @@ impl Parse for NonNegativeLengthOrPercentageOrAuto {
|
|||
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum LengthOrPercentageOrNone {
|
||||
Length(NoCalcLength),
|
||||
Percentage(computed::Percentage),
|
||||
Calc(Box<CalcLengthOrPercentage>),
|
||||
LengthOrPercentage(LengthOrPercentage),
|
||||
None,
|
||||
}
|
||||
|
||||
|
@ -1089,48 +1045,16 @@ impl LengthOrPercentageOrNone {
|
|||
num_context: AllowedNumericType,
|
||||
allow_quirks: AllowQuirks,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
// FIXME: remove early returns when lifetimes are non-lexical
|
||||
{
|
||||
let location = input.current_source_location();
|
||||
let token = input.next()?;
|
||||
match *token {
|
||||
Token::Dimension {
|
||||
value, ref unit, ..
|
||||
} if num_context.is_ok(context.parsing_mode, value) => {
|
||||
return NoCalcLength::parse_dimension(context, value, unit)
|
||||
.map(LengthOrPercentageOrNone::Length)
|
||||
.map_err(|()| location.new_unexpected_token_error(token.clone()));
|
||||
},
|
||||
Token::Percentage { unit_value, .. }
|
||||
if num_context.is_ok(context.parsing_mode, unit_value) =>
|
||||
{
|
||||
return Ok(LengthOrPercentageOrNone::Percentage(computed::Percentage(
|
||||
unit_value,
|
||||
)));
|
||||
}
|
||||
Token::Number { value, .. } if num_context.is_ok(context.parsing_mode, value) => {
|
||||
if value != 0. &&
|
||||
!context.parsing_mode.allows_unitless_lengths() &&
|
||||
!allow_quirks.allowed(context.quirks_mode)
|
||||
{
|
||||
return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
return Ok(LengthOrPercentageOrNone::Length(NoCalcLength::Absolute(
|
||||
AbsoluteLength::Px(value),
|
||||
)));
|
||||
},
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {},
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") => {
|
||||
return Ok(LengthOrPercentageOrNone::None);
|
||||
},
|
||||
_ => return Err(location.new_unexpected_token_error(token.clone())),
|
||||
}
|
||||
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
|
||||
return Ok(LengthOrPercentageOrNone::None);
|
||||
}
|
||||
|
||||
let calc = input.parse_nested_block(|i| {
|
||||
CalcNode::parse_length_or_percentage(context, i, num_context)
|
||||
})?;
|
||||
Ok(LengthOrPercentageOrNone::Calc(Box::new(calc)))
|
||||
Ok(LengthOrPercentageOrNone::LengthOrPercentage(LengthOrPercentage::parse_internal(
|
||||
context,
|
||||
input,
|
||||
num_context,
|
||||
allow_quirks,
|
||||
)?))
|
||||
}
|
||||
|
||||
/// Parse a non-negative LengthOrPercentageOrNone.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue