lewp_css/domain/at_rules/counter_style/
additive_symbols.rs

1use {
2    super::AdditiveTuple,
3    crate::{
4        parsers::{Parse, ParserContext},
5        CustomParseError,
6    },
7    cssparser::{ParseError, Parser, ToCss},
8};
9
10// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
11// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
12
13/// <https://drafts.csswg.org/css-counter-styles/#descdef-counter-style-additive-symbols>
14#[derive(Clone, Debug)]
15pub struct AdditiveSymbols(pub Vec<AdditiveTuple>);
16
17impl ToCss for AdditiveSymbols {
18    fn to_css<W: std::fmt::Write>(&self, dest: &mut W) -> std::fmt::Result {
19        let mut iter = self.0.iter();
20        let first = iter.next().unwrap();
21        first.to_css(dest)?;
22        for item in iter {
23            dest.write_char(',')?;
24            item.to_css(dest)?;
25        }
26        Ok(())
27    }
28}
29
30impl Parse for AdditiveSymbols {
31    fn parse<'i, 't>(
32        context: &ParserContext,
33        input: &mut Parser<'i, 't>,
34    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
35        let tuples = Vec::<AdditiveTuple>::parse(context, input)?;
36        // FIXME maybe? https://github.com/w3c/csswg-drafts/issues/1220
37        if tuples
38            .windows(2)
39            .any(|window| window[0].weight <= window[1].weight)
40        {
41            return Err(ParseError::from(CustomParseError::CounterStyleAdditiveSymbolsCanNotHaveASecondWeightEqualToOrGreaterThanTheFirst));
42        }
43        Ok(AdditiveSymbols(tuples))
44    }
45}