Introduce enum CounterBound for the bounds in counter_style::Ranges

This commit is contained in:
Anthony Ramine 2018-03-04 23:05:51 +01:00
parent e3f69668ae
commit 0b3a5b42ba
2 changed files with 28 additions and 11 deletions

View file

@ -444,7 +444,18 @@ impl Parse for Negative {
/// ///
/// Empty Vec represents 'auto' /// Empty Vec represents 'auto'
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Ranges(pub Vec<Range<Option<i32>>>); pub struct Ranges(pub Vec<Range<CounterBound>>);
/// A bound found in `Ranges`.
#[derive(Clone, Copy, Debug)]
pub enum CounterBound {
/// An integer bound.
///
/// FIXME(https://github.com/servo/servo/issues/20197)
Integer(i32),
/// The infinite bound.
Infinite,
}
impl Parse for Ranges { impl Parse for Ranges {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
@ -454,7 +465,7 @@ impl Parse for Ranges {
input.parse_comma_separated(|input| { input.parse_comma_separated(|input| {
let opt_start = parse_bound(input)?; let opt_start = parse_bound(input)?;
let opt_end = parse_bound(input)?; let opt_end = parse_bound(input)?;
if let (Some(start), Some(end)) = (opt_start, opt_end) { if let (CounterBound::Integer(start), CounterBound::Integer(end)) = (opt_start, opt_end) {
if start > end { if start > end {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} }
@ -465,11 +476,17 @@ impl Parse for Ranges {
} }
} }
fn parse_bound<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Option<i32>, ParseError<'i>> { fn parse_bound<'i, 't>(
input: &mut Parser<'i, 't>,
) -> Result<CounterBound, ParseError<'i>> {
let location = input.current_source_location(); let location = input.current_source_location();
match *input.next()? { match *input.next()? {
Token::Number { int_value: Some(v), .. } => Ok(Some(v)), Token::Number { int_value: Some(v), .. } => {
Token::Ident(ref ident) if ident.eq_ignore_ascii_case("infinite") => Ok(None), Ok(CounterBound::Integer(v))
}
Token::Ident(ref ident) if ident.eq_ignore_ascii_case("infinite") => {
Ok(CounterBound::Infinite)
}
ref t => Err(location.new_unexpected_token_error(t.clone())), ref t => Err(location.new_unexpected_token_error(t.clone())),
} }
} }
@ -493,7 +510,7 @@ impl ToCss for Ranges {
} }
} }
fn range_to_css<W>(range: &Range<Option<i32>>, dest: &mut CssWriter<W>) -> fmt::Result fn range_to_css<W>(range: &Range<CounterBound>, dest: &mut CssWriter<W>) -> fmt::Result
where where
W: Write, W: Write,
{ {
@ -502,11 +519,11 @@ where
bound_to_css(range.end, dest) bound_to_css(range.end, dest)
} }
fn bound_to_css<W>(range: Option<i32>, dest: &mut CssWriter<W>) -> fmt::Result fn bound_to_css<W>(range: CounterBound, dest: &mut CssWriter<W>) -> fmt::Result
where where
W: Write, W: Write,
{ {
if let Some(finite) = range { if let CounterBound::Integer(finite) = range {
finite.to_css(dest) finite.to_css(dest)
} else { } else {
dest.write_str("infinite") dest.write_str("infinite")

View file

@ -6,7 +6,7 @@
use byteorder::{BigEndian, WriteBytesExt}; use byteorder::{BigEndian, WriteBytesExt};
use computed_values::{font_stretch, font_style, font_weight}; use computed_values::{font_stretch, font_style, font_weight};
use counter_style; use counter_style::{self, CounterBound};
use cssparser::UnicodeRange; use cssparser::UnicodeRange;
use font_face::{FontFaceRuleData, Source, FontDisplay, FontWeight}; use font_face::{FontFaceRuleData, Source, FontDisplay, FontWeight};
use gecko_bindings::bindings; use gecko_bindings::bindings;
@ -324,8 +324,8 @@ impl ToNsCssValue for counter_style::Ranges {
nscssvalue.set_auto(); nscssvalue.set_auto();
} else { } else {
nscssvalue.set_pair_list(self.0.into_iter().map(|range| { nscssvalue.set_pair_list(self.0.into_iter().map(|range| {
fn set_bound(bound: Option<i32>, nscssvalue: &mut nsCSSValue) { fn set_bound(bound: CounterBound, nscssvalue: &mut nsCSSValue) {
if let Some(finite) = bound { if let CounterBound::Integer(finite) = bound {
nscssvalue.set_integer(finite) nscssvalue.set_integer(finite)
} else { } else {
nscssvalue.set_enum(structs::NS_STYLE_COUNTER_RANGE_INFINITE as i32) nscssvalue.set_enum(structs::NS_STYLE_COUNTER_RANGE_INFINITE as i32)