style: Derive ToCss for Counters.

Bug: 1457332
Reviewed-by: xidorn
MozReview-Commit-ID: 1jOglcqt1Dd
This commit is contained in:
Emilio Cobos Álvarez 2018-04-29 05:12:57 +02:00
parent 8c322b9c1e
commit a375baf84b
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 29 additions and 44 deletions

View file

@ -4,12 +4,19 @@
//! Generic types for counters-related CSS values.
use std::fmt;
use std::fmt::Write;
use std::ops::Deref;
use style_traits::{CssWriter, ToCss};
use values::CustomIdent;
/// A name / value pair for counters.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo,
ToComputedValue, ToCss)]
pub struct CounterPair<Integer> {
/// The name of the counter.
pub name: CustomIdent,
/// The value of the counter / increment / etc.
pub value: Integer,
}
/// A generic value for the `counter-increment` property.
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq, SpecifiedValueInfo,
ToComputedValue, ToCss)]
@ -18,13 +25,13 @@ pub struct CounterIncrement<I>(Counters<I>);
impl<I> CounterIncrement<I> {
/// Returns a new value for `counter-increment`.
#[inline]
pub fn new(counters: Vec<(CustomIdent, I)>) -> Self {
pub fn new(counters: Vec<CounterPair<I>>) -> Self {
CounterIncrement(Counters(counters.into_boxed_slice()))
}
}
impl<I> Deref for CounterIncrement<I> {
type Target = [(CustomIdent, I)];
type Target = [CounterPair<I>];
#[inline]
fn deref(&self) -> &Self::Target {
@ -40,13 +47,13 @@ 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 {
pub fn new(counters: Vec<CounterPair<I>>) -> Self {
CounterReset(Counters(counters.into_boxed_slice()))
}
}
impl<I> Deref for CounterReset<I> {
type Target = [(CustomIdent, I)];
type Target = [CounterPair<I>];
#[inline]
fn deref(&self) -> &Self::Target {
@ -58,8 +65,8 @@ impl<I> Deref for CounterReset<I> {
///
/// Keyword `none` is represented by an empty vector.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo,
ToComputedValue)]
pub struct Counters<I>(#[css(if_empty = "none")] Box<[(CustomIdent, I)]>);
ToComputedValue, ToCss)]
pub struct Counters<I>(#[css(iterable, if_empty = "none")] Box<[CounterPair<I>]>);
impl<I> Default for Counters<I> {
#[inline]
@ -67,30 +74,3 @@ impl<I> Default for Counters<I> {
Counters(vec![].into_boxed_slice())
}
}
impl<I> ToCss for Counters<I>
where
I: ToCss,
{
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: fmt::Write,
{
if self.0.is_empty() {
return dest.write_str("none");
}
let mut first = true;
for &(ref name, ref value) in &*self.0 {
if !first {
dest.write_str(" ")?;
}
first = false;
name.to_css(dest)?;
dest.write_str(" ")?;
value.to_css(dest)?;
}
Ok(())
}
}