Combine LengthOrPercentage and Auto into LengthOrPercentageOrAuto for {Min,Max}Length.

This commit is contained in:
Hiroyuki Ikezoe 2017-05-20 11:58:15 +09:00
parent 323760f47e
commit 95bda2dff9
5 changed files with 54 additions and 80 deletions

View file

@ -338,38 +338,28 @@ impl GeckoStyleCoordConvertible for ExtremumLength {
impl GeckoStyleCoordConvertible for MinLength { impl GeckoStyleCoordConvertible for MinLength {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) { fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self { match *self {
MinLength::LengthOrPercentage(ref lop) => lop.to_gecko_style_coord(coord), MinLength::LengthOrPercentageOrAuto(ref lopoa) => lopoa.to_gecko_style_coord(coord),
MinLength::Auto => coord.set_value(CoordDataValue::Auto),
MinLength::ExtremumLength(ref e) => e.to_gecko_style_coord(coord), MinLength::ExtremumLength(ref e) => e.to_gecko_style_coord(coord),
} }
} }
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> { fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
LengthOrPercentage::from_gecko_style_coord(coord).map(MinLength::LengthOrPercentage) LengthOrPercentageOrAuto::from_gecko_style_coord(coord).map(MinLength::LengthOrPercentageOrAuto)
.or_else(|| ExtremumLength::from_gecko_style_coord(coord).map(MinLength::ExtremumLength)) .or_else(|| ExtremumLength::from_gecko_style_coord(coord).map(MinLength::ExtremumLength))
.or_else(|| match coord.as_value() {
CoordDataValue::Auto => Some(MinLength::Auto),
_ => None,
})
} }
} }
impl GeckoStyleCoordConvertible for MaxLength { impl GeckoStyleCoordConvertible for MaxLength {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) { fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
match *self { match *self {
MaxLength::LengthOrPercentage(ref lop) => lop.to_gecko_style_coord(coord), MaxLength::LengthOrPercentageOrNone(ref lopon) => lopon.to_gecko_style_coord(coord),
MaxLength::None => coord.set_value(CoordDataValue::None),
MaxLength::ExtremumLength(ref e) => e.to_gecko_style_coord(coord), MaxLength::ExtremumLength(ref e) => e.to_gecko_style_coord(coord),
} }
} }
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> { fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
LengthOrPercentage::from_gecko_style_coord(coord).map(MaxLength::LengthOrPercentage) LengthOrPercentageOrNone::from_gecko_style_coord(coord).map(MaxLength::LengthOrPercentageOrNone)
.or_else(|| ExtremumLength::from_gecko_style_coord(coord).map(MaxLength::ExtremumLength)) .or_else(|| ExtremumLength::from_gecko_style_coord(coord).map(MaxLength::ExtremumLength))
.or_else(|| match coord.as_value() {
CoordDataValue::None => Some(MaxLength::None),
_ => None,
})
} }
} }

View file

@ -1241,10 +1241,10 @@ impl Animatable for MinLength {
#[inline] #[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> { fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
match (*self, *other) { match (*self, *other) {
(MinLength::LengthOrPercentage(ref this), (MinLength::LengthOrPercentageOrAuto(ref this),
MinLength::LengthOrPercentage(ref other)) => { MinLength::LengthOrPercentageOrAuto(ref other)) => {
this.add_weighted(other, self_portion, other_portion) this.add_weighted(other, self_portion, other_portion)
.map(MinLength::LengthOrPercentage) .map(MinLength::LengthOrPercentageOrAuto)
} }
_ => Err(()), _ => Err(()),
} }
@ -1253,8 +1253,8 @@ impl Animatable for MinLength {
#[inline] #[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> { fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
match (*self, *other) { match (*self, *other) {
(MinLength::LengthOrPercentage(ref this), (MinLength::LengthOrPercentageOrAuto(ref this),
MinLength::LengthOrPercentage(ref other)) => { MinLength::LengthOrPercentageOrAuto(ref other)) => {
this.compute_distance(other) this.compute_distance(other)
}, },
_ => Err(()), _ => Err(()),
@ -1267,10 +1267,10 @@ impl Animatable for MaxLength {
#[inline] #[inline]
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> { fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result<Self, ()> {
match (*self, *other) { match (*self, *other) {
(MaxLength::LengthOrPercentage(ref this), (MaxLength::LengthOrPercentageOrNone(ref this),
MaxLength::LengthOrPercentage(ref other)) => { MaxLength::LengthOrPercentageOrNone(ref other)) => {
this.add_weighted(other, self_portion, other_portion) this.add_weighted(other, self_portion, other_portion)
.map(MaxLength::LengthOrPercentage) .map(MaxLength::LengthOrPercentageOrNone)
} }
_ => Err(()), _ => Err(()),
} }
@ -1279,8 +1279,8 @@ impl Animatable for MaxLength {
#[inline] #[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> { fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
match (*self, *other) { match (*self, *other) {
(MaxLength::LengthOrPercentage(ref this), (MaxLength::LengthOrPercentageOrNone(ref this),
MaxLength::LengthOrPercentage(ref other)) => { MaxLength::LengthOrPercentageOrNone(ref other)) => {
this.compute_distance(other) this.compute_distance(other)
}, },
_ => Err(()), _ => Err(()),

View file

@ -164,7 +164,7 @@ ${helpers.predefined_type("flex-basis",
% for min_max in ["min", "max"]: % for min_max in ["min", "max"]:
<% <%
MinMax = min_max.title() MinMax = min_max.title()
initial = "None" if "max" == min_max else "Auto" initial = "none()" if "max" == min_max else "auto()"
%> %>
// min-width, min-height, min-block-size, min-inline-size, // min-width, min-height, min-block-size, min-inline-size,

View file

@ -612,22 +612,25 @@ pub type LengthOrNormal = Either<Length, Normal>;
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum MinLength { pub enum MinLength {
LengthOrPercentage(LengthOrPercentage), LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
Auto,
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl MinLength {
/// Returns the `auto` value.
pub fn auto() -> Self {
MinLength::LengthOrPercentageOrAuto(LengthOrPercentageOrAuto::Auto)
}
}
impl ToComputedValue for specified::MinLength { impl ToComputedValue for specified::MinLength {
type ComputedValue = MinLength; type ComputedValue = MinLength;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> MinLength { fn to_computed_value(&self, context: &Context) -> MinLength {
match *self { match *self {
specified::MinLength::LengthOrPercentage(ref lop) => { specified::MinLength::LengthOrPercentageOrAuto(ref lopoa) => {
MinLength::LengthOrPercentage(lop.to_computed_value(context)) MinLength::LengthOrPercentageOrAuto(lopoa.to_computed_value(context))
}
specified::MinLength::Auto => {
MinLength::Auto
} }
specified::MinLength::ExtremumLength(ref ext) => { specified::MinLength::ExtremumLength(ref ext) => {
MinLength::ExtremumLength(ext.clone()) MinLength::ExtremumLength(ext.clone())
@ -638,10 +641,9 @@ impl ToComputedValue for specified::MinLength {
#[inline] #[inline]
fn from_computed_value(computed: &MinLength) -> Self { fn from_computed_value(computed: &MinLength) -> Self {
match *computed { match *computed {
MinLength::Auto => MinLength::LengthOrPercentageOrAuto(ref lopoa) =>
specified::MinLength::Auto, specified::MinLength::LengthOrPercentageOrAuto(
MinLength::LengthOrPercentage(ref lop) => specified::LengthOrPercentageOrAuto::from_computed_value(&lopoa)),
specified::MinLength::LengthOrPercentage(specified::LengthOrPercentage::from_computed_value(&lop)),
MinLength::ExtremumLength(ref ext) => MinLength::ExtremumLength(ref ext) =>
specified::MinLength::ExtremumLength(ext.clone()), specified::MinLength::ExtremumLength(ext.clone()),
} }
@ -651,10 +653,8 @@ impl ToComputedValue for specified::MinLength {
impl ToCss for MinLength { impl ToCss for MinLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
MinLength::LengthOrPercentage(lop) => MinLength::LengthOrPercentageOrAuto(lopoa) =>
lop.to_css(dest), lopoa.to_css(dest),
MinLength::Auto =>
dest.write_str("auto"),
MinLength::ExtremumLength(ext) => MinLength::ExtremumLength(ext) =>
ext.to_css(dest), ext.to_css(dest),
} }
@ -667,22 +667,24 @@ impl ToCss for MinLength {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum MaxLength { pub enum MaxLength {
LengthOrPercentage(LengthOrPercentage), LengthOrPercentageOrNone(LengthOrPercentageOrNone),
None,
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl MaxLength {
/// Returns the `none` value.
pub fn none() -> Self {
MaxLength::LengthOrPercentageOrNone(LengthOrPercentageOrNone::None)
}
}
impl ToComputedValue for specified::MaxLength { impl ToComputedValue for specified::MaxLength {
type ComputedValue = MaxLength; type ComputedValue = MaxLength;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> MaxLength { fn to_computed_value(&self, context: &Context) -> MaxLength {
match *self { match *self {
specified::MaxLength::LengthOrPercentage(ref lop) => { specified::MaxLength::LengthOrPercentageOrNone(ref lopon) => {
MaxLength::LengthOrPercentage(lop.to_computed_value(context)) MaxLength::LengthOrPercentageOrNone(lopon.to_computed_value(context))
}
specified::MaxLength::None => {
MaxLength::None
} }
specified::MaxLength::ExtremumLength(ref ext) => { specified::MaxLength::ExtremumLength(ref ext) => {
MaxLength::ExtremumLength(ext.clone()) MaxLength::ExtremumLength(ext.clone())
@ -693,10 +695,9 @@ impl ToComputedValue for specified::MaxLength {
#[inline] #[inline]
fn from_computed_value(computed: &MaxLength) -> Self { fn from_computed_value(computed: &MaxLength) -> Self {
match *computed { match *computed {
MaxLength::None => MaxLength::LengthOrPercentageOrNone(ref lopon) =>
specified::MaxLength::None, specified::MaxLength::LengthOrPercentageOrNone(
MaxLength::LengthOrPercentage(ref lop) => specified::LengthOrPercentageOrNone::from_computed_value(&lopon)),
specified::MaxLength::LengthOrPercentage(specified::LengthOrPercentage::from_computed_value(&lop)),
MaxLength::ExtremumLength(ref ext) => MaxLength::ExtremumLength(ref ext) =>
specified::MaxLength::ExtremumLength(ext.clone()), specified::MaxLength::ExtremumLength(ext.clone()),
} }
@ -706,10 +707,8 @@ impl ToComputedValue for specified::MaxLength {
impl ToCss for MaxLength { impl ToCss for MaxLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
MaxLength::LengthOrPercentage(lop) => MaxLength::LengthOrPercentageOrNone(lopon) =>
lop.to_css(dest), lopon.to_css(dest),
MaxLength::None =>
dest.write_str("none"),
MaxLength::ExtremumLength(ext) => MaxLength::ExtremumLength(ext) =>
ext.to_css(dest), ext.to_css(dest),
} }

View file

@ -1182,18 +1182,15 @@ impl LengthOrNumber {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum MinLength { pub enum MinLength {
LengthOrPercentage(LengthOrPercentage), LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
Auto,
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl ToCss for MinLength { impl ToCss for MinLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
MinLength::LengthOrPercentage(ref lop) => MinLength::LengthOrPercentageOrAuto(ref lopoa) =>
lop.to_css(dest), lopoa.to_css(dest),
MinLength::Auto =>
dest.write_str("auto"),
MinLength::ExtremumLength(ref ext) => MinLength::ExtremumLength(ref ext) =>
ext.to_css(dest), ext.to_css(dest),
} }
@ -1212,9 +1209,8 @@ impl MinLength {
input: &mut Parser, input: &mut Parser,
allow_quirks: AllowQuirks) -> Result<Self, ()> { allow_quirks: AllowQuirks) -> Result<Self, ()> {
input.try(ExtremumLength::parse).map(MinLength::ExtremumLength) input.try(ExtremumLength::parse).map(MinLength::ExtremumLength)
.or_else(|()| input.try(|i| LengthOrPercentage::parse_non_negative_quirky(context, i, allow_quirks)) .or_else(|()| input.try(|i| LengthOrPercentageOrAuto::parse_non_negative_quirky(context, i, allow_quirks))
.map(MinLength::LengthOrPercentage)) .map(MinLength::LengthOrPercentageOrAuto))
.or_else(|()| input.expect_ident_matching("auto").map(|()| MinLength::Auto))
} }
} }
@ -1223,19 +1219,15 @@ impl MinLength {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum MaxLength { pub enum MaxLength {
LengthOrPercentage(LengthOrPercentage), LengthOrPercentageOrNone(LengthOrPercentageOrNone),
None,
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl ToCss for MaxLength { impl ToCss for MaxLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
MaxLength::LengthOrPercentage(ref lop) => MaxLength::LengthOrPercentageOrNone(ref lopon) =>
lop.to_css(dest), lopon.to_css(dest),
MaxLength::None =>
dest.write_str("none"),
MaxLength::ExtremumLength(ref ext) => MaxLength::ExtremumLength(ref ext) =>
ext.to_css(dest), ext.to_css(dest),
} }
@ -1254,14 +1246,7 @@ impl MaxLength {
input: &mut Parser, input: &mut Parser,
allow_quirks: AllowQuirks) -> Result<Self, ()> { allow_quirks: AllowQuirks) -> Result<Self, ()> {
input.try(ExtremumLength::parse).map(MaxLength::ExtremumLength) input.try(ExtremumLength::parse).map(MaxLength::ExtremumLength)
.or_else(|()| input.try(|i| LengthOrPercentage::parse_non_negative_quirky(context, i, allow_quirks)) .or_else(|()| input.try(|i| LengthOrPercentageOrNone::parse_non_negative_quirky(context, i, allow_quirks))
.map(MaxLength::LengthOrPercentage)) .map(MaxLength::LengthOrPercentageOrNone))
.or_else(|()| {
match_ignore_ascii_case! { &try!(input.expect_ident()),
"none" =>
Ok(MaxLength::None),
_ => Err(())
}
})
} }
} }