Simplify counter CSS properties

This commit is contained in:
Anthony Ramine 2018-02-04 03:15:15 +01:00
parent 2f8cd0fe22
commit df11a7fec7
7 changed files with 120 additions and 186 deletions

View file

@ -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<I>(Box<[(CustomIdent, I)]>);
/// A generic value for the `counter-increment` property.
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
pub struct CounterIncrement<I>(Counters<I>);
impl<I> CounterIntegerList<I> {
/// Returns the `none` value.
impl<I> CounterIncrement<I> {
/// Returns a new value for `counter-increment`.
#[inline]
pub fn none() -> CounterIntegerList<I> {
CounterIntegerList(vec![].into_boxed_slice())
}
/// Returns a new CounterIntegerList object.
pub fn new(vec: Vec<(CustomIdent, I)>) -> CounterIntegerList<I> {
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<I> ToCss for CounterIntegerList<I>
impl<I> Deref for CounterIncrement<I> {
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<I>(Counters<I>);
impl<I> CounterReset<I> {
/// Returns a new value for `counter-reset`.
#[inline]
pub fn new(counters: Vec<(CustomIdent, I)>) -> Self {
CounterReset(Counters(counters.into_boxed_slice()))
}
}
impl<I> Deref for CounterReset<I> {
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<I>(Box<[(CustomIdent, I)]>);
impl<I> Default for Counters<I> {
#[inline]
fn default() -> Self {
Counters(vec![].into_boxed_slice())
}
}
impl<I> ToCss for Counters<I>
where
I: ToCss
I: ToCss,
{
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> 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(" ")?;
}