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

@ -5697,9 +5697,9 @@ clip-path
) {
unsafe {
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;
for (i, ref pair) in v.iter().enumerate() {
self.gecko.m${counter_property}s[i].mCounter.assign(pair.name.0.as_slice());
self.gecko.m${counter_property}s[i].mValue = pair.value;
}
}
}
@ -5717,12 +5717,16 @@ clip-path
pub fn clone_counter_${counter_property.lower()}(
&self
) -> longhands::counter_${counter_property.lower()}::computed_value::T {
use values::generics::counters::CounterPair;
use values::CustomIdent;
use gecko_string_cache::Atom;
longhands::counter_${counter_property.lower()}::computed_value::T::new(
self.gecko.m${counter_property}s.iter().map(|ref gecko_counter| {
(CustomIdent(Atom::from(gecko_counter.mCounter.to_string())), gecko_counter.mValue)
CounterPair {
name: CustomIdent(Atom::from(gecko_counter.mCounter.to_string())),
value: gecko_counter.mValue,
}
}).collect()
)
}

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(())
}
}

View file

@ -12,6 +12,7 @@ use style_traits::{ParseError, StyleParseErrorKind};
use values::CustomIdent;
#[cfg(feature = "gecko")]
use values::generics::CounterStyleOrNone;
use values::generics::counters::CounterPair;
use values::generics::counters::CounterIncrement as GenericCounterIncrement;
use values::generics::counters::CounterReset as GenericCounterReset;
#[cfg(feature = "gecko")]
@ -48,7 +49,7 @@ fn parse_counters<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
default_value: i32,
) -> Result<Vec<(CustomIdent, Integer)>, ParseError<'i>> {
) -> Result<Vec<CounterPair<Integer>>, ParseError<'i>> {
if input
.try(|input| input.expect_ident_matching("none"))
.is_ok()
@ -59,16 +60,16 @@ fn parse_counters<'i, 't>(
let mut counters = Vec::new();
loop {
let location = input.current_source_location();
let counter_name = match input.next() {
let 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
let value = input
.try(|input| Integer::parse(context, input))
.unwrap_or(Integer::new(default_value));
counters.push((counter_name, counter_delta))
counters.push(CounterPair { name, value });
}
if !counters.is_empty() {