mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Simplify counter CSS properties
This commit is contained in:
parent
2f8cd0fe22
commit
df11a7fec7
7 changed files with 120 additions and 186 deletions
|
@ -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>;
|
||||
|
|
|
@ -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(" ")?;
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue