diff --git a/components/style/counter_style/mod.rs b/components/style/counter_style/mod.rs index 77de5f52d2b..9e2bc096e2d 100644 --- a/components/style/counter_style/mod.rs +++ b/components/style/counter_style/mod.rs @@ -19,7 +19,6 @@ use selectors::parser::SelectorParseErrorKind; use std::fmt::{self, Write}; use std::mem; use std::num::Wrapping; -use std::ops::Range; use style_traits::{Comma, CssWriter, OneOrMoreSeparated, ParseError}; use style_traits::{StyleParseErrorKind, ToCss}; @@ -261,7 +260,7 @@ counter_style_descriptors! { "suffix" suffix / set_suffix [_]: Symbol, /// - "range" range / set_range [_]: Ranges, + "range" range / set_range [_]: CounterRanges, /// "pad" pad / set_pad [_]: Pad, @@ -371,7 +370,7 @@ impl Parse for System { "additive" => Ok(System::Additive), "fixed" => { let first_symbol_value = input.try(|i| Integer::parse(context, i)).ok(); - Ok(System::Fixed { first_symbol_value: first_symbol_value }) + Ok(System::Fixed { first_symbol_value }) } "extends" => { let other = parse_counter_style_name(input)?; @@ -409,11 +408,10 @@ impl ToCss for System { } /// -#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] -#[derive(Clone, Debug, Eq, PartialEq, ToComputedValue, ToCss, ToShmem)] +#[derive(Clone, Debug, Eq, PartialEq, ToComputedValue, ToCss, ToShmem, MallocSizeOf)] pub enum Symbol { /// - String(String), + String(crate::OwnedStr), /// Ident(CustomIdent), // Not implemented: @@ -428,7 +426,7 @@ impl Parse for Symbol { ) -> Result> { let location = input.current_source_location(); match *input.next()? { - Token::QuotedString(ref s) => Ok(Symbol::String(s.as_ref().to_owned())), + Token::QuotedString(ref s) => Ok(Symbol::String(s.as_ref().to_owned().into())), Token::Ident(ref s) => Ok(Symbol::Ident(CustomIdent::from_ident(location, s, &[])?)), ref t => Err(location.new_unexpected_token_error(t.clone())), } @@ -463,12 +461,25 @@ impl Parse for Negative { } /// -/// -/// Empty Vec represents 'auto' -#[derive(Clone, Debug, ToShmem)] -pub struct Ranges(pub Vec>); +#[derive(Clone, Debug, ToShmem, ToCss)] +pub struct CounterRange { + /// The start of the range. + pub start: CounterBound, + /// The end of the range. + pub end: CounterBound, +} -/// A bound found in `Ranges`. +/// +/// +/// Empty represents 'auto' +#[derive(Clone, Debug, ToShmem, ToCss)] +#[css(comma)] +pub struct CounterRanges( + #[css(iterable, if_empty = "auto")] + pub crate::OwnedSlice, +); + +/// A bound found in `CounterRanges`. #[derive(Clone, Copy, Debug, ToCss, ToShmem)] pub enum CounterBound { /// An integer bound. @@ -477,7 +488,7 @@ pub enum CounterBound { Infinite, } -impl Parse for Ranges { +impl Parse for CounterRanges { fn parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't>, @@ -486,25 +497,25 @@ impl Parse for Ranges { .try(|input| input.expect_ident_matching("auto")) .is_ok() { - Ok(Ranges(Vec::new())) - } else { - input - .parse_comma_separated(|input| { - let opt_start = parse_bound(context, input)?; - let opt_end = parse_bound(context, input)?; - if let (CounterBound::Integer(start), CounterBound::Integer(end)) = - (opt_start, opt_end) - { - if start > end { - return Err( - input.new_custom_error(StyleParseErrorKind::UnspecifiedError) - ); - } - } - Ok(opt_start..opt_end) - }) - .map(Ranges) + return Ok(CounterRanges(Default::default())); } + + let ranges = input.parse_comma_separated(|input| { + let start = parse_bound(context, input)?; + let end = parse_bound(context, input)?; + if let (CounterBound::Integer(start), CounterBound::Integer(end)) = + (start, end) + { + if start > end { + return Err( + input.new_custom_error(StyleParseErrorKind::UnspecifiedError) + ); + } + } + Ok(CounterRange { start, end }) + })?; + + Ok(CounterRanges(ranges.into())) } } @@ -519,34 +530,6 @@ fn parse_bound<'i, 't>( Ok(CounterBound::Infinite) } -impl ToCss for Ranges { - fn to_css(&self, dest: &mut CssWriter) -> fmt::Result - where - W: Write, - { - let mut iter = self.0.iter(); - if let Some(first) = iter.next() { - range_to_css(first, dest)?; - for item in iter { - dest.write_str(", ")?; - range_to_css(item, dest)?; - } - Ok(()) - } else { - dest.write_str("auto") - } - } -} - -fn range_to_css(range: &Range, dest: &mut CssWriter) -> fmt::Result -where - W: Write, -{ - range.start.to_css(dest)?; - dest.write_char(' ')?; - range.end.to_css(dest) -} - /// #[derive(Clone, Debug, ToCss, ToShmem)] pub struct Pad(pub Integer, pub Symbol); @@ -572,14 +555,13 @@ impl Parse for Fallback { _context: &ParserContext, input: &mut Parser<'i, 't>, ) -> Result> { - parse_counter_style_name(input).map(Fallback) + Ok(Fallback(parse_counter_style_name(input)?)) } } /// -#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] -#[derive(Clone, Debug, Eq, PartialEq, ToComputedValue, ToCss, ToShmem)] -pub struct Symbols(#[css(iterable)] pub Vec); +#[derive(Clone, Debug, Eq, PartialEq, MallocSizeOf, ToComputedValue, ToCss, ToShmem)] +pub struct Symbols(#[css(iterable)] pub crate::OwnedSlice); impl Parse for Symbols { fn parse<'i, 't>( @@ -587,23 +569,20 @@ impl Parse for Symbols { input: &mut Parser<'i, 't>, ) -> Result> { let mut symbols = Vec::new(); - loop { - if let Ok(s) = input.try(|input| Symbol::parse(context, input)) { - symbols.push(s) - } else { - if symbols.is_empty() { - return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)); - } else { - return Ok(Symbols(symbols)); - } - } + while let Ok(s) = input.try(|input| Symbol::parse(context, input)) { + symbols.push(s); } + if symbols.is_empty() { + return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)); + } + Ok(Symbols(symbols.into())) } } /// #[derive(Clone, Debug, ToCss, ToShmem)] -pub struct AdditiveSymbols(pub Vec); +#[css(comma)] +pub struct AdditiveSymbols(#[css(iterable)] pub crate::OwnedSlice); impl Parse for AdditiveSymbols { fn parse<'i, 't>( @@ -618,7 +597,7 @@ impl Parse for AdditiveSymbols { { return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)); } - Ok(AdditiveSymbols(tuples)) + Ok(AdditiveSymbols(tuples.into())) } } @@ -643,10 +622,7 @@ impl Parse for AdditiveTuple { let symbol = input.try(|input| Symbol::parse(context, input)); let weight = Integer::parse_non_negative(context, input)?; let symbol = symbol.or_else(|_| Symbol::parse(context, input))?; - Ok(AdditiveTuple { - weight: weight, - symbol: symbol, - }) + Ok(Self { weight, symbol }) } } diff --git a/components/style/gecko/values.rs b/components/style/gecko/values.rs index 98fe90fe3d0..8f777b9f782 100644 --- a/components/style/gecko/values.rs +++ b/components/style/gecko/values.rs @@ -288,7 +288,7 @@ impl CounterStyleOrNone { .0 .iter() .map(|symbol| match *symbol { - Symbol::String(ref s) => nsCStr::from(s), + Symbol::String(ref s) => nsCStr::from(&**s), Symbol::Ident(_) => unreachable!("Should not have identifier in symbols()"), }) .collect(); @@ -333,7 +333,7 @@ impl CounterStyleOrNone { let symbol_type = SymbolsType::from_gecko_keyword(anonymous.mSystem as u32); let symbols = symbols .iter() - .map(|gecko_symbol| Symbol::String(gecko_symbol.to_string())) + .map(|gecko_symbol| Symbol::String(gecko_symbol.to_string().into())) .collect(); Either::First(CounterStyleOrNone::Symbols(symbol_type, Symbols(symbols))) }