mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Change LengthOrPercentage to make use of NoCalcLength
This commit is contained in:
parent
377a23df50
commit
590c9579f0
10 changed files with 61 additions and 60 deletions
|
@ -299,7 +299,6 @@ impl ToComputedValue for InsetRect {
|
|||
/// the keywords are folded into the percentages
|
||||
fn serialize_basicshape_position<W>(position: &Position, dest: &mut W)
|
||||
-> fmt::Result where W: fmt::Write {
|
||||
use values::specified::Length;
|
||||
use values::specified::position::Keyword;
|
||||
|
||||
// keyword-percentage pairs can be folded into a single percentage
|
||||
|
@ -327,7 +326,7 @@ fn serialize_basicshape_position<W>(position: &Position, dest: &mut W)
|
|||
// 0 length should be replaced with 0%
|
||||
fn replace_with_percent(input: LengthOrPercentage) -> LengthOrPercentage {
|
||||
match input {
|
||||
LengthOrPercentage::Length(ref l) if l == &Length::zero() => {
|
||||
LengthOrPercentage::Length(ref l) if l.is_zero() => {
|
||||
LengthOrPercentage::Percentage(Percentage(0.0))
|
||||
}
|
||||
_ => {
|
||||
|
|
|
@ -323,7 +323,13 @@ impl NoCalcLength {
|
|||
NoCalcLength::Absolute(Au(0))
|
||||
}
|
||||
|
||||
/// Get an absolute length from a px values.
|
||||
#[inline]
|
||||
/// Checks whether the length value is zero.
|
||||
pub fn is_zero(&self) -> bool {
|
||||
*self == NoCalcLength::Absolute(Au(0))
|
||||
}
|
||||
|
||||
/// Get an absolute length from a px value.
|
||||
#[inline]
|
||||
pub fn from_px(px_value: CSSFloat) -> NoCalcLength {
|
||||
NoCalcLength::Absolute(Au((px_value * AU_PER_PX) as i32))
|
||||
|
@ -337,7 +343,7 @@ impl NoCalcLength {
|
|||
#[derive(Clone, PartialEq, Debug)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub enum Length {
|
||||
/// The `NoCalcLength` type that cannot parse `calc`
|
||||
/// The internal length type that cannot parse `calc`
|
||||
NoCalc(NoCalcLength),
|
||||
/// A calc expression.
|
||||
///
|
||||
|
@ -428,8 +434,7 @@ impl Length {
|
|||
match try!(input.next()) {
|
||||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
Length::parse_dimension(value.value, unit),
|
||||
Token::Number(ref value) if value.value == 0. =>
|
||||
Ok(Length::zero()),
|
||||
Token::Number(ref value) if value.value == 0. => Ok(Length::zero()),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") =>
|
||||
input.parse_nested_block(|input| {
|
||||
CalcLengthOrPercentage::parse_length(input, context)
|
||||
|
@ -443,7 +448,7 @@ impl Length {
|
|||
Length::parse_internal(input, AllowedNumericType::NonNegative)
|
||||
}
|
||||
|
||||
/// Get an absolute length from a px values.
|
||||
/// Get an absolute length from a px value.
|
||||
#[inline]
|
||||
pub fn from_px(px_value: CSSFloat) -> Length {
|
||||
Length::NoCalc(NoCalcLength::from_px(px_value))
|
||||
|
@ -490,7 +495,7 @@ pub struct CalcProductNode {
|
|||
#[derive(Clone, Debug)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum CalcValueNode {
|
||||
Length(LengthInternal),
|
||||
Length(NoCalcLength),
|
||||
Angle(Angle),
|
||||
Time(Time),
|
||||
Percentage(CSSFloat),
|
||||
|
@ -583,7 +588,7 @@ impl CalcLengthOrPercentage {
|
|||
(Token::Number(ref value), _) => Ok(CalcValueNode::Number(value.value)),
|
||||
(Token::Dimension(ref value, ref unit), CalcUnit::Length) |
|
||||
(Token::Dimension(ref value, ref unit), CalcUnit::LengthOrPercentage) => {
|
||||
LengthInternal::parse_dimension(value.value, unit).map(CalcValueNode::Length)
|
||||
NoCalcLength::parse_dimension(value.value, unit).map(CalcValueNode::Length)
|
||||
}
|
||||
(Token::Dimension(ref value, ref unit), CalcUnit::Angle) => {
|
||||
Angle::parse_dimension(value.value, unit).map(CalcValueNode::Angle)
|
||||
|
@ -899,11 +904,20 @@ impl Parse for Percentage {
|
|||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[allow(missing_docs)]
|
||||
pub enum LengthOrPercentage {
|
||||
Length(Length),
|
||||
Length(NoCalcLength),
|
||||
Percentage(Percentage),
|
||||
Calc(Box<CalcLengthOrPercentage>),
|
||||
}
|
||||
|
||||
impl From<Length> for LengthOrPercentage {
|
||||
fn from(len: Length) -> LengthOrPercentage {
|
||||
match len {
|
||||
Length::NoCalc(l) => LengthOrPercentage::Length(l),
|
||||
Length::Calc(l, _) => LengthOrPercentage::Calc(l),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasViewportPercentage for LengthOrPercentage {
|
||||
fn has_viewport_percentage(&self) -> bool {
|
||||
match *self {
|
||||
|
@ -924,10 +938,9 @@ impl ToCss for LengthOrPercentage {
|
|||
}
|
||||
}
|
||||
impl LengthOrPercentage {
|
||||
#[inline]
|
||||
/// Returns a `zero` length.
|
||||
pub fn zero() -> LengthOrPercentage {
|
||||
LengthOrPercentage::Length(Length::zero())
|
||||
LengthOrPercentage::Length(NoCalcLength::zero())
|
||||
}
|
||||
|
||||
fn parse_internal(input: &mut Parser, context: AllowedNumericType)
|
||||
|
@ -935,11 +948,11 @@ impl LengthOrPercentage {
|
|||
{
|
||||
match try!(input.next()) {
|
||||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
Length::parse_dimension(value.value, unit).map(LengthOrPercentage::Length),
|
||||
NoCalcLength::parse_dimension(value.value, unit).map(LengthOrPercentage::Length),
|
||||
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||
Ok(LengthOrPercentage::Percentage(Percentage(value.unit_value))),
|
||||
Token::Number(ref value) if value.value == 0. =>
|
||||
Ok(LengthOrPercentage::Length(Length::zero())),
|
||||
Ok(LengthOrPercentage::zero()),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
let calc = try!(input.parse_nested_block(CalcLengthOrPercentage::parse_length_or_percentage));
|
||||
Ok(LengthOrPercentage::Calc(Box::new(calc)))
|
||||
|
|
|
@ -198,8 +198,8 @@ impl NoViewportPercentage for BorderRadiusSize {}
|
|||
impl BorderRadiusSize {
|
||||
#[allow(missing_docs)]
|
||||
pub fn zero() -> BorderRadiusSize {
|
||||
let zero = LengthOrPercentage::Length(Length::default());
|
||||
BorderRadiusSize(Size2D::new(zero.clone(), zero))
|
||||
let zero = LengthOrPercentage::Length(NoCalcLength::zero());
|
||||
BorderRadiusSize(Size2D::new(zero.clone(), zero))
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
|
@ -292,11 +292,11 @@ pub fn parse_border_radius(context: &ParserContext, input: &mut Parser) -> Resul
|
|||
input.try(|i| BorderRadiusSize::parse(context, i)).or_else(|_| {
|
||||
match_ignore_ascii_case! { try!(input.expect_ident()),
|
||||
"thin" => Ok(BorderRadiusSize::circle(
|
||||
LengthOrPercentage::Length(Length::from_px(1.)))),
|
||||
LengthOrPercentage::Length(NoCalcLength::from_px(1.)))),
|
||||
"medium" => Ok(BorderRadiusSize::circle(
|
||||
LengthOrPercentage::Length(Length::from_px(3.)))),
|
||||
LengthOrPercentage::Length(NoCalcLength::from_px(3.)))),
|
||||
"thick" => Ok(BorderRadiusSize::circle(
|
||||
LengthOrPercentage::Length(Length::from_px(5.)))),
|
||||
LengthOrPercentage::Length(NoCalcLength::from_px(5.)))),
|
||||
_ => Err(())
|
||||
}
|
||||
})
|
||||
|
@ -606,10 +606,7 @@ impl Shadow {
|
|||
#[allow(missing_docs)]
|
||||
pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result<Shadow, ()> {
|
||||
let length_count = if disable_spread_and_inset { 3 } else { 4 };
|
||||
let mut lengths = [Length::default(),
|
||||
Length::default(),
|
||||
Length::default(),
|
||||
Length::default()];
|
||||
let mut lengths = [Length::zero(), Length::zero(), Length::zero(), Length::zero()];
|
||||
let mut lengths_parsed = false;
|
||||
let mut color = None;
|
||||
let mut inset = false;
|
||||
|
@ -661,7 +658,7 @@ impl Shadow {
|
|||
offset_x: lengths[0].take(),
|
||||
offset_y: lengths[1].take(),
|
||||
blur_radius: lengths[2].take(),
|
||||
spread_radius: if disable_spread_and_inset { Length::default() } else { lengths[3].take() },
|
||||
spread_radius: if disable_spread_and_inset { Length::zero() } else { lengths[3].take() },
|
||||
color: color,
|
||||
inset: inset,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue