From df11a7fec736c4c6846c7af2de329dd319c42f37 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sun, 4 Feb 2018 03:15:15 +0100 Subject: [PATCH] Simplify counter CSS properties --- components/layout/flow.rs | 4 +- components/layout/generated_content.rs | 4 +- components/style/properties/gecko.mako.rs | 5 +- .../properties/longhand/counters.mako.rs | 24 ++-- components/style/values/computed/counters.rs | 76 +----------- components/style/values/generics/counters.rs | 76 ++++++++---- components/style/values/specified/counters.rs | 117 ++++++------------ 7 files changed, 120 insertions(+), 186 deletions(-) diff --git a/components/layout/flow.rs b/components/layout/flow.rs index c21371b8520..3f2b64827af 100644 --- a/components/layout/flow.rs +++ b/components/layout/flow.rs @@ -1035,8 +1035,8 @@ impl BaseFlow { } } - if !style.get_counters().counter_reset.get_values().is_empty() || - !style.get_counters().counter_increment.get_values().is_empty() { + if !style.get_counters().counter_reset.is_empty() || + !style.get_counters().counter_increment.is_empty() { flags.insert(FlowFlags::AFFECTS_COUNTERS) } } diff --git a/components/layout/generated_content.rs b/components/layout/generated_content.rs index b1716d9dc54..0251c528e6c 100644 --- a/components/layout/generated_content.rs +++ b/components/layout/generated_content.rs @@ -272,7 +272,7 @@ impl<'a, 'b> ResolveGeneratedContentFragmentMutator<'a, 'b> { } self.traversal.list_item.truncate_to_level(self.level); - for &(ref counter_name, value) in fragment.style().get_counters().counter_reset.get_values() { + for &(ref counter_name, value) in &*fragment.style().get_counters().counter_reset { let counter_name = &*counter_name.0; if let Some(ref mut counter) = self.traversal.counters.get_mut(counter_name) { counter.reset(self.level, value); @@ -284,7 +284,7 @@ impl<'a, 'b> ResolveGeneratedContentFragmentMutator<'a, 'b> { self.traversal.counters.insert(counter_name.to_owned(), counter); } - for &(ref counter_name, value) in fragment.style().get_counters().counter_increment.get_values() { + for &(ref counter_name, value) in &*fragment.style().get_counters().counter_increment { let counter_name = &*counter_name.0; if let Some(ref mut counter) = self.traversal.counters.get_mut(counter_name) { counter.increment(self.level, value); diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 615df419d3a..5b68b1eae1b 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -5669,9 +5669,8 @@ clip-path v: longhands::counter_${counter_property.lower()}::computed_value::T ) { unsafe { - bindings::Gecko_ClearAndResizeCounter${counter_property}s(&mut self.gecko, - v.get_values().len() as u32); - for (i, &(ref name, value)) in v.get_values().into_iter().enumerate() { + bindings::Gecko_ClearAndResizeCounter${counter_property}s(&mut self.gecko, v.len() as u32); + for (i, &(ref name, value)) in v.iter().enumerate() { self.gecko.m${counter_property}s[i].mCounter.assign(name.0.as_slice()); self.gecko.m${counter_property}s[i].mValue = value; } diff --git a/components/style/properties/longhand/counters.mako.rs b/components/style/properties/longhand/counters.mako.rs index 0c906b75f0f..79585b6734a 100644 --- a/components/style/properties/longhand/counters.mako.rs +++ b/components/style/properties/longhand/counters.mako.rs @@ -236,14 +236,18 @@ } -${helpers.predefined_type("counter-increment", - "CounterIncrement", - initial_value="computed::CounterIncrement::none()", - animation_value_type="discrete", - spec="https://drafts.csswg.org/css-lists/#propdef-counter-increment")} +${helpers.predefined_type( + "counter-increment", + "CounterIncrement", + initial_value="Default::default()", + animation_value_type="discrete", + spec="https://drafts.csswg.org/css-lists/#propdef-counter-increment", +)} -${helpers.predefined_type("counter-reset", - "CounterReset", - initial_value="computed::CounterReset::none()", - animation_value_type="discrete", - spec="https://drafts.csswg.org/css-lists-3/#propdef-counter-reset")} +${helpers.predefined_type( + "counter-reset", + "CounterReset", + initial_value="Default::default()", + animation_value_type="discrete", + spec="https://drafts.csswg.org/css-lists-3/#propdef-counter-reset", +)} diff --git a/components/style/values/computed/counters.rs b/components/style/values/computed/counters.rs index 5c654ec8054..415c76d2a68 100644 --- a/components/style/values/computed/counters.rs +++ b/components/style/values/computed/counters.rs @@ -4,77 +4,11 @@ //! Computed values for counter properties -use values::CustomIdent; -use values::computed::{Context, ToComputedValue}; -use values::generics::counters::CounterIntegerList; -use values::specified::{CounterIncrement as SpecifiedCounterIncrement, CounterReset as SpecifiedCounterReset}; - -type ComputedIntegerList = CounterIntegerList; +use values::generics::counters::CounterIncrement as GenericCounterIncrement; +use values::generics::counters::CounterReset as GenericCounterReset; /// A computed value for the `counter-increment` property. -#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)] -pub struct CounterIncrement(pub ComputedIntegerList); +pub type CounterIncrement = GenericCounterIncrement; -impl CounterIncrement { - /// Returns the `none` value. - #[inline] - pub fn none() -> CounterIncrement { - CounterIncrement(ComputedIntegerList::new(Vec::new())) - } - - /// Returns a new computed `counter-increment` object with the given values. - pub fn new(vec: Vec<(CustomIdent, i32)>) -> CounterIncrement { - CounterIncrement(ComputedIntegerList::new(vec)) - } - - /// Returns the values of the computed `counter-increment` object. - pub fn get_values(&self) -> &[(CustomIdent, i32)] { - self.0.get_values() - } -} - -impl ToComputedValue for SpecifiedCounterIncrement { - type ComputedValue = CounterIncrement; - - fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { - CounterIncrement(self.0.to_computed_value(context)) - } - - fn from_computed_value(computed: &Self::ComputedValue) -> Self { - SpecifiedCounterIncrement(ToComputedValue::from_computed_value(&computed.0)) - } -} - -/// A computed value for the `counter-reset` property. -#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)] -pub struct CounterReset(pub ComputedIntegerList); - -impl CounterReset { - /// Returns the `none` value. - #[inline] - pub fn none() -> CounterReset { - CounterReset(ComputedIntegerList::new(Vec::new())) - } - - /// Returns a new computed `counter-reset` object with the given values. - pub fn new(vec: Vec<(CustomIdent, i32)>) -> CounterReset { - CounterReset(ComputedIntegerList::new(vec)) - } - - /// Returns the values of the computed `counter-reset` object. - pub fn get_values(&self) -> &[(CustomIdent, i32)] { - self.0.get_values() - } -} - -impl ToComputedValue for SpecifiedCounterReset { - type ComputedValue = CounterReset; - - fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { - CounterReset(self.0.to_computed_value(context)) - } - - fn from_computed_value(computed: &Self::ComputedValue) -> Self { - SpecifiedCounterReset(ToComputedValue::from_computed_value(&computed.0)) - } -} +/// A computed value for the `counter-increment` property. +pub type CounterReset = GenericCounterReset; diff --git a/components/style/values/generics/counters.rs b/components/style/values/generics/counters.rs index a2349ec222c..0123923395b 100644 --- a/components/style/values/generics/counters.rs +++ b/components/style/values/generics/counters.rs @@ -6,36 +6,68 @@ use std::fmt; use std::fmt::Write; +use std::ops::Deref; use style_traits::{CssWriter, ToCss}; use values::CustomIdent; -/// A generic value for both the `counter-increment` and `counter-reset` property. -/// -/// Keyword `none` is represented by an empty vector. -#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)] -pub struct CounterIntegerList(Box<[(CustomIdent, I)]>); +/// A generic value for the `counter-increment` property. +#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq, ToComputedValue, ToCss)] +pub struct CounterIncrement(Counters); -impl CounterIntegerList { - /// Returns the `none` value. +impl CounterIncrement { + /// Returns a new value for `counter-increment`. #[inline] - pub fn none() -> CounterIntegerList { - CounterIntegerList(vec![].into_boxed_slice()) - } - - /// Returns a new CounterIntegerList object. - pub fn new(vec: Vec<(CustomIdent, I)>) -> CounterIntegerList { - CounterIntegerList(vec.into_boxed_slice()) - } - - /// Returns the values of the CounterIntegerList object. - pub fn get_values(&self) -> &[(CustomIdent, I)] { - self.0.as_ref() + pub fn new(counters: Vec<(CustomIdent, I)>) -> Self { + CounterIncrement(Counters(counters.into_boxed_slice())) } } -impl ToCss for CounterIntegerList +impl Deref for CounterIncrement { + type Target = [(CustomIdent, I)]; + + #[inline] + fn deref(&self) -> &Self::Target { + &(self.0).0 + } +} + +/// A generic value for the `counter-reset` property. +#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq, ToComputedValue, ToCss)] +pub struct CounterReset(Counters); + +impl CounterReset { + /// Returns a new value for `counter-reset`. + #[inline] + pub fn new(counters: Vec<(CustomIdent, I)>) -> Self { + CounterReset(Counters(counters.into_boxed_slice())) + } +} + +impl Deref for CounterReset { + type Target = [(CustomIdent, I)]; + + #[inline] + fn deref(&self) -> &Self::Target { + &(self.0).0 + } +} + +/// A generic value for lists of counters. +/// +/// Keyword `none` is represented by an empty vector. +#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)] +pub struct Counters(Box<[(CustomIdent, I)]>); + +impl Default for Counters { + #[inline] + fn default() -> Self { + Counters(vec![].into_boxed_slice()) + } +} + +impl ToCss for Counters where - I: ToCss + I: ToCss, { #[inline] fn to_css(&self, dest: &mut CssWriter) -> fmt::Result @@ -47,7 +79,7 @@ where } let mut first = true; - for &(ref name, ref value) in self.get_values() { + for &(ref name, ref value) in &*self.0 { if !first { dest.write_str(" ")?; } diff --git a/components/style/values/specified/counters.rs b/components/style/values/specified/counters.rs index 5ffe35f80ce..ec8915eeaa4 100644 --- a/components/style/values/specified/counters.rs +++ b/components/style/values/specified/counters.rs @@ -4,99 +4,64 @@ //! Specified types for counter properties. -use cssparser::Parser; +use cssparser::{Token, Parser}; use parser::{Parse, ParserContext}; -use style_traits::ParseError; +use style_traits::{ParseError, StyleParseErrorKind}; use values::CustomIdent; -use values::generics::counters::CounterIntegerList; +use values::generics::counters::CounterIncrement as GenericCounterIncrement; +use values::generics::counters::CounterReset as GenericCounterReset; use values::specified::Integer; -/// A specified value for the `counter-increment` and `counter-reset` property. -type SpecifiedIntegerList = CounterIntegerList; - -impl SpecifiedIntegerList { - fn parse_with_default<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - default_value: i32 - ) -> Result> { - use cssparser::Token; - use style_traits::StyleParseErrorKind; - - if input.try(|input| input.expect_ident_matching("none")).is_ok() { - return Ok(CounterIntegerList::new(Vec::new())) - } - - let mut counters: Vec<(CustomIdent, Integer)> = Vec::new(); - loop { - let location = input.current_source_location(); - let counter_name = match input.next() { - Ok(&Token::Ident(ref ident)) => CustomIdent::from_ident(location, ident, &["none"])?, - Ok(t) => return Err(location.new_unexpected_token_error(t.clone())), - Err(_) => break, - }; - - let counter_delta = input.try(|input| Integer::parse(context, input)) - .unwrap_or(Integer::new(default_value)); - counters.push((counter_name, counter_delta)) - } - - if !counters.is_empty() { - Ok(CounterIntegerList::new(counters)) - } else { - Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) - } - } -} - /// A specified value for the `counter-increment` property. -#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] -#[derive(Clone, Debug, PartialEq, ToCss)] -pub struct CounterIncrement(pub SpecifiedIntegerList); - -impl CounterIncrement { - /// Returns a new specified `counter-increment` object with the given values. - pub fn new(vec: Vec<(CustomIdent, Integer)>) -> CounterIncrement { - CounterIncrement(SpecifiedIntegerList::new(vec)) - } - - /// Returns the values of the specified `counter-increment` object. - pub fn get_values(&self) -> &[(CustomIdent, Integer)] { - self.0.get_values() - } -} +pub type CounterIncrement = GenericCounterIncrement; impl Parse for CounterIncrement { fn parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't> - ) -> Result> { - Ok(CounterIncrement(SpecifiedIntegerList::parse_with_default(context, input, 1)?)) + ) -> Result> { + Ok(Self::new(parse_counters(context, input, 1)?)) } } -/// A specified value for the `counter-reset` property. -#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] -#[derive(Clone, Debug, PartialEq, ToCss)] -pub struct CounterReset(pub SpecifiedIntegerList); - -impl CounterReset { - /// Returns a new specified `counter-reset` object with the given values. - pub fn new(vec: Vec<(CustomIdent, Integer)>) -> CounterReset { - CounterReset(SpecifiedIntegerList::new(vec)) - } - - /// Returns the values of the specified `counter-reset` object. - pub fn get_values(&self) -> &[(CustomIdent, Integer)] { - self.0.get_values() - } -} +/// A specified value for the `counter-increment` property. +pub type CounterReset = GenericCounterReset; impl Parse for CounterReset { fn parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't> - ) -> Result> { - Ok(CounterReset(SpecifiedIntegerList::parse_with_default(context, input, 0)?)) + ) -> Result> { + Ok(Self::new(parse_counters(context, input, 0)?)) + } +} + +fn parse_counters<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + default_value: i32, +) -> Result, ParseError<'i>> { + if input.try(|input| input.expect_ident_matching("none")).is_ok() { + return Ok(vec![]); + } + + let mut counters = Vec::new(); + loop { + let location = input.current_source_location(); + let counter_name = match input.next() { + Ok(&Token::Ident(ref ident)) => CustomIdent::from_ident(location, ident, &["none"])?, + Ok(t) => return Err(location.new_unexpected_token_error(t.clone())), + Err(_) => break, + }; + + let counter_delta = input.try(|input| Integer::parse(context, input)) + .unwrap_or(Integer::new(default_value)); + counters.push((counter_name, counter_delta)) + } + + if !counters.is_empty() { + Ok(counters) + } else { + Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) } }