From b44ffcf4989fe7f4b1a878aec8f44c126712462a Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 20 Feb 2018 10:13:01 +0100 Subject: [PATCH] Replace IntegerOrAuto with ZIndex It's its only use. --- components/layout/fragment.rs | 4 +-- components/style/properties/data.py | 2 +- components/style/properties/gecko.mako.rs | 11 ++++--- .../properties/longhand/position.mako.rs | 14 ++++---- components/style/values/computed/mod.rs | 16 +-------- components/style/values/computed/position.rs | 6 +++- components/style/values/generics/position.rs | 33 +++++++++++++++++++ components/style/values/specified/mod.rs | 21 ++---------- components/style/values/specified/position.rs | 18 +++++++++- 9 files changed, 75 insertions(+), 50 deletions(-) diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index e03de77a6cd..dc7054b9ae6 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -58,7 +58,7 @@ use style::properties::ComputedValues; use style::selector_parser::RestyleDamage; use style::servo::restyle_damage::ServoRestyleDamage; use style::str::char_is_whitespace; -use style::values::{self, Either, Auto}; +use style::values::{self, Either}; use style::values::computed::{Length, LengthOrPercentage, LengthOrPercentageOrAuto}; use style::values::computed::counters::ContentItem; use style::values::generics::box_::VerticalAlign; @@ -2512,7 +2512,7 @@ impl Fragment { // For absolutely and relatively positioned fragments we only establish a stacking // context if there is a z-index set. // See https://www.w3.org/TR/CSS2/visuren.html#z-index - self.style().get_position().z_index != Either::Second(Auto) + !self.style().get_position().z_index.is_auto() } // Get the effective z-index of this fragment. Z-indices only apply to positioned element diff --git a/components/style/properties/data.py b/components/style/properties/data.py index f87278f2478..419377ea01d 100644 --- a/components/style/properties/data.py +++ b/components/style/properties/data.py @@ -262,7 +262,6 @@ class Longhand(object): "ImageOrientation", "InitialLetter", "Integer", - "IntegerOrAuto", "JustifyContent", "JustifyItems", "JustifySelf", @@ -283,6 +282,7 @@ class Longhand(object): "TransformStyle", "XSpan", "XTextZoom", + "ZIndex", } return bool(self.keyword) diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 32ae4fe6d3d..11b8ac5210a 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -62,6 +62,7 @@ use values::computed::{NonNegativeLength, ToComputedValue, Percentage}; use values::computed::font::{FontSize, SingleFontFamily}; use values::computed::effects::{BoxShadow, Filter, SimpleShadow}; use values::computed::outline::OutlineStyle; +use values::generics::position::ZIndex; use values::generics::transform::TransformStyle; use computed_values::border_style; @@ -1750,8 +1751,8 @@ fn static_assert() { pub fn set_z_index(&mut self, v: longhands::z_index::computed_value::T) { match v { - Either::First(n) => self.gecko.mZIndex.set_value(CoordDataValue::Integer(n)), - Either::Second(Auto) => self.gecko.mZIndex.set_value(CoordDataValue::Auto), + ZIndex::Integer(n) => self.gecko.mZIndex.set_value(CoordDataValue::Integer(n)), + ZIndex::Auto => self.gecko.mZIndex.set_value(CoordDataValue::Auto), } } @@ -1771,11 +1772,11 @@ fn static_assert() { pub fn clone_z_index(&self) -> longhands::z_index::computed_value::T { return match self.gecko.mZIndex.as_value() { - CoordDataValue::Integer(n) => Either::First(n), - CoordDataValue::Auto => Either::Second(Auto), + CoordDataValue::Integer(n) => ZIndex::Integer(n), + CoordDataValue::Auto => ZIndex::Auto, _ => { debug_assert!(false); - Either::First(0) + ZIndex::Integer(0) } } } diff --git a/components/style/properties/longhand/position.mako.rs b/components/style/properties/longhand/position.mako.rs index 40003b7b8f7..67bcf59cb25 100644 --- a/components/style/properties/longhand/position.mako.rs +++ b/components/style/properties/longhand/position.mako.rs @@ -42,12 +42,14 @@ macro_rules! impl_align_conversions { }; } -${helpers.predefined_type("z-index", "IntegerOrAuto", - "Either::Second(Auto)", - spec="https://www.w3.org/TR/CSS2/visuren.html#z-index", - flags="CREATES_STACKING_CONTEXT", - animation_value_type="ComputedValue")} - +${helpers.predefined_type( + "z-index", + "ZIndex", + "computed::ZIndex::auto()", + spec="https://www.w3.org/TR/CSS2/visuren.html#z-index", + flags="CREATES_STACKING_CONTEXT", + animation_value_type="ComputedValue", +)} // CSS Flexible Box Layout Module Level 1 // http://www.w3.org/TR/css3-flexbox/ diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 14548efc909..c67ae7aea98 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -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 `` value. pub type Integer = CSSInteger; -/// | auto -pub type IntegerOrAuto = Either; - -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; diff --git a/components/style/values/computed/position.rs b/components/style/values/computed/position.rs index abd3522382d..eae5161cd05 100644 --- a/components/style/values/computed/position.rs +++ b/components/style/values/computed/position.rs @@ -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 `` @@ -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; diff --git a/components/style/values/generics/position.rs b/components/style/values/generics/position.rs index f00160d7015..6376d871e93 100644 --- a/components/style/values/generics/position.rs +++ b/components/style/values/generics/position.rs @@ -24,3 +24,36 @@ impl Position { } } } + +/// A generic value for the `z-index` property. +#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)] +#[derive(MallocSizeOf, PartialEq, ToAnimatedZero, ToComputedValue, ToCss)] +pub enum ZIndex { + /// An integer value. + Integer(Integer), + /// The keyword `auto`. + Auto, +} + +impl ZIndex { + /// 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, + } + } +} diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index abb8940778d..9b728dbe585 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -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 { } } -/// | auto -pub type IntegerOrAuto = Either; - -impl IntegerOrAuto { - /// Parse `auto` or a positive integer. - pub fn parse_positive<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - ) -> Result> { - 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; diff --git a/components/style/values/specified/position.rs b/components/style/values/specified/position.rs index 7ad5999ba1d..d768875ce3a 100644 --- a/components/style/values/specified/position.rs +++ b/components/style/values/specified/position.rs @@ -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 `` @@ -702,3 +703,18 @@ impl GridTemplateAreas { Either::Second(None_) } } + +/// A specified value for the `z-index` property. +pub type ZIndex = GenericZIndex; + +impl Parse for ZIndex { + fn parse<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { + if input.try(|i| i.expect_ident_matching("auto")).is_ok() { + return Ok(GenericZIndex::Auto); + } + Ok(GenericZIndex::Integer(Integer::parse(context, input)?)) + } +}