Replace IntegerOrAuto with ZIndex

It's its only use.
This commit is contained in:
Anthony Ramine 2018-02-20 10:13:01 +01:00
parent 6a7ae3b12e
commit b44ffcf498
9 changed files with 75 additions and 50 deletions

View file

@ -68,7 +68,7 @@ pub use self::list::{ListStyleImage, Quotes};
pub use self::list::ListStyleType;
pub use self::outline::OutlineStyle;
pub use self::percentage::Percentage;
pub use self::position::{Position, GridAutoFlow, GridTemplateAreas};
pub use self::position::{GridAutoFlow, GridTemplateAreas, Position, ZIndex};
pub use self::pointing::Cursor;
#[cfg(feature = "gecko")]
pub use self::pointing::CursorImage;
@ -524,20 +524,6 @@ pub type Opacity = CSSFloat;
/// A `<integer>` value.
pub type Integer = CSSInteger;
/// <integer> | auto
pub type IntegerOrAuto = Either<CSSInteger, Auto>;
impl IntegerOrAuto {
/// Returns the integer value if it is an integer, otherwise return
/// the given value.
pub fn integer_or(&self, auto_value: CSSInteger) -> CSSInteger {
match *self {
Either::First(n) => n,
Either::Second(Auto) => auto_value,
}
}
}
/// A wrapper of Integer, but only accept a value >= 1.
pub type PositiveInteger = GreaterThanOrEqualToOne<CSSInteger>;

View file

@ -9,8 +9,9 @@
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};
use values::computed::{LengthOrPercentage, Percentage};
use values::computed::{Integer, LengthOrPercentage, Percentage};
use values::generics::position::Position as GenericPosition;
use values::generics::position::ZIndex as GenericZIndex;
pub use values::specified::position::{GridAutoFlow, GridTemplateAreas};
/// The computed value of a CSS `<position>`
@ -49,3 +50,6 @@ impl ToCss for Position {
self.vertical.to_css(dest)
}
}
/// A computed value for the `z-index` property.
pub type ZIndex = GenericZIndex<Integer>;

View file

@ -24,3 +24,36 @@ impl<H, V> Position<H, V> {
}
}
}
/// A generic value for the `z-index` property.
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)]
#[derive(MallocSizeOf, PartialEq, ToAnimatedZero, ToComputedValue, ToCss)]
pub enum ZIndex<Integer> {
/// An integer value.
Integer(Integer),
/// The keyword `auto`.
Auto,
}
impl<Integer> ZIndex<Integer> {
/// Returns `auto`
#[inline]
pub fn auto() -> Self {
ZIndex::Auto
}
/// Returns whether `self` is `auto`.
#[inline]
pub fn is_auto(self) -> bool {
matches!(self, ZIndex::Auto)
}
/// Returns the integer value if it is an integer, or `auto`.
#[inline]
pub fn integer_or(self, auto: Integer) -> Integer {
match self {
ZIndex::Integer(n) => n,
ZIndex::Auto => auto,
}
}
}

View file

@ -61,7 +61,8 @@ pub use self::list::ListStyleType;
pub use self::outline::OutlineStyle;
pub use self::rect::LengthOrNumberRect;
pub use self::percentage::Percentage;
pub use self::position::{Position, PositionComponent, GridAutoFlow, GridTemplateAreas};
pub use self::position::{GridAutoFlow, GridTemplateAreas, Position};
pub use self::position::{PositionComponent, ZIndex};
pub use self::pointing::Cursor;
#[cfg(feature = "gecko")]
pub use self::pointing::CursorImage;
@ -518,24 +519,6 @@ impl ToCss for Integer {
}
}
/// <integer> | auto
pub type IntegerOrAuto = Either<Integer, Auto>;
impl IntegerOrAuto {
/// Parse `auto` or a positive integer.
pub fn parse_positive<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<IntegerOrAuto, ParseError<'i>> {
match IntegerOrAuto::parse(context, input) {
Ok(Either::First(integer)) if integer.value() <= 0 => {
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
}
result => result,
}
}
}
/// A wrapper of Integer, with value >= 1.
pub type PositiveInteger = GreaterThanOrEqualToOne<Integer>;

View file

@ -20,7 +20,8 @@ use values::{Either, None_};
use values::computed::{CalcLengthOrPercentage, LengthOrPercentage as ComputedLengthOrPercentage};
use values::computed::{Context, Percentage, ToComputedValue};
use values::generics::position::Position as GenericPosition;
use values::specified::{AllowQuirks, LengthOrPercentage};
use values::generics::position::ZIndex as GenericZIndex;
use values::specified::{AllowQuirks, Integer, LengthOrPercentage};
use values::specified::transform::OriginComponent;
/// The specified value of a CSS `<position>`
@ -702,3 +703,18 @@ impl GridTemplateAreas {
Either::Second(None_)
}
}
/// A specified value for the `z-index` property.
pub type ZIndex = GenericZIndex<Integer>;
impl Parse for ZIndex {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("auto")).is_ok() {
return Ok(GenericZIndex::Auto);
}
Ok(GenericZIndex::Integer(Integer::parse(context, input)?))
}
}