From 95712d5edfa6ba17d2b0e8e3e66577b633f16edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:06:52 +0200 Subject: [PATCH 01/19] style: Minor reformatting in macros.rs --- components/style/macros.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/components/style/macros.rs b/components/style/macros.rs index 390a7c89538..79dafc6e050 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -35,10 +35,10 @@ macro_rules! define_numbered_css_keyword_enum { } impl $crate::parser::Parse for $name { - #[allow(missing_docs)] - fn parse<'i, 't>(_context: &$crate::parser::ParserContext, - input: &mut ::cssparser::Parser<'i, 't>) - -> Result<$name, ::style_traits::ParseError<'i>> { + fn parse<'i, 't>( + _context: &$crate::parser::ParserContext, + input: &mut ::cssparser::Parser<'i, 't>, + ) -> Result<$name, ::style_traits::ParseError<'i>> { try_match_ident_ignore_ascii_case! { input.expect_ident()?, $( $css => Ok($name::$variant), )+ } @@ -47,7 +47,8 @@ macro_rules! define_numbered_css_keyword_enum { impl ::style_traits::values::ToCss for $name { fn to_css(&self, dest: &mut W) -> ::std::fmt::Result - where W: ::std::fmt::Write, + where + W: ::std::fmt::Write, { match *self { $( $name::$variant => dest.write_str($css) ),+ @@ -67,9 +68,10 @@ macro_rules! add_impls_for_keyword_enum { ($name:ident) => { impl $crate::parser::Parse for $name { #[inline] - fn parse<'i, 't>(_context: &$crate::parser::ParserContext, - input: &mut ::cssparser::Parser<'i, 't>) - -> Result> { + fn parse<'i, 't>( + _context: &$crate::parser::ParserContext, + input: &mut ::cssparser::Parser<'i, 't>, + ) -> Result> { $name::parse(input) } } @@ -93,9 +95,10 @@ macro_rules! define_keyword_type { } impl $crate::parser::Parse for $name { - fn parse<'i, 't>(_context: &$crate::parser::ParserContext, - input: &mut ::cssparser::Parser<'i, 't>) - -> Result<$name, ::style_traits::ParseError<'i>> { + fn parse<'i, 't>( + _context: &$crate::parser::ParserContext, + input: &mut ::cssparser::Parser<'i, 't> + ) -> Result<$name, ::style_traits::ParseError<'i>> { input.expect_ident_matching($css).map(|_| $name).map_err(|e| e.into()) } } From 06c8e0163e665d86d2c43a92047396e9b26cc8c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:07:12 +0200 Subject: [PATCH 02/19] style: Manually expand ComputedValueAsSpecified in the keyword macros. --- components/style/macros.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/style/macros.rs b/components/style/macros.rs index 79dafc6e050..4edcf3f7d08 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -58,7 +58,7 @@ macro_rules! define_numbered_css_keyword_enum { } } -/// A macro for implementing `ComputedValueAsSpecified`, and `Parse` traits for +/// A macro for implementing `ToComputedValue`, and `Parse` traits for /// the enums defined using `define_css_keyword_enum` macro. /// /// NOTE: We should either move `Parse` trait to `style_traits` @@ -76,7 +76,17 @@ macro_rules! add_impls_for_keyword_enum { } } - impl $crate::values::computed::ComputedValueAsSpecified for $name {} + impl $crate::values::computed::ToComputedValue for $name { + type ComputedValue = $name; + + fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self { + self.clone() + } + + fn from_computed_value(other: &Self) -> Self { + other.clone() + } + } }; } From c87b485fa6adc871c167dbcd2be045ec58004724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:07:47 +0200 Subject: [PATCH 03/19] style: Derive ToComputedValue for keyword types. --- components/style/macros.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/style/macros.rs b/components/style/macros.rs index 4edcf3f7d08..503c13ba283 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -95,7 +95,7 @@ macro_rules! define_keyword_type { #[allow(missing_docs)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Animate, Clone, ComputeSquaredDistance, Copy, PartialEq)] - #[derive(ToAnimatedZero, ToCss)] + #[derive(ToAnimatedZero, ToComputedValue, ToCss)] pub struct $name; impl fmt::Debug for $name { @@ -113,7 +113,6 @@ macro_rules! define_keyword_type { } } - impl $crate::values::computed::ComputedValueAsSpecified for $name {} impl $crate::values::animated::AnimatedValueAsComputed for $name {} }; } From 9788ffe3e47badfd8db604d08ce106b01cc18f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:08:10 +0200 Subject: [PATCH 04/19] style: Derive ToComputedValue for font-variant-caps. --- components/style/properties/longhand/font.mako.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 6418a0c8215..f93add36312 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -1175,15 +1175,12 @@ ${helpers.single_keyword_system("font-variant-caps", spec="https://drafts.csswg.org/css-fonts/#propdef-font-synthesis"> use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} pub mod computed_value { pub use super::SpecifiedValue as T; } - #[derive(Clone, Debug, PartialEq)] + #[derive(Clone, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct SpecifiedValue { pub weight: bool, From 900eaec3cc8e79a94d219b18576779d9399a4b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:14:19 +0200 Subject: [PATCH 05/19] style: Remove the special-case in single_keyword_computed. The other case is pretty much the same. --- components/style/properties/helpers.mako.rs | 40 +++++++++------------ 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 76c78e40e24..ab290abefdc 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -500,32 +500,26 @@ <%def name="single_keyword(name, values, vector=False, **kwargs)"> <%call expr="single_keyword_computed(name, values, vector, **kwargs)"> - % if not "extra_specified" in kwargs and ("aliases" in kwargs or (("extra_%s_aliases" % product) in kwargs)): - impl ToComputedValue for SpecifiedValue { - type ComputedValue = computed_value::T; + impl ToComputedValue for SpecifiedValue { + type ComputedValue = computed_value::T; - #[inline] - fn to_computed_value(&self, _context: &Context) -> computed_value::T { - match *self { - % for value in data.longhands_by_name[name].keyword.values_for(product): - SpecifiedValue::${to_rust_ident(value)} => computed_value::T::${to_rust_ident(value)}, - % endfor - } - } - #[inline] - fn from_computed_value(computed: &computed_value::T) -> Self { - match *computed { - % for value in data.longhands_by_name[name].keyword.values_for(product): - computed_value::T::${to_rust_ident(value)} => SpecifiedValue::${to_rust_ident(value)}, - % endfor - } + #[inline] + fn to_computed_value(&self, _context: &Context) -> computed_value::T { + match *self { + % for value in data.longhands_by_name[name].keyword.values_for(product): + SpecifiedValue::${to_rust_ident(value)} => computed_value::T::${to_rust_ident(value)}, + % endfor } } - % else: - use values::computed::ComputedValueAsSpecified; - impl ComputedValueAsSpecified for SpecifiedValue {} - % endif - + #[inline] + fn from_computed_value(computed: &computed_value::T) -> Self { + match *computed { + % for value in data.longhands_by_name[name].keyword.values_for(product): + computed_value::T::${to_rust_ident(value)} => SpecifiedValue::${to_rust_ident(value)}, + % endfor + } + } + } From 156b371376cada7cd2bba991b65b4d816f08a88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:16:17 +0200 Subject: [PATCH 06/19] style: Use derive(TCV) in GeometryBox. --- components/style/values/generics/basic_shape.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/style/values/generics/basic_shape.rs b/components/style/values/generics/basic_shape.rs index 1e892a781a4..348e5c2a3ef 100644 --- a/components/style/values/generics/basic_shape.rs +++ b/components/style/values/generics/basic_shape.rs @@ -8,7 +8,6 @@ use std::fmt; use style_traits::ToCss; use values::animated::{Animate, Procedure, ToAnimatedZero}; -use values::computed::ComputedValueAsSpecified; use values::distance::{ComputeSquaredDistance, SquaredDistance}; use values::generics::border::BorderRadius; use values::generics::position::Position; @@ -20,14 +19,13 @@ pub type ClippingShape = ShapeSource = ShapeSource; From b9f5388635844033b9a3f3386ac7cf7dd61bd1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:18:50 +0200 Subject: [PATCH 07/19] style: Use derive for -x-span. --- components/style/macros.rs | 30 ++++++++++++------- .../style/properties/longhand/table.mako.rs | 11 ++++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/components/style/macros.rs b/components/style/macros.rs index 503c13ba283..bdbaaab5dbe 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -4,6 +4,24 @@ //! Various macro helpers. +macro_rules! trivial_to_computed_value { + ($name: ident) => { + impl $crate::values::computed::ToComputedValue for $name { + type ComputedValue = $name; + + fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self { + self.clone() + } + + fn from_computed_value(other: &Self) -> Self { + other.clone() + } + } + } +} + +trivial_to_computed_value!(i32); + /// A macro to parse an identifier, or return an `UnexpectedIndent` error /// otherwise. /// @@ -76,17 +94,7 @@ macro_rules! add_impls_for_keyword_enum { } } - impl $crate::values::computed::ToComputedValue for $name { - type ComputedValue = $name; - - fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self { - self.clone() - } - - fn from_computed_value(other: &Self) -> Self { - other.clone() - } - } + trivial_to_computed_value!($name); }; } diff --git a/components/style/properties/longhand/table.mako.rs b/components/style/properties/longhand/table.mako.rs index 68f40e22d37..5cc753b38ab 100644 --- a/components/style/properties/longhand/table.mako.rs +++ b/components/style/properties/longhand/table.mako.rs @@ -14,15 +14,12 @@ ${helpers.single_keyword("table-layout", "auto fixed", spec="Internal-only (for `` pres attr)" animation_value_type="none" internal="True"> - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} pub type SpecifiedValue = computed_value::T; pub mod computed_value { use std::fmt; use style_traits::ToCss; - #[derive(Clone, Copy, Debug, PartialEq)] + #[derive(Clone, Copy, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T(pub i32); @@ -39,8 +36,10 @@ ${helpers.single_keyword("table-layout", "auto fixed", } // never parse it, only set via presentation attribute - fn parse<'i, 't>(_: &ParserContext, _: &mut Parser<'i, 't>) - -> Result> { + fn parse<'i, 't>( + _: &ParserContext, + _: &mut Parser<'i, 't>, + ) -> Result> { Err(StyleParseError::UnspecifiedError.into()) } From 5b05aa7c5262c6f1a2b9741eab837ad6dd21c7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:25:53 +0200 Subject: [PATCH 08/19] style: Derive ToComputedValue in grid properties. --- components/style/properties/longhand/position.mako.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/components/style/properties/longhand/position.mako.rs b/components/style/properties/longhand/position.mako.rs index e1cd423a689..ff163346238 100644 --- a/components/style/properties/longhand/position.mako.rs +++ b/components/style/properties/longhand/position.mako.rs @@ -291,19 +291,18 @@ ${helpers.predefined_type("object-position", animation_value_type="discrete"> use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; pub type SpecifiedValue = computed_value::T; pub mod computed_value { - #[derive(Clone, Copy, Debug, Eq, PartialEq)] + #[derive(Clone, Copy, Debug, Eq, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum AutoFlow { Row, Column, } - #[derive(Clone, Copy, Debug, Eq, PartialEq)] + #[derive(Clone, Copy, Debug, Eq, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T { pub autoflow: AutoFlow, @@ -311,8 +310,6 @@ ${helpers.predefined_type("object-position", } } - impl ComputedValueAsSpecified for SpecifiedValue {} - impl ToCss for computed_value::T { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { dest.write_str(match self.autoflow { From 84f3073402e4d0ddae4815e581d98e286badb7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:29:06 +0200 Subject: [PATCH 09/19] style: Avoid ComputedValueAsSpecified in template-areas. Would need to impl ToComputedValue for Range and Box, which is undoable right now. --- components/style/properties/longhand/position.mako.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/style/properties/longhand/position.mako.rs b/components/style/properties/longhand/position.mako.rs index ff163346238..c92ffcf4f42 100644 --- a/components/style/properties/longhand/position.mako.rs +++ b/components/style/properties/longhand/position.mako.rs @@ -418,7 +418,6 @@ ${helpers.predefined_type("object-position", use std::ops::Range; use str::HTML_SPACE_CHARACTERS; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; pub mod computed_value { pub use super::SpecifiedValue as T; @@ -450,11 +449,13 @@ ${helpers.predefined_type("object-position", pub columns: Range, } - impl ComputedValueAsSpecified for TemplateAreas {} + trivial_to_computed_value!(TemplateAreas); impl Parse for TemplateAreas { - fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + fn parse<'i, 't>( + _context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { let mut strings = vec![]; while let Ok(string) = input.try(|i| i.expect_string().map(|s| s.as_ref().into())) { strings.push(string); From b94dd4885dc5f3829f81b8022db298a84f141b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:31:46 +0200 Subject: [PATCH 10/19] style: Derive ToComputedValue for TransitionProperty. --- .../style/properties/helpers/animated_properties.mako.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 759ba8ae0bb..7a62561a8e1 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -46,7 +46,7 @@ use values::animated::effects::Filter as AnimatedFilter; use values::animated::effects::FilterList as AnimatedFilterList; use values::animated::effects::TextShadowList as AnimatedTextShadowList; use values::computed::{Angle, BorderCornerRadius, CalcLengthOrPercentage}; -use values::computed::{ClipRect, Context, ComputedUrl, ComputedValueAsSpecified}; +use values::computed::{ClipRect, Context, ComputedUrl}; use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto}; use values::computed::{LengthOrPercentageOrNone, MaxLength, NonNegativeAu}; use values::computed::{NonNegativeNumber, Number, NumberOrPercentage, Percentage}; @@ -203,7 +203,7 @@ pub fn nscsspropertyid_is_animatable(property: nsCSSPropertyID) -> bool { // NB: This needs to be here because it needs all the longhands generated // beforehand. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Debug, Eq, Hash, PartialEq, ToCss)] +#[derive(Clone, Debug, Eq, Hash, PartialEq, ToCss, ToComputedValue)] pub enum TransitionProperty { /// All, any transitionable property changing should generate a transition. All, @@ -218,9 +218,6 @@ pub enum TransitionProperty { Unsupported(CustomIdent) } - -impl ComputedValueAsSpecified for TransitionProperty {} - impl TransitionProperty { /// Iterates over each longhand property. pub fn each ()>(mut cb: F) { From a4da2220c19c06f2c06e134422498c1b10851ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:44:56 +0200 Subject: [PATCH 11/19] style: Unify the trivial ToComputedValue implementations, and add impls for i32. --- components/style/macros.rs | 2 -- components/style/values/computed/mod.rs | 11 ++++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/components/style/macros.rs b/components/style/macros.rs index bdbaaab5dbe..aeec5516fda 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -20,8 +20,6 @@ macro_rules! trivial_to_computed_value { } } -trivial_to_computed_value!(i32); - /// A macro to parse an identifier, or return an `UnexpectedIndent` error /// otherwise. /// diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index d4d584a9f65..a1ca7b8422b 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -333,11 +333,12 @@ impl ToComputedValue for T } } -impl ComputedValueAsSpecified for Atom {} -impl ComputedValueAsSpecified for bool {} -impl ComputedValueAsSpecified for f32 {} - -impl ComputedValueAsSpecified for specified::BorderStyle {} +trivial_to_computed_value!(Atom); +trivial_to_computed_value!(u8); +trivial_to_computed_value!(bool); +trivial_to_computed_value!(i32); +trivial_to_computed_value!(f32); +trivial_to_computed_value!(BorderStyle); /// A `` value. pub type Number = CSSFloat; From 5e136a7a05906f626508a9e83a134061d0e16ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:40:57 +0200 Subject: [PATCH 12/19] style: Get rid of ComputedValueAsSpecified in box.mako.rs. --- .../style/properties/longhand/box.mako.rs | 46 ++++++++----------- components/style/values/mod.rs | 2 +- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/components/style/properties/longhand/box.mako.rs b/components/style/properties/longhand/box.mako.rs index 72a26d3f43f..b30a11c50fe 100644 --- a/components/style/properties/longhand/box.mako.rs +++ b/components/style/properties/longhand/box.mako.rs @@ -34,7 +34,6 @@ -moz-grid-group -moz-grid-line -moz-stack -moz-inline-stack -moz-deck -moz-popup -moz-groupbox""".split() %> - use values::computed::ComputedValueAsSpecified; use style_traits::ToCss; pub mod computed_value { @@ -137,7 +136,7 @@ } #[allow(non_camel_case_types)] - #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] pub enum SpecifiedValue { % for value in values: @@ -180,8 +179,6 @@ } } - impl ComputedValueAsSpecified for SpecifiedValue {} - % if product == "servo": fn cascade_property_custom(_declaration: &PropertyDeclaration, context: &mut computed::Context) { @@ -403,14 +400,13 @@ ${helpers.predefined_type("transition-delay", use Atom; use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; use values::KeyframesName; pub mod computed_value { pub use super::SpecifiedValue as T; } - #[derive(Clone, Debug, Eq, Hash, PartialEq)] + #[derive(Clone, Debug, Eq, Hash, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct SpecifiedValue(pub Option); @@ -448,8 +444,10 @@ ${helpers.predefined_type("transition-delay", } impl Parse for SpecifiedValue { - fn parse<'i, 't>(context: &ParserContext, input: &mut ::cssparser::Parser<'i, 't>) - -> Result> { + fn parse<'i, 't>( + context: &ParserContext, + input: &mut ::cssparser::Parser<'i, 't> + ) -> Result> { if let Ok(name) = input.try(|input| KeyframesName::parse(context, input)) { Ok(SpecifiedValue(Some(name))) } else { @@ -458,12 +456,13 @@ ${helpers.predefined_type("transition-delay", } } - pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + pub fn parse<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { SpecifiedValue::parse(context, input) } - impl ComputedValueAsSpecified for SpecifiedValue {} ${helpers.predefined_type("animation-duration", @@ -496,15 +495,13 @@ ${helpers.predefined_type("animation-timing-function", extra_prefixes="moz webkit" spec="https://drafts.csswg.org/css-animations/#propdef-animation-iteration-count", allowed_in_keyframe_block="False"> - use values::computed::ComputedValueAsSpecified; - pub mod computed_value { pub use super::SpecifiedValue as T; } // https://drafts.csswg.org/css-animations/#animation-iteration-count #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - #[derive(Clone, Debug, PartialEq, ToCss)] + #[derive(Clone, Debug, PartialEq, ToCss, ToComputedValue)] pub enum SpecifiedValue { Number(f32), Infinite, @@ -538,12 +535,12 @@ ${helpers.predefined_type("animation-timing-function", } #[inline] - pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + pub fn parse<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { SpecifiedValue::parse(context, input) } - - impl ComputedValueAsSpecified for SpecifiedValue {} <% animation_direction_custom_consts = { "alternate-reverse": "Alternate_reverse" } %> @@ -1626,15 +1623,13 @@ ${helpers.predefined_type("transform-origin", spec="https://drafts.csswg.org/css-contain/#contain-property"> use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} pub mod computed_value { pub type T = super::SpecifiedValue; } bitflags! { + #[derive(ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub flags SpecifiedValue: u8 { const LAYOUT = 0x01, @@ -1770,15 +1765,12 @@ ${helpers.single_keyword("-moz-orient", use std::fmt; use style_traits::ToCss; use values::CustomIdent; - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} pub mod computed_value { pub use super::SpecifiedValue as T; } - #[derive(Clone, Debug, PartialEq)] + #[derive(Clone, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum SpecifiedValue { Auto, @@ -1843,9 +1835,6 @@ ${helpers.predefined_type( use gecko_bindings::structs; use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} pub mod computed_value { pub use super::SpecifiedValue as T; @@ -1853,6 +1842,7 @@ ${helpers.predefined_type( bitflags! { /// These constants match Gecko's `NS_STYLE_TOUCH_ACTION_*` constants. + #[derive(ToComputedValue)] pub flags SpecifiedValue: u8 { const TOUCH_ACTION_NONE = structs::NS_STYLE_TOUCH_ACTION_NONE as u8, const TOUCH_ACTION_AUTO = structs::NS_STYLE_TOUCH_ACTION_AUTO as u8, diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 076e52de6af..5d093160b9e 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -127,7 +127,7 @@ impl ToCss for CustomIdent { } /// https://drafts.csswg.org/css-animations/#typedef-keyframes-name -#[derive(Clone, Debug)] +#[derive(Clone, Debug, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum KeyframesName { /// From 02d24061e740b61923744ffc500708c0b7a27965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 13:49:06 +0200 Subject: [PATCH 13/19] style: Avoid ComputedValueAsSpecified for TrackKeyword. --- components/style/values/generics/grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/style/values/generics/grid.rs b/components/style/values/generics/grid.rs index f8ec1470eb3..a70c0e7c4c3 100644 --- a/components/style/values/generics/grid.rs +++ b/components/style/values/generics/grid.rs @@ -136,7 +136,7 @@ define_css_keyword_enum!{ TrackKeyword: "max-content" => MaxContent, "min-content" => MinContent } -impl ComputedValueAsSpecified for TrackKeyword {} +add_impls_for_keyword_enum!(TrackKeyword); /// A track breadth for explicit grid track sizing. It's generic solely to /// avoid re-implementing it for the computed type. From 64ab73eabd16bf85ae616d0ba8f7583f121cda4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 14:33:53 +0200 Subject: [PATCH 14/19] style: Properly use integers on grid properties, and derive ToComputedValue. --- components/style/gecko/conversions.rs | 4 +- components/style/properties/gecko.mako.rs | 5 +- .../properties/shorthand/position.mako.rs | 4 +- components/style/values/computed/mod.rs | 12 +- components/style/values/generics/grid.rs | 105 +++++++++--------- components/style/values/specified/grid.rs | 26 +++-- components/style/values/specified/mod.rs | 47 +++++--- 7 files changed, 112 insertions(+), 91 deletions(-) diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index c00420e7b1d..7b89cb0cefb 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -18,7 +18,7 @@ use gecko_bindings::sugar::ns_style_coord::{CoordDataValue, CoordData, CoordData use std::f32::consts::PI; use stylesheets::{Origin, RulesMutateError}; use values::computed::{Angle, CalcLengthOrPercentage, Gradient, Image}; -use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, Percentage}; +use values::computed::{Integer, LengthOrPercentage, LengthOrPercentageOrAuto, Percentage}; use values::generics::box_::VerticalAlign; use values::generics::grid::{TrackListValue, TrackSize}; use values::generics::image::{CompatMode, Image as GenericImage, GradientItem}; @@ -896,7 +896,7 @@ impl TrackSize { } } -impl TrackListValue { +impl TrackListValue { /// Return TrackSize from given two nsStyleCoord pub fn from_gecko_style_coords(gecko_min: &T, gecko_max: &T) -> Self { TrackListValue::TrackSize(TrackSize::from_gecko_style_coords(gecko_min, gecko_max)) diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index fb59b00c704..a2d01bbf4ca 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -1700,7 +1700,7 @@ fn static_assert() { if let Some(integer) = v.line_num { // clamping the integer between a range self.gecko.${value.gecko}.mInteger = cmp::max(nsStyleGridLine_kMinLine, - cmp::min(integer.value(), nsStyleGridLine_kMaxLine)); + cmp::min(integer, nsStyleGridLine_kMaxLine)); } } @@ -1717,7 +1717,6 @@ fn static_assert() { pub fn clone_${value.name}(&self) -> longhands::${value.name}::computed_value::T { use gecko_bindings::structs::{nsStyleGridLine_kMinLine, nsStyleGridLine_kMaxLine}; use string_cache::Atom; - use values::specified::Integer; longhands::${value.name}::computed_value::T { is_span: self.gecko.${value.gecko}.mHasSpan, @@ -1735,7 +1734,7 @@ fn static_assert() { } else { debug_assert!(nsStyleGridLine_kMinLine <= self.gecko.${value.gecko}.mInteger); debug_assert!(self.gecko.${value.gecko}.mInteger <= nsStyleGridLine_kMaxLine); - Some(Integer::new(self.gecko.${value.gecko}.mInteger)) + Some(self.gecko.${value.gecko}.mInteger) }, } } diff --git a/components/style/properties/shorthand/position.mako.rs b/components/style/properties/shorthand/position.mako.rs index 667462f6b05..6a4661c4f56 100644 --- a/components/style/properties/shorthand/position.mako.rs +++ b/components/style/properties/shorthand/position.mako.rs @@ -147,7 +147,7 @@ let end = if input.try(|i| i.expect_delim('/')).is_ok() { GridLine::parse(context, input)? } else { - let mut line = GridLine::default(); + let mut line = GridLine::auto(); if start.line_num.is_none() && !start.is_span { line.ident = start.ident.clone(); // ident from start value should be taken } @@ -182,7 +182,7 @@ pub fn parse_value<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { fn line_with_ident_from(other: &GridLine) -> GridLine { - let mut this = GridLine::default(); + let mut this = GridLine::auto(); if other.line_num.is_none() && !other.is_span { this.ident = other.ident.clone(); } diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index a1ca7b8422b..304fee4bba9 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -21,9 +21,9 @@ use std::sync::Arc; use style_traits::ToCss; use super::{CSSFloat, CSSInteger}; use super::generics::{GreaterThanOrEqualToOne, NonNegative}; -use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize}; +use super::generics::grid::{GridLine as GenericGridLine, TrackBreadth as GenericTrackBreadth}; +use super::generics::grid::{TrackSize as GenericTrackSize, TrackList as GenericTrackList}; use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent; -use super::generics::grid::TrackList as GenericTrackList; use super::specified; pub use app_units::Au; @@ -44,7 +44,6 @@ pub use self::gecko::ScrollSnapPoint; pub use self::rect::LengthOrNumberRect; pub use super::{Auto, Either, None_}; pub use super::specified::BorderStyle; -pub use super::generics::grid::GridLine; pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNone, LengthOrNumber, LengthOrPercentage}; pub use self::length::{LengthOrPercentageOrAuto, LengthOrPercentageOrNone, MaxLength, MozLength}; pub use self::length::NonNegativeLengthOrPercentage; @@ -502,10 +501,13 @@ pub type TrackSize = GenericTrackSize; /// The computed value of a grid `` /// (could also be `` or ``) -pub type TrackList = GenericTrackList; +pub type TrackList = GenericTrackList; + +/// The computed value of a ``. +pub type GridLine = GenericGridLine; /// ` | ` -pub type GridTemplateComponent = GenericGridTemplateComponent; +pub type GridTemplateComponent = GenericGridTemplateComponent; impl ClipRectOrAuto { /// Return an auto (default for clip-rect and image-region) value diff --git a/components/style/values/generics/grid.rs b/components/style/values/generics/grid.rs index a70c0e7c4c3..5a05b318733 100644 --- a/components/style/values/generics/grid.rs +++ b/components/style/values/generics/grid.rs @@ -11,15 +11,15 @@ use std::{fmt, mem, usize}; use style_traits::{ToCss, ParseError, StyleParseError}; use values::{CSSFloat, CustomIdent, serialize_dimension}; use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; -use values::specified::Integer; +use values::specified; use values::specified::grid::parse_line_names; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] /// A `` type. /// /// https://drafts.csswg.org/css-grid/#typedef-grid-row-start-grid-line -pub struct GridLine { +pub struct GridLine { /// Flag to check whether it's a `span` keyword. pub is_span: bool, /// A custom identifier for named lines. @@ -30,24 +30,26 @@ pub struct GridLine { pub line_num: Option, } -impl GridLine { +impl GridLine { + /// The `auto` value. + pub fn auto() -> Self { + Self { + is_span: false, + line_num: None, + ident: None, + } + } + /// Check whether this `` represents an `auto` value. pub fn is_auto(&self) -> bool { self.ident.is_none() && self.line_num.is_none() && !self.is_span } } -impl Default for GridLine { - fn default() -> Self { - GridLine { - is_span: false, - ident: None, - line_num: None, - } - } -} - -impl ToCss for GridLine { +impl ToCss for GridLine +where + Integer: ToCss, +{ fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { if self.is_auto() { return dest.write_str("auto") @@ -57,9 +59,9 @@ impl ToCss for GridLine { dest.write_str("span")?; } - if let Some(i) = self.line_num { + if let Some(ref i) = self.line_num { dest.write_str(" ")?; - i.value().to_css(dest)?; + i.to_css(dest)?; } if let Some(ref s) = self.ident { @@ -71,9 +73,9 @@ impl ToCss for GridLine { } } -impl Parse for GridLine { +impl Parse for GridLine { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { - let mut grid_line = Default::default(); + let mut grid_line = Self::auto(); if input.try(|i| i.expect_ident_matching("auto")).is_ok() { return Ok(grid_line) } @@ -95,7 +97,8 @@ impl Parse for GridLine { } grid_line.is_span = true; - } else if let Ok(i) = input.try(|i| Integer::parse(context, i)) { + } else if let Ok(i) = input.try(|i| specified::Integer::parse(context, i)) { + // FIXME(emilio): Probably shouldn't reject if it's calc()... if i.value() == 0 || val_before_span || grid_line.line_num.is_some() { return Err(StyleParseError::UnspecifiedError.into()) } @@ -129,8 +132,6 @@ impl Parse for GridLine { } } -impl ComputedValueAsSpecified for GridLine {} - define_css_keyword_enum!{ TrackKeyword: "auto" => Auto, "max-content" => MaxContent, @@ -316,9 +317,15 @@ impl ToComputedValue for TrackSize { /// Helper function for serializing identifiers with a prefix and suffix, used /// for serializing (in grid). -pub fn concat_serialize_idents(prefix: &str, suffix: &str, - slice: &[CustomIdent], sep: &str, dest: &mut W) -> fmt::Result - where W: fmt::Write +pub fn concat_serialize_idents( + prefix: &str, + suffix: &str, + slice: &[CustomIdent], + sep: &str, + dest: &mut W, +) -> fmt::Result +where + W: fmt::Write { if let Some((ref first, rest)) = slice.split_first() { dest.write_str(prefix)?; @@ -338,8 +345,8 @@ pub fn concat_serialize_idents(prefix: &str, suffix: &str, /// /// https://drafts.csswg.org/css-grid/#typedef-track-repeat #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Copy, Debug, PartialEq, ToCss)] -pub enum RepeatCount { +#[derive(Clone, Copy, Debug, PartialEq, ToComputedValue, ToCss)] +pub enum RepeatCount { /// A positive integer. This is allowed only for `` and `` Number(Integer), /// An `` keyword allowed only for `` @@ -348,19 +355,15 @@ pub enum RepeatCount { AutoFit, } -impl Parse for RepeatCount { +impl Parse for RepeatCount { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { // Maximum number of repeat is 10000. The greater numbers should be clamped. const MAX_LINE: i32 = 10000; - if let Ok(mut i) = input.try(|i| Integer::parse(context, i)) { - if i.value() > 0 { - if i.value() > MAX_LINE { - i = Integer::new(MAX_LINE); - } - Ok(RepeatCount::Number(i)) - } else { - Err(StyleParseError::UnspecifiedError.into()) + if let Ok(mut i) = input.try(|i| specified::Integer::parse_positive(context, i)) { + if i.value() > MAX_LINE { + i = specified::Integer::new(MAX_LINE); } + Ok(RepeatCount::Number(i)) } else { try_match_ident_ignore_ascii_case! { input.expect_ident()?, "auto-fill" => Ok(RepeatCount::AutoFill), @@ -370,17 +373,15 @@ impl Parse for RepeatCount { } } -impl ComputedValueAsSpecified for RepeatCount {} - /// The structure containing `` and `` values. /// /// It can also hold `repeat()` function parameters, which expands into the respective /// values in its computed form. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Debug, PartialEq, ToComputedValue)] -pub struct TrackRepeat { +pub struct TrackRepeat { /// The number of times for the value to be repeated (could also be `auto-fit` or `auto-fill`) - pub count: RepeatCount, + pub count: RepeatCount, /// `` accompanying `` values. /// /// If there's no ``, then it's represented by an empty vector. @@ -392,7 +393,7 @@ pub struct TrackRepeat { pub track_sizes: Vec>, } -impl ToCss for TrackRepeat { +impl ToCss for TrackRepeat { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { dest.write_str("repeat(")?; self.count.to_css(dest)?; @@ -418,7 +419,7 @@ impl ToCss for TrackRepeat { Ok(()) } } -impl TrackRepeat { +impl TrackRepeat { /// If the repeat count is numeric, then expand the values and merge accordingly. pub fn expand(&self) -> Self { if let RepeatCount::Number(num) = self.count { @@ -460,11 +461,11 @@ impl TrackRepeat { /// Track list values. Can be or #[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -pub enum TrackListValue { +pub enum TrackListValue { /// A value. - TrackSize(TrackSize), + TrackSize(TrackSize), /// A value. - TrackRepeat(TrackRepeat), + TrackRepeat(TrackRepeat), } /// The type of a `` as determined during parsing. @@ -497,14 +498,14 @@ impl ComputedValueAsSpecified for TrackListType {} /// https://drafts.csswg.org/css-grid/#typedef-track-list #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Debug, PartialEq)] -pub struct TrackList { +pub struct TrackList { /// The type of this `` (auto, explicit or general). /// /// In order to avoid parsing the same value multiple times, this does a single traversal /// and arrives at the type of value it has parsed (or bails out gracefully with an error). pub list_type: TrackListType, /// A vector of ` | ` values. - pub values: Vec>, + pub values: Vec>, /// `` accompanying ` | ` values. /// /// If there's no ``, then it's represented by an empty vector. @@ -512,10 +513,10 @@ pub struct TrackList { /// length is always one value more than that of the ``. pub line_names: Box<[Box<[CustomIdent]>]>, /// `` value. There can only be one `` in a TrackList. - pub auto_repeat: Option>, + pub auto_repeat: Option>, } -impl ToCss for TrackList { +impl ToCss for TrackList { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { let auto_idx = match self.list_type { TrackListType::Auto(i) => i as usize, @@ -661,16 +662,16 @@ impl ComputedValueAsSpecified for LineNameList {} /// But it's implemented in gecko, so we have to as well. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)] -pub enum GridTemplateComponent { +pub enum GridTemplateComponent { /// `none` value. None, /// The grid `` - TrackList(TrackList), + TrackList(TrackList), /// A `subgrid ?` Subgrid(LineNameList), } -impl GridTemplateComponent { +impl GridTemplateComponent { /// Returns length of the s pub fn track_list_len(&self) -> usize { match *self { diff --git a/components/style/values/specified/grid.rs b/components/style/values/specified/grid.rs index f68325f9c27..72e6ebec5d8 100644 --- a/components/style/values/specified/grid.rs +++ b/components/style/values/specified/grid.rs @@ -14,7 +14,7 @@ use values::{CSSFloat, CustomIdent}; use values::computed::{self, Context, ToComputedValue}; use values::generics::grid::{GridTemplateComponent, RepeatCount, TrackBreadth, TrackKeyword, TrackRepeat}; use values::generics::grid::{LineNameList, TrackSize, TrackList, TrackListType, TrackListValue}; -use values::specified::LengthOrPercentage; +use values::specified::{LengthOrPercentage, Integer}; /// Parse a single flexible length. pub fn parse_flex<'i, 't>(input: &mut Parser<'i, 't>) -> Result> { @@ -97,9 +97,11 @@ enum RepeatType { Fixed, } -impl TrackRepeat { - fn parse_with_repeat_type<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result<(TrackRepeat, RepeatType), ParseError<'i>> { +impl TrackRepeat { + fn parse_with_repeat_type<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result<(Self, RepeatType), ParseError<'i>> { input.try(|i| i.expect_function_matching("repeat").map_err(|e| e.into())).and_then(|_| { input.parse_nested_block(|input| { let count = RepeatCount::parse(context, input)?; @@ -165,7 +167,7 @@ impl TrackRepeat { } } -impl Parse for TrackList { +impl Parse for TrackList { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { let mut current_names = vec![]; let mut names = vec![]; @@ -248,8 +250,8 @@ impl Parse for TrackList { } } -impl ToComputedValue for TrackList { - type ComputedValue = TrackList; +impl ToComputedValue for TrackList { + type ComputedValue = TrackList; #[inline] fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { @@ -321,7 +323,7 @@ impl ToComputedValue for TrackList { } } -impl Parse for GridTemplateComponent { +impl Parse for GridTemplateComponent { // FIXME: Derive Parse (probably with None_) fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { if input.try(|i| i.expect_ident_matching("none")).is_ok() { @@ -332,10 +334,12 @@ impl Parse for GridTemplateComponent { } } -impl GridTemplateComponent { +impl GridTemplateComponent { /// Parses a `GridTemplateComponent` except `none` keyword. - pub fn parse_without_none<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + pub fn parse_without_none<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { if let Ok(t) = input.try(|i| TrackList::parse(context, i)) { return Ok(GridTemplateComponent::TrackList(t)) } diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index ecceede0156..7c91096bb8b 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -19,8 +19,8 @@ use style_traits::values::specified::AllowedNumericType; use super::{Auto, CSSFloat, CSSInteger, Either, None_}; use super::computed::{Context, ToComputedValue}; use super::generics::{GreaterThanOrEqualToOne, NonNegative}; -use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize}; -use super::generics::grid::TrackList as GenericTrackList; +use super::generics::grid::{GridLine as GenericGridLine, TrackBreadth as GenericTrackBreadth}; +use super::generics::grid::{TrackSize as GenericTrackSize, TrackList as GenericTrackList}; use values::computed::ComputedValueAsSpecified; use values::specified::calc::CalcNode; @@ -52,7 +52,6 @@ pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind, SVGStrokeDash pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::time::Time; pub use self::transform::{TimingFunction, TransformOrigin}; -pub use super::generics::grid::GridLine; pub use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent; #[cfg(feature = "gecko")] @@ -356,9 +355,11 @@ impl ToComputedValue for Opacity { } } +/// An specified ``, optionally coming from a `calc()` expression. +/// +/// https://drafts.csswg.org/css-values/#integers #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[allow(missing_docs)] pub struct Integer { value: CSSInteger, was_calc: bool, @@ -387,7 +388,6 @@ impl Integer { } } - impl Parse for Integer { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { parse_integer(context, input) @@ -395,25 +395,37 @@ impl Parse for Integer { } impl Integer { - #[allow(missing_docs)] - pub fn parse_with_minimum<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>, min: i32) - -> Result> { + /// Parse an integer value which is at least `min`. + pub fn parse_with_minimum<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + min: i32 + ) -> Result> { match parse_integer(context, input) { + // FIXME(emilio): The spec asks us to avoid rejecting it at parse + // time except until computed value time. + // + // It's not totally clear it's worth it though, and no other browser + // does this. Ok(value) if value.value() >= min => Ok(value), Ok(_value) => Err(StyleParseError::UnspecifiedError.into()), Err(e) => Err(e), } } - #[allow(missing_docs)] - pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + /// Parse a non-negative integer. + pub fn parse_non_negative<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { Integer::parse_with_minimum(context, input, 0) } - #[allow(missing_docs)] - pub fn parse_positive<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { + /// Parse a positive integer (>= 1). + pub fn parse_positive<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't> + ) -> Result> { Integer::parse_with_minimum(context, input, 1) } } @@ -484,10 +496,13 @@ pub type TrackSize = GenericTrackSize; /// The specified value of a grid `` /// (could also be `` or ``) -pub type TrackList = GenericTrackList; +pub type TrackList = GenericTrackList; + +/// The specified value of a ``. +pub type GridLine = GenericGridLine; /// ` | ` -pub type GridTemplateComponent = GenericGridTemplateComponent; +pub type GridTemplateComponent = GenericGridTemplateComponent; /// | | pub type LengthOrPercentageOrNumber = Either; From 4c283fbed60436d917a5aa914a72a2586a33c126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 14:37:26 +0200 Subject: [PATCH 15/19] style: Get rid of ComputedValueAsSpecified for alignment props. --- components/style/values/computed/align.rs | 6 +----- components/style/values/computed/mod.rs | 1 + components/style/values/specified/align.rs | 7 ++++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/components/style/values/computed/align.rs b/components/style/values/computed/align.rs index d90ab28782c..785f396cf2e 100644 --- a/components/style/values/computed/align.rs +++ b/components/style/values/computed/align.rs @@ -8,7 +8,7 @@ use std::fmt; use style_traits::ToCss; -use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; +use values::computed::{Context, ToComputedValue}; use values::specified; pub use super::specified::{AlignItems, AlignJustifyContent, AlignJustifySelf}; @@ -69,7 +69,3 @@ impl ToComputedValue for specified::JustifyItems { computed.specified } } - -impl ComputedValueAsSpecified for AlignItems {} -impl ComputedValueAsSpecified for AlignJustifyContent {} -impl ComputedValueAsSpecified for AlignJustifySelf {} diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 304fee4bba9..53a7fbe74c4 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -334,6 +334,7 @@ impl ToComputedValue for T trivial_to_computed_value!(Atom); trivial_to_computed_value!(u8); +trivial_to_computed_value!(u16); trivial_to_computed_value!(bool); trivial_to_computed_value!(i32); trivial_to_computed_value!(f32); diff --git a/components/style/values/specified/align.rs b/components/style/values/specified/align.rs index c5b6212e89c..424f0042d10 100644 --- a/components/style/values/specified/align.rs +++ b/components/style/values/specified/align.rs @@ -18,6 +18,7 @@ bitflags! { /// Constants shared by multiple CSS Box Alignment properties /// /// These constants match Gecko's `NS_STYLE_ALIGN_*` constants. + #[derive(ToComputedValue)] pub flags AlignFlags: u8 { // Enumeration stored in the lower 5 bits: /// 'auto' @@ -113,7 +114,7 @@ const ALIGN_ALL_SHIFT: u32 = structs::NS_STYLE_ALIGN_ALL_SHIFT; /// /// The 16-bit field stores the primary value in its lower 8 bits, and the optional fallback value /// in its upper 8 bits. This matches the representation of these properties in Gecko. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] pub struct AlignJustifyContent(u16); @@ -205,7 +206,7 @@ impl Parse for AlignJustifyContent { /// Value of the `align-self` or `justify-self` property. /// /// https://drafts.csswg.org/css-align/#self-alignment -#[derive(Clone, Copy, Debug, Eq, PartialEq, ToCss)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, ToComputedValue, ToCss)] pub struct AlignJustifySelf(pub AlignFlags); impl AlignJustifySelf { @@ -242,7 +243,7 @@ impl Parse for AlignJustifySelf { /// Value of the `align-items` property /// /// https://drafts.csswg.org/css-align/#self-alignment -#[derive(Clone, Copy, Debug, Eq, PartialEq, ToCss)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, ToComputedValue, ToCss)] pub struct AlignItems(pub AlignFlags); impl AlignItems { From a3bfa5db3e1d19dfd352987b1e4f109110098ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 14:38:38 +0200 Subject: [PATCH 16/19] style: Get rid of a few ComputedValueAsSpecified in grid. Need Box to remove the macro invocation. --- components/style/values/generics/grid.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/style/values/generics/grid.rs b/components/style/values/generics/grid.rs index 5a05b318733..cd828c28a00 100644 --- a/components/style/values/generics/grid.rs +++ b/components/style/values/generics/grid.rs @@ -10,7 +10,7 @@ use parser::{Parse, ParserContext}; use std::{fmt, mem, usize}; use style_traits::{ToCss, ParseError, StyleParseError}; use values::{CSSFloat, CustomIdent, serialize_dimension}; -use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; +use values::computed::{Context, ToComputedValue}; use values::specified; use values::specified::grid::parse_line_names; @@ -471,7 +471,7 @@ pub enum TrackListValue { /// The type of a `` as determined during parsing. /// /// https://drafts.csswg.org/css-grid/#typedef-track-list -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum TrackListType { /// [``](https://drafts.csswg.org/css-grid/#typedef-auto-track-list) @@ -491,8 +491,6 @@ pub enum TrackListType { Explicit, } -impl ComputedValueAsSpecified for TrackListType {} - /// A grid `` type. /// /// https://drafts.csswg.org/css-grid/#typedef-track-list @@ -573,6 +571,8 @@ pub struct LineNameList { pub fill_idx: Option, } +trivial_to_computed_value!(LineNameList); + impl Parse for LineNameList { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { input.expect_ident_matching("subgrid")?; @@ -655,8 +655,6 @@ impl ToCss for LineNameList { } } -impl ComputedValueAsSpecified for LineNameList {} - /// Variants for ` | ` /// Subgrid deferred to Level 2 spec due to lack of implementation. /// But it's implemented in gecko, so we have to as well. From d1a3cb6d94f877edd03718569102fa73b37e850d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 14:49:27 +0200 Subject: [PATCH 17/19] style: Get rid of ComputedValueAsSpecified in inherited_text.rs --- .../properties/longhand/inherited_svg.mako.rs | 4 ---- .../longhand/inherited_text.mako.rs | 23 ++++++++----------- components/style/values/mod.rs | 2 +- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/components/style/properties/longhand/inherited_svg.mako.rs b/components/style/properties/longhand/inherited_svg.mako.rs index 9b05707f33c..3d3b82541fc 100644 --- a/components/style/properties/longhand/inherited_svg.mako.rs +++ b/components/style/properties/longhand/inherited_svg.mako.rs @@ -268,10 +268,6 @@ ${helpers.predefined_type("marker-end", "UrlOrNone", "Either::Second(None_)", spec="Nonstandard (Internal-only)" allow_empty="True"> use values::CustomIdent; - use values::computed::ComputedValueAsSpecified; - - - impl ComputedValueAsSpecified for SpecifiedValue { } pub type SpecifiedValue = CustomIdent; diff --git a/components/style/properties/longhand/inherited_text.mako.rs b/components/style/properties/longhand/inherited_text.mako.rs index 346d864b3bb..29483ba43f7 100644 --- a/components/style/properties/longhand/inherited_text.mako.rs +++ b/components/style/properties/longhand/inherited_text.mako.rs @@ -255,9 +255,8 @@ ${helpers.single_keyword("text-align-last", } } % else: - use values::computed::ComputedValueAsSpecified; - impl ComputedValueAsSpecified for SpecifiedValue {} pub use self::computed_value::T as SpecifiedValue; + add_impls_for_keyword_enum!(SpecifiedValue); pub fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { computed_value::T::parse(input) @@ -287,9 +286,6 @@ ${helpers.predefined_type("word-spacing", use cssparser::RGBA; use std::fmt; use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - - impl ComputedValueAsSpecified for SpecifiedValue {} #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] @@ -299,6 +295,8 @@ ${helpers.predefined_type("word-spacing", pub line_through: Option, } + trivial_to_computed_value!(SpecifiedValue); + pub mod computed_value { pub type T = super::SpecifiedValue; } @@ -374,9 +372,7 @@ ${helpers.predefined_type("word-spacing", // !important. flags="APPLIES_TO_PLACEHOLDER" spec="https://drafts.csswg.org/css-text/#propdef-white-space"> - use values::computed::ComputedValueAsSpecified; - impl ComputedValueAsSpecified for SpecifiedValue {} - + trivial_to_computed_value!(SpecifiedValue); % if product != "gecko": impl SpecifiedValue { pub fn allow_wrap(&self) -> bool { @@ -433,8 +429,8 @@ ${helpers.predefined_type( pub mod computed_value { - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Debug, PartialEq, ToCss)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf, ToComputedValue))] pub enum T { Keyword(KeywordValue), None, @@ -449,8 +445,8 @@ ${helpers.predefined_type( } } - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Debug, PartialEq, ToCss)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum SpecifiedValue { Keyword(KeywordValue), None, @@ -614,26 +610,25 @@ ${helpers.predefined_type( <%helpers:longhand name="text-emphasis-position" animation_value_type="discrete" products="gecko" spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position"> - use values::computed::ComputedValueAsSpecified; use style_traits::ToCss; define_css_keyword_enum!(HorizontalWritingModeValue: "over" => Over, "under" => Under); + add_impls_for_keyword_enum!(VerticalWritingModeValue); define_css_keyword_enum!(VerticalWritingModeValue: "right" => Right, "left" => Left); + add_impls_for_keyword_enum!(HorizontalWritingModeValue); #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - #[derive(Clone, Debug, PartialEq, ToCss)] + #[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)] pub struct SpecifiedValue(pub HorizontalWritingModeValue, pub VerticalWritingModeValue); pub mod computed_value { pub type T = super::SpecifiedValue; } - impl ComputedValueAsSpecified for SpecifiedValue {} - pub fn get_initial_value() -> computed_value::T { SpecifiedValue(HorizontalWritingModeValue::Over, VerticalWritingModeValue::Right) } diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 5d093160b9e..c1f533dd5d4 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -98,7 +98,7 @@ impl Parse for Either { } /// https://drafts.csswg.org/css-values-4/#custom-idents -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Debug, Eq, Hash, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct CustomIdent(pub Atom); From b12a34e06ecab1a4bb7067f0a23686b4803c5546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 14:53:22 +0200 Subject: [PATCH 18/19] style: Kill CVAS in image.rs. --- components/style/values/generics/image.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/style/values/generics/image.rs b/components/style/values/generics/image.rs index 4f42e459036..874511330d4 100644 --- a/components/style/values/generics/image.rs +++ b/components/style/values/generics/image.rs @@ -8,10 +8,10 @@ use Atom; use cssparser::serialize_identifier; -use custom_properties::SpecifiedValue; +use custom_properties; use std::fmt; use style_traits::ToCss; -use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; +use values::computed::{Context, ToComputedValue}; /// An [image]. /// @@ -153,7 +153,7 @@ define_css_keyword_enum!(ShapeExtent: "contain" => Contain, "cover" => Cover ); -impl ComputedValueAsSpecified for ShapeExtent {} +add_impls_for_keyword_enum!(ShapeExtent); /// A gradient item. /// https://drafts.csswg.org/css-images-4/#color-stop-syntax @@ -186,10 +186,10 @@ pub struct PaintWorklet { pub name: Atom, /// The arguments for the worklet. /// TODO: store a parsed representation of the arguments. - pub arguments: Vec, + pub arguments: Vec, } -impl ComputedValueAsSpecified for PaintWorklet {} +trivial_to_computed_value!(PaintWorklet); impl ToCss for PaintWorklet { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { From ba5897adbd7865cc07359bb4ad52bd56ba74286a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 9 Sep 2017 16:03:03 +0200 Subject: [PATCH 19/19] style: Kill a few more CVAS uses in font.mako.rs. --- components/style/properties/longhand/font.mako.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index f93add36312..18c25ecbd79 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -2183,11 +2183,8 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control- <%helpers:longhand name="-x-lang" products="gecko" animation_value_type="none" internal="True" spec="Internal (not web-exposed)"> - use values::computed::ComputedValueAsSpecified; pub use self::computed_value::T as SpecifiedValue; - impl ComputedValueAsSpecified for SpecifiedValue {} - pub mod computed_value { use Atom; use std::fmt; @@ -2199,7 +2196,7 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control- } } - #[derive(Clone, Debug, PartialEq)] + #[derive(Clone, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T(pub Atom); } @@ -2390,11 +2387,8 @@ ${helpers.single_keyword("-moz-math-variant", <%helpers:longhand name="-x-text-zoom" products="gecko" animation_value_type="none" internal="True" spec="Internal (not web-exposed)"> - use values::computed::ComputedValueAsSpecified; pub use self::computed_value::T as SpecifiedValue; - impl ComputedValueAsSpecified for SpecifiedValue {} - pub mod computed_value { use std::fmt; use style_traits::ToCss; @@ -2405,7 +2399,7 @@ ${helpers.single_keyword("-moz-math-variant", } } - #[derive(Clone, Debug, PartialEq)] + #[derive(Clone, Debug, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] /// text-zoom. Enable if true, disable if false pub struct T(pub bool);