Replace Au with CSSPixelLength in CalcLengthOrPercentage.

We replace Au with CSSPixelLength for the length part of
computed::CalcLengthOrPercentage. Therefore, it would be easier to use
CSSPixelLength for all other LengthOrPercentage{*} types.
This commit is contained in:
Boris Chiou 2017-09-08 17:43:41 +08:00
parent a949e2a057
commit 535c1e3c6f
13 changed files with 99 additions and 117 deletions

View file

@ -1520,7 +1520,7 @@ impl Fragment {
LengthOrPercentageOrAuto::Calc(calc) => { LengthOrPercentageOrAuto::Calc(calc) => {
// TODO(nox): This is probably wrong, because it accounts neither for // TODO(nox): This is probably wrong, because it accounts neither for
// clamping (not sure if necessary here) nor percentage. // clamping (not sure if necessary here) nor percentage.
calc.unclamped_length() Au::from(calc.unclamped_length())
}, },
}; };

View file

@ -29,7 +29,7 @@ impl From<CalcLengthOrPercentage> for nsStyleCoord_CalcValue {
fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue { fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue {
let has_percentage = other.percentage.is_some(); let has_percentage = other.percentage.is_some();
nsStyleCoord_CalcValue { nsStyleCoord_CalcValue {
mLength: other.unclamped_length().0, mLength: other.unclamped_length().to_i32_au(),
mPercent: other.percentage.map_or(0., |p| p.0), mPercent: other.percentage.map_or(0., |p| p.0),
mHasPercent: has_percentage, mHasPercent: has_percentage,
} }
@ -43,7 +43,7 @@ impl From<nsStyleCoord_CalcValue> for CalcLengthOrPercentage {
} else { } else {
None None
}; };
Self::new(Au(other.mLength), percentage) Self::new(Au(other.mLength).into(), percentage)
} }
} }

View file

@ -2316,7 +2316,7 @@ impl ComputeSquaredDistance for TransformOperation {
match *lop { match *lop {
LengthOrPercentage::Length(au) => au.to_f64_px(), LengthOrPercentage::Length(au) => au.to_f64_px(),
LengthOrPercentage::Percentage(_) => 0., LengthOrPercentage::Percentage(_) => 0.,
LengthOrPercentage::Calc(calc) => calc.length().to_f64_px(), LengthOrPercentage::Calc(calc) => calc.length().px() as f64,
} }
}; };

View file

@ -7,9 +7,9 @@
use app_units::{Au, AU_PER_PX}; use app_units::{Au, AU_PER_PX};
use ordered_float::NotNaN; use ordered_float::NotNaN;
use std::fmt; use std::fmt;
use std::ops::Add; use std::ops::{Add, Neg};
use style_traits::ToCss; use style_traits::ToCss;
use style_traits::values::specified::AllowedLengthType; use style_traits::values::specified::AllowedNumericType;
use super::{Number, ToComputedValue, Context, Percentage}; use super::{Number, ToComputedValue, Context, Percentage};
use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified}; use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified};
use values::animated::{Animate, Procedure, ToAnimatedZero}; use values::animated::{Animate, Procedure, ToAnimatedZero};
@ -55,7 +55,7 @@ impl ToComputedValue for specified::Length {
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self { match *self {
specified::Length::NoCalc(l) => l.to_computed_value(context), specified::Length::NoCalc(l) => l.to_computed_value(context),
specified::Length::Calc(ref calc) => calc.to_computed_value(context).length().into(), specified::Length::Calc(ref calc) => calc.to_computed_value(context).length(),
} }
} }
@ -70,8 +70,8 @@ impl ToComputedValue for specified::Length {
#[derive(Clone, Copy, Debug, PartialEq, ToAnimatedZero)] #[derive(Clone, Copy, Debug, PartialEq, ToAnimatedZero)]
pub struct CalcLengthOrPercentage { pub struct CalcLengthOrPercentage {
#[animation(constant)] #[animation(constant)]
pub clamping_mode: AllowedLengthType, pub clamping_mode: AllowedNumericType,
length: Au, length: Length,
pub percentage: Option<Percentage>, pub percentage: Option<Percentage>,
} }
@ -81,8 +81,7 @@ impl ComputeSquaredDistance for CalcLengthOrPercentage {
// FIXME(nox): This looks incorrect to me, to add a distance between lengths // FIXME(nox): This looks incorrect to me, to add a distance between lengths
// with a distance between percentages. // with a distance between percentages.
Ok( Ok(
self.unclamped_length().to_f64_px().compute_squared_distance( self.unclamped_length().compute_squared_distance(&other.unclamped_length())? +
&other.unclamped_length().to_f64_px())? +
self.percentage().compute_squared_distance(&other.percentage())?, self.percentage().compute_squared_distance(&other.percentage())?,
) )
} }
@ -91,15 +90,15 @@ impl ComputeSquaredDistance for CalcLengthOrPercentage {
impl CalcLengthOrPercentage { impl CalcLengthOrPercentage {
/// Returns a new `CalcLengthOrPercentage`. /// Returns a new `CalcLengthOrPercentage`.
#[inline] #[inline]
pub fn new(length: Au, percentage: Option<Percentage>) -> Self { pub fn new(length: Length, percentage: Option<Percentage>) -> Self {
Self::with_clamping_mode(length, percentage, AllowedLengthType::All) Self::with_clamping_mode(length, percentage, AllowedNumericType::All)
} }
/// Returns a new `CalcLengthOrPercentage` with a specific clamping mode. /// Returns a new `CalcLengthOrPercentage` with a specific clamping mode.
#[inline] #[inline]
pub fn with_clamping_mode(length: Au, pub fn with_clamping_mode(length: Length,
percentage: Option<Percentage>, percentage: Option<Percentage>,
clamping_mode: AllowedLengthType) clamping_mode: AllowedNumericType)
-> Self { -> Self {
Self { Self {
clamping_mode: clamping_mode, clamping_mode: clamping_mode,
@ -112,20 +111,20 @@ impl CalcLengthOrPercentage {
/// ///
/// Panics in debug mode if a percentage is present in the expression. /// Panics in debug mode if a percentage is present in the expression.
#[inline] #[inline]
pub fn length(&self) -> Au { pub fn length(&self) -> CSSPixelLength {
debug_assert!(self.percentage.is_none()); debug_assert!(self.percentage.is_none());
self.length_component() self.length_component()
} }
/// Returns the length component of this `calc()` /// Returns the length component of this `calc()`
#[inline] #[inline]
pub fn length_component(&self) -> Au { pub fn length_component(&self) -> CSSPixelLength {
self.clamping_mode.clamp(self.length) CSSPixelLength::new(self.clamping_mode.clamp(self.length.px()))
} }
/// Returns the `<length>` component of this `calc()`, unclamped. /// Returns the `<length>` component of this `calc()`, unclamped.
#[inline] #[inline]
pub fn unclamped_length(&self) -> Au { pub fn unclamped_length(&self) -> CSSPixelLength {
self.length self.length
} }
@ -140,9 +139,10 @@ impl CalcLengthOrPercentage {
pub fn to_used_value(&self, container_len: Option<Au>) -> Option<Au> { pub fn to_used_value(&self, container_len: Option<Au>) -> Option<Au> {
match (container_len, self.percentage) { match (container_len, self.percentage) {
(Some(len), Some(percent)) => { (Some(len), Some(percent)) => {
Some(self.clamping_mode.clamp(self.length + len.scale_by(percent.0))) let pixel = self.length.px() + len.scale_by(percent.0).to_f32_px();
Some(Au::from_f32_px(self.clamping_mode.clamp(pixel)))
}, },
(_, None) => Some(self.length()), (_, None) => Some(Au::from(self.length())),
_ => None, _ => None,
} }
} }
@ -152,10 +152,10 @@ impl From<LengthOrPercentage> for CalcLengthOrPercentage {
fn from(len: LengthOrPercentage) -> CalcLengthOrPercentage { fn from(len: LengthOrPercentage) -> CalcLengthOrPercentage {
match len { match len {
LengthOrPercentage::Percentage(this) => { LengthOrPercentage::Percentage(this) => {
CalcLengthOrPercentage::new(Au(0), Some(this)) CalcLengthOrPercentage::new(Length::new(0.), Some(this))
} }
LengthOrPercentage::Length(this) => { LengthOrPercentage::Length(this) => {
CalcLengthOrPercentage::new(this, None) CalcLengthOrPercentage::new(this.into(), None)
} }
LengthOrPercentage::Calc(this) => { LengthOrPercentage::Calc(this) => {
this this
@ -168,10 +168,10 @@ impl From<LengthOrPercentageOrAuto> for Option<CalcLengthOrPercentage> {
fn from(len: LengthOrPercentageOrAuto) -> Option<CalcLengthOrPercentage> { fn from(len: LengthOrPercentageOrAuto) -> Option<CalcLengthOrPercentage> {
match len { match len {
LengthOrPercentageOrAuto::Percentage(this) => { LengthOrPercentageOrAuto::Percentage(this) => {
Some(CalcLengthOrPercentage::new(Au(0), Some(this))) Some(CalcLengthOrPercentage::new(Length::new(0.), Some(this)))
} }
LengthOrPercentageOrAuto::Length(this) => { LengthOrPercentageOrAuto::Length(this) => {
Some(CalcLengthOrPercentage::new(this, None)) Some(CalcLengthOrPercentage::new(this.into(), None))
} }
LengthOrPercentageOrAuto::Calc(this) => { LengthOrPercentageOrAuto::Calc(this) => {
Some(this) Some(this)
@ -187,10 +187,10 @@ impl From<LengthOrPercentageOrNone> for Option<CalcLengthOrPercentage> {
fn from(len: LengthOrPercentageOrNone) -> Option<CalcLengthOrPercentage> { fn from(len: LengthOrPercentageOrNone) -> Option<CalcLengthOrPercentage> {
match len { match len {
LengthOrPercentageOrNone::Percentage(this) => { LengthOrPercentageOrNone::Percentage(this) => {
Some(CalcLengthOrPercentage::new(Au(0), Some(this))) Some(CalcLengthOrPercentage::new(Length::new(0.), Some(this)))
} }
LengthOrPercentageOrNone::Length(this) => { LengthOrPercentageOrNone::Length(this) => {
Some(CalcLengthOrPercentage::new(this, None)) Some(CalcLengthOrPercentage::new(this.into(), None))
} }
LengthOrPercentageOrNone::Calc(this) => { LengthOrPercentageOrNone::Calc(this) => {
Some(this) Some(this)
@ -208,14 +208,14 @@ impl ToCss for CalcLengthOrPercentage {
let (length, percentage) = match (self.length, self.percentage) { let (length, percentage) = match (self.length, self.percentage) {
(l, None) => return l.to_css(dest), (l, None) => return l.to_css(dest),
(l, Some(p)) if l == Au(0) => return p.to_css(dest), (l, Some(p)) if l.px() == 0. => return p.to_css(dest),
(l, Some(p)) => (l, p), (l, Some(p)) => (l, p),
}; };
dest.write_str("calc(")?; dest.write_str("calc(")?;
percentage.to_css(dest)?; percentage.to_css(dest)?;
dest.write_str(if length < Zero::zero() { " - " } else { " + " })?; dest.write_str(if length.px() < Zero::zero() { " - " } else { " + " })?;
length.abs().to_css(dest)?; length.abs().to_css(dest)?;
dest.write_str(")") dest.write_str(")")
@ -226,11 +226,12 @@ impl specified::CalcLengthOrPercentage {
/// Compute the value, zooming any absolute units by the zoom function. /// Compute the value, zooming any absolute units by the zoom function.
fn to_computed_value_with_zoom<F>(&self, context: &Context, zoom_fn: F, fn to_computed_value_with_zoom<F>(&self, context: &Context, zoom_fn: F,
base_size: FontBaseSize) -> CalcLengthOrPercentage base_size: FontBaseSize) -> CalcLengthOrPercentage
where F: Fn(Au) -> Au { where F: Fn(Length) -> Length {
let mut length = Au(0); use std::f32;
let mut length = 0.;
if let Some(absolute) = self.absolute { if let Some(absolute) = self.absolute {
length += zoom_fn(Au::from(absolute.to_computed_value(context))); length += zoom_fn(absolute.to_computed_value(context)).px();
} }
for val in &[self.vw.map(ViewportPercentageLength::Vw), for val in &[self.vw.map(ViewportPercentageLength::Vw),
@ -239,7 +240,7 @@ impl specified::CalcLengthOrPercentage {
self.vmax.map(ViewportPercentageLength::Vmax)] { self.vmax.map(ViewportPercentageLength::Vmax)] {
if let Some(val) = *val { if let Some(val) = *val {
let viewport_size = context.viewport_size_for_viewport_unit_resolution(); let viewport_size = context.viewport_size_for_viewport_unit_resolution();
length += Au::from(val.to_computed_value(viewport_size)); length += val.to_computed_value(viewport_size).px();
} }
} }
@ -248,20 +249,20 @@ impl specified::CalcLengthOrPercentage {
self.ex.map(FontRelativeLength::Ex), self.ex.map(FontRelativeLength::Ex),
self.rem.map(FontRelativeLength::Rem)] { self.rem.map(FontRelativeLength::Rem)] {
if let Some(val) = *val { if let Some(val) = *val {
length += Au::from(val.to_computed_value(context, base_size)); length += val.to_computed_value(context, base_size).px();
} }
} }
CalcLengthOrPercentage { CalcLengthOrPercentage {
clamping_mode: self.clamping_mode, clamping_mode: self.clamping_mode,
length: length, length: Length::new(length.min(f32::MAX).max(f32::MIN)),
percentage: self.percentage, percentage: self.percentage,
} }
} }
/// Compute font-size or line-height taking into account text-zoom if necessary. /// Compute font-size or line-height taking into account text-zoom if necessary.
pub fn to_computed_value_zoomed(&self, context: &Context, base_size: FontBaseSize) -> CalcLengthOrPercentage { pub fn to_computed_value_zoomed(&self, context: &Context, base_size: FontBaseSize) -> CalcLengthOrPercentage {
self.to_computed_value_with_zoom(context, |abs| Au::from(context.maybe_zoom_text(abs.into())), base_size) self.to_computed_value_with_zoom(context, |abs| context.maybe_zoom_text(abs), base_size)
} }
} }
@ -277,7 +278,7 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self { fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
specified::CalcLengthOrPercentage { specified::CalcLengthOrPercentage {
clamping_mode: computed.clamping_mode, clamping_mode: computed.clamping_mode,
absolute: Some(AbsoluteLength::from_computed_value(&computed.length.into())), absolute: Some(AbsoluteLength::from_computed_value(&computed.length)),
percentage: computed.percentage, percentage: computed.percentage,
..Default::default() ..Default::default()
} }
@ -368,7 +369,7 @@ impl LengthOrPercentage {
match *self { match *self {
Length(l) => (l, NotNaN::new(0.0).unwrap()), Length(l) => (l, NotNaN::new(0.0).unwrap()),
Percentage(p) => (Au(0), NotNaN::new(p.0).unwrap()), Percentage(p) => (Au(0), NotNaN::new(p.0).unwrap()),
Calc(c) => (c.unclamped_length(), NotNaN::new(c.percentage()).unwrap()), Calc(c) => (Au::from(c.unclamped_length()), NotNaN::new(c.percentage()).unwrap()),
} }
} }
@ -690,6 +691,11 @@ impl CSSPixelLength {
pub fn to_i32_au(&self) -> i32 { pub fn to_i32_au(&self) -> i32 {
Au::from(*self).0 Au::from(*self).0
} }
/// Return the absolute value of this length.
pub fn abs(self) -> Self {
CSSPixelLength::new(self.0.abs())
}
} }
impl ToCss for CSSPixelLength { impl ToCss for CSSPixelLength {
@ -700,6 +706,15 @@ impl ToCss for CSSPixelLength {
} }
} }
impl Neg for CSSPixelLength {
type Output = Self;
#[inline]
fn neg(self) -> Self {
CSSPixelLength::new(-self.0)
}
}
impl From<CSSPixelLength> for Au { impl From<CSSPixelLength> for Au {
#[inline] #[inline]
fn from(len: CSSPixelLength) -> Self { fn from(len: CSSPixelLength) -> Self {

View file

@ -74,7 +74,7 @@ impl TransformList {
match *lop { match *lop {
LengthOrPercentage::Length(au) => au.to_f32_px(), LengthOrPercentage::Length(au) => au.to_f32_px(),
LengthOrPercentage::Percentage(_) => 0., LengthOrPercentage::Percentage(_) => 0.,
LengthOrPercentage::Calc(calc) => calc.length().to_f32_px(), LengthOrPercentage::Calc(calc) => calc.length().px(),
} }
}; };

View file

@ -11,7 +11,7 @@ use parser::ParserContext;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::fmt; use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError}; use style_traits::{ToCss, ParseError, StyleParseError};
use style_traits::values::specified::AllowedLengthType; use style_traits::values::specified::AllowedNumericType;
use values::{CSSInteger, CSSFloat}; use values::{CSSInteger, CSSFloat};
use values::computed; use values::computed;
use values::specified::{Angle, Time}; use values::specified::{Angle, Time};
@ -67,7 +67,7 @@ pub enum CalcUnit {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub struct CalcLengthOrPercentage { pub struct CalcLengthOrPercentage {
pub clamping_mode: AllowedLengthType, pub clamping_mode: AllowedNumericType,
pub absolute: Option<AbsoluteLength>, pub absolute: Option<AbsoluteLength>,
pub vw: Option<CSSFloat>, pub vw: Option<CSSFloat>,
pub vh: Option<CSSFloat>, pub vh: Option<CSSFloat>,
@ -291,7 +291,7 @@ impl CalcNode {
/// Tries to simplify this expression into a `<length>` or `<percentage`> /// Tries to simplify this expression into a `<length>` or `<percentage`>
/// value. /// value.
fn to_length_or_percentage(&self, clamping_mode: AllowedLengthType) fn to_length_or_percentage(&self, clamping_mode: AllowedNumericType)
-> Result<CalcLengthOrPercentage, ()> { -> Result<CalcLengthOrPercentage, ()> {
let mut ret = CalcLengthOrPercentage { let mut ret = CalcLengthOrPercentage {
clamping_mode: clamping_mode, clamping_mode: clamping_mode,
@ -565,7 +565,7 @@ impl CalcNode {
pub fn parse_length_or_percentage<'i, 't>( pub fn parse_length_or_percentage<'i, 't>(
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
clamping_mode: AllowedLengthType clamping_mode: AllowedNumericType
) -> Result<CalcLengthOrPercentage, ParseError<'i>> { ) -> Result<CalcLengthOrPercentage, ParseError<'i>> {
Self::parse(context, input, CalcUnit::LengthOrPercentage)? Self::parse(context, input, CalcUnit::LengthOrPercentage)?
.to_length_or_percentage(clamping_mode) .to_length_or_percentage(clamping_mode)
@ -586,7 +586,7 @@ impl CalcNode {
pub fn parse_length<'i, 't>( pub fn parse_length<'i, 't>(
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
clamping_mode: AllowedLengthType clamping_mode: AllowedNumericType
) -> Result<CalcLengthOrPercentage, ParseError<'i>> { ) -> Result<CalcLengthOrPercentage, ParseError<'i>> {
Self::parse(context, input, CalcUnit::Length)? Self::parse(context, input, CalcUnit::Length)?
.to_length_or_percentage(clamping_mode) .to_length_or_percentage(clamping_mode)

View file

@ -15,7 +15,7 @@ use std::{cmp, fmt, mem};
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::ops::{Add, Mul}; use std::ops::{Add, Mul};
use style_traits::{ToCss, ParseError, StyleParseError}; use style_traits::{ToCss, ParseError, StyleParseError};
use style_traits::values::specified::AllowedLengthType; use style_traits::values::specified::AllowedNumericType;
use stylesheets::CssRuleType; use stylesheets::CssRuleType;
use super::{AllowQuirks, Number, ToComputedValue, Percentage}; use super::{AllowQuirks, Number, ToComputedValue, Percentage};
use values::{Auto, CSSFloat, Either, FONT_MEDIUM_PX, None_, Normal}; use values::{Auto, CSSFloat, Either, FONT_MEDIUM_PX, None_, Normal};
@ -642,7 +642,7 @@ impl Length {
#[inline] #[inline]
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
num_context: AllowedLengthType, num_context: AllowedNumericType,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Length, ParseError<'i>> { -> Result<Length, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
@ -683,7 +683,7 @@ impl Length {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Length, ParseError<'i>> { -> Result<Length, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::NonNegative, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::NonNegative, allow_quirks)
} }
/// Get an absolute length from a px value. /// Get an absolute length from a px value.
@ -713,7 +713,7 @@ impl Length {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::All, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::All, allow_quirks)
} }
} }
@ -725,7 +725,7 @@ impl<T: Parse> Either<Length, T> {
if let Ok(v) = input.try(|input| T::parse(context, input)) { if let Ok(v) = input.try(|input| T::parse(context, input)) {
return Ok(Either::Second(v)); return Ok(Either::Second(v));
} }
Length::parse_internal(context, input, AllowedLengthType::NonNegative, AllowQuirks::No).map(Either::First) Length::parse_internal(context, input, AllowedNumericType::NonNegative, AllowQuirks::No).map(Either::First)
} }
} }
@ -752,7 +752,7 @@ impl<T: Parse> Parse for Either<NonNegativeLength, T> {
if let Ok(v) = input.try(|input| T::parse(context, input)) { if let Ok(v) = input.try(|input| T::parse(context, input)) {
return Ok(Either::Second(v)); return Ok(Either::Second(v));
} }
Length::parse_internal(context, input, AllowedLengthType::NonNegative, AllowQuirks::No) Length::parse_internal(context, input, AllowedNumericType::NonNegative, AllowQuirks::No)
.map(NonNegative::<Length>).map(Either::First) .map(NonNegative::<Length>).map(Either::First)
} }
} }
@ -836,7 +836,7 @@ impl LengthOrPercentage {
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
num_context: AllowedLengthType, num_context: AllowedNumericType,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<LengthOrPercentage, ParseError<'i>> -> Result<LengthOrPercentage, ParseError<'i>>
{ {
@ -885,7 +885,7 @@ impl LengthOrPercentage {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<LengthOrPercentage, ParseError<'i>> { -> Result<LengthOrPercentage, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::NonNegative, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::NonNegative, allow_quirks)
} }
/// Parse a length, treating dimensionless numbers as pixels /// Parse a length, treating dimensionless numbers as pixels
@ -946,7 +946,7 @@ impl LengthOrPercentage {
pub fn parse_quirky<'i, 't>(context: &ParserContext, pub fn parse_quirky<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) -> Result<Self, ParseError<'i>> { allow_quirks: AllowQuirks) -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::All, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::All, allow_quirks)
} }
} }
@ -978,7 +978,7 @@ impl From<computed::Percentage> for LengthOrPercentageOrAuto {
impl LengthOrPercentageOrAuto { impl LengthOrPercentageOrAuto {
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
num_context: AllowedLengthType, num_context: AllowedNumericType,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
@ -1030,7 +1030,7 @@ impl LengthOrPercentageOrAuto {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::NonNegative, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::NonNegative, allow_quirks)
} }
/// Returns the `auto` value. /// Returns the `auto` value.
@ -1063,7 +1063,7 @@ impl LengthOrPercentageOrAuto {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::All, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::All, allow_quirks)
} }
} }
@ -1081,7 +1081,7 @@ pub enum LengthOrPercentageOrNone {
impl LengthOrPercentageOrNone { impl LengthOrPercentageOrNone {
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
num_context: AllowedLengthType, num_context: AllowedNumericType,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<LengthOrPercentageOrNone, ParseError<'i>> -> Result<LengthOrPercentageOrNone, ParseError<'i>>
{ {
@ -1133,14 +1133,14 @@ impl LengthOrPercentageOrNone {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::NonNegative, allow_quirks) Self::parse_internal(context, input, AllowedNumericType::NonNegative, allow_quirks)
} }
} }
impl Parse for LengthOrPercentageOrNone { impl Parse for LengthOrPercentageOrNone {
#[inline] #[inline]
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Self::parse_internal(context, input, AllowedLengthType::All, AllowQuirks::No) Self::parse_internal(context, input, AllowedNumericType::All, AllowQuirks::No)
} }
} }

View file

@ -203,14 +203,15 @@ impl<S: Side> ToComputedValue for PositionComponent<S> {
match length.to_computed_value(context) { match length.to_computed_value(context) {
ComputedLengthOrPercentage::Length(length) => { ComputedLengthOrPercentage::Length(length) => {
ComputedLengthOrPercentage::Calc( ComputedLengthOrPercentage::Calc(
CalcLengthOrPercentage::new(-length, Some(Percentage::hundred()))) CalcLengthOrPercentage::new((-length).into(), Some(Percentage::hundred())))
}, },
ComputedLengthOrPercentage::Percentage(p) => { ComputedLengthOrPercentage::Percentage(p) => {
ComputedLengthOrPercentage::Percentage(Percentage(1.0 - p.0)) ComputedLengthOrPercentage::Percentage(Percentage(1.0 - p.0))
}, },
ComputedLengthOrPercentage::Calc(calc) => { ComputedLengthOrPercentage::Calc(calc) => {
let p = Percentage(1. - calc.percentage.map_or(0., |p| p.0)); let p = Percentage(1. - calc.percentage.map_or(0., |p| p.0));
ComputedLengthOrPercentage::Calc(CalcLengthOrPercentage::new(-calc.unclamped_length(), Some(p))) let l = -calc.unclamped_length();
ComputedLengthOrPercentage::Calc(CalcLengthOrPercentage::new(l, Some(p)))
}, },
} }
}, },

View file

@ -84,7 +84,7 @@ impl ToComputedValue for LineHeight {
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
use app_units::Au; use values::computed::NonNegativeLength;
use values::specified::length::FontBaseSize; use values::specified::length::FontBaseSize;
match *self { match *self {
GenericLineHeight::Normal => { GenericLineHeight::Normal => {
@ -119,13 +119,13 @@ impl ToComputedValue for LineHeight {
.to_computed_value( .to_computed_value(
context, context,
FontBaseSize::CurrentStyle, FontBaseSize::CurrentStyle,
); ).px();
let absolute_length = computed_calc.unclamped_length(); let absolute_length = computed_calc.unclamped_length().px();
computed_calc let pixel = computed_calc
.clamping_mode .clamping_mode
.clamp(absolute_length + Au::from(font_relative_length)) .clamp(absolute_length + font_relative_length);
.into() NonNegativeLength::new(pixel)
} }
}; };
GenericLineHeight::Length(result) GenericLineHeight::Length(result)

View file

@ -469,49 +469,6 @@ macro_rules! __define_css_keyword_enum__actual {
/// Helper types for the handling of specified values. /// Helper types for the handling of specified values.
pub mod specified { pub mod specified {
use ParsingMode; use ParsingMode;
use app_units::Au;
use std::cmp;
/// Whether to allow negative lengths or not.
#[repr(u8)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AllowedLengthType {
/// Allow all kind of lengths.
All,
/// Allow only non-negative lengths.
NonNegative
}
impl Default for AllowedLengthType {
#[inline]
fn default() -> Self {
AllowedLengthType::All
}
}
impl AllowedLengthType {
/// Whether value is valid for this allowed length type.
#[inline]
pub fn is_ok(&self, parsing_mode: ParsingMode, value: f32) -> bool {
if parsing_mode.allows_all_numeric_values() {
return true;
}
match *self {
AllowedLengthType::All => true,
AllowedLengthType::NonNegative => value >= 0.,
}
}
/// Clamp the value following the rules of this numeric type.
#[inline]
pub fn clamp(&self, val: Au) -> Au {
match *self {
AllowedLengthType::All => val,
AllowedLengthType::NonNegative => cmp::max(Au(0), val),
}
}
}
/// Whether to allow negative lengths or not. /// Whether to allow negative lengths or not.
#[repr(u8)] #[repr(u8)]
@ -526,6 +483,13 @@ pub mod specified {
AtLeastOne, AtLeastOne,
} }
impl Default for AllowedNumericType {
#[inline]
fn default() -> Self {
AllowedNumericType::All
}
}
impl AllowedNumericType { impl AllowedNumericType {
/// Whether the value fits the rules of this numeric type. /// Whether the value fits the rules of this numeric type.
#[inline] #[inline]

View file

@ -107,7 +107,7 @@ impl Zoom {
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Zoom, ParseError<'i>> { pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Zoom, ParseError<'i>> {
use PARSING_MODE_DEFAULT; use PARSING_MODE_DEFAULT;
use cssparser::Token; use cssparser::Token;
use values::specified::AllowedLengthType::NonNegative; use values::specified::AllowedNumericType::NonNegative;
match *input.next()? { match *input.next()? {
// TODO: This parse() method should take ParserContext as an // TODO: This parse() method should take ParserContext as an

View file

@ -90,16 +90,18 @@ fn test_transform_interpolation_on_translate() {
Length::new(25.), Length::new(25.),
)])); )]));
let to = TransformList(Some(vec![ let to = TransformList(Some(vec![
TransformOperation::Translate(LengthOrPercentage::Length(Au(100)), TransformOperation::Translate(LengthOrPercentage::Length(Au::from_px(100)),
LengthOrPercentage::Length(Au(50)), LengthOrPercentage::Length(Au::from_px(50)),
Length::new(75.))])); Length::new(75.))]));
assert_eq!( assert_eq!(
from.animate(&to, Procedure::Interpolate { progress: 0.5 }).unwrap(), from.animate(&to, Procedure::Interpolate { progress: 0.5 }).unwrap(),
TransformList(Some(vec![TransformOperation::Translate( TransformList(Some(vec![TransformOperation::Translate(
// calc(50px + 25%) // calc(50px + 25%)
LengthOrPercentage::Calc(CalcLengthOrPercentage::new(Au(50), Some(Percentage(0.25)))), LengthOrPercentage::Calc(CalcLengthOrPercentage::new(Length::new(50.),
Some(Percentage(0.25)))),
// calc(25px + 50%) // calc(25px + 50%)
LengthOrPercentage::Calc(CalcLengthOrPercentage::new(Au(25), Some(Percentage(0.5)))), LengthOrPercentage::Calc(CalcLengthOrPercentage::new(Length::new(25.),
Some(Percentage(0.5)))),
Length::new(50.), Length::new(50.),
)])) )]))
); );

View file

@ -8,12 +8,12 @@ use style::values::computed::{CalcLengthOrPercentage, Percentage};
#[test] #[test]
fn test_length_calc() { fn test_length_calc() {
let calc = CalcLengthOrPercentage::new(Au(10), Some(Percentage(0.2))); let calc = CalcLengthOrPercentage::new(Au(10).into(), Some(Percentage(0.2)));
assert_eq!(calc.to_used_value(Some(Au(10))), Some(Au(12))); assert_eq!(calc.to_used_value(Some(Au(10))), Some(Au(12)));
assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10))); assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
assert_eq!(calc.to_used_value(None), None); assert_eq!(calc.to_used_value(None), None);
let calc = CalcLengthOrPercentage::new(Au(10), None); let calc = CalcLengthOrPercentage::new(Au(10).into(), None);
assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10))); assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
assert_eq!(calc.to_used_value(None), Some(Au(10))); assert_eq!(calc.to_used_value(None), Some(Au(10)));
} }