Auto merge of #19945 - servo:derive-all-the-things, r=emilio

Simplify counter CSS properties

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19945)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-02-04 09:11:05 -05:00 committed by GitHub
commit 7f7a460a15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 120 additions and 186 deletions

View file

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

View file

@ -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);

View file

@ -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;
}

View file

@ -236,14 +236,18 @@
}
</%helpers:longhand>
${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",
)}

View file

@ -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<i32>;
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<i32>;
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<i32>;

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(" ")?;
}

View file

@ -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<Integer>;
impl SpecifiedIntegerList {
fn parse_with_default<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
default_value: i32
) -> Result<SpecifiedIntegerList, ParseError<'i>> {
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<Integer>;
impl Parse for CounterIncrement {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<CounterIncrement, ParseError<'i>> {
Ok(CounterIncrement(SpecifiedIntegerList::parse_with_default(context, input, 1)?))
) -> Result<Self, ParseError<'i>> {
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<Integer>;
impl Parse for CounterReset {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<CounterReset, ParseError<'i>> {
Ok(CounterReset(SpecifiedIntegerList::parse_with_default(context, input, 0)?))
) -> Result<Self, ParseError<'i>> {
Ok(Self::new(parse_counters(context, input, 0)?))
}
}
fn parse_counters<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
default_value: i32,
) -> Result<Vec<(CustomIdent, Integer)>, 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))
}
}