1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Parser for the `stroke-dasharray` property.

use cssparser::Parser;

use crate::error::*;
use crate::length::*;
use crate::parsers::{optional_comma, Parse};

#[derive(Debug, Default, PartialEq, Clone)]
pub enum Dasharray {
    #[default]
    None,
    Array(Box<[ULength<Both>]>),
}

impl Parse for Dasharray {
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Dasharray, ParseError<'i>> {
        if parser
            .try_parse(|p| p.expect_ident_matching("none"))
            .is_ok()
        {
            return Ok(Dasharray::None);
        }

        let mut dasharray = Vec::new();

        loop {
            let d = ULength::<Both>::parse(parser)?;
            dasharray.push(d);

            if parser.is_exhausted() {
                break;
            }

            optional_comma(parser);
        }

        Ok(Dasharray::Array(dasharray.into_boxed_slice()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dasharray(l: &[ULength<Both>]) -> Dasharray {
        Dasharray::Array(l.to_vec().into_boxed_slice())
    }

    #[test]
    fn parses_dash_array() {
        // helper to cut down boilderplate
        let length_parse = |s| ULength::<Both>::parse_str(s).unwrap();

        let expected = dasharray(&[
            length_parse("1"),
            length_parse("2in"),
            length_parse("3"),
            length_parse("4%"),
        ]);

        let sample_1 = dasharray(&[length_parse("10"), length_parse("6")]);

        let sample_2 = dasharray(&[length_parse("5"), length_parse("5"), length_parse("20")]);

        let sample_3 = dasharray(&[
            length_parse("10px"),
            length_parse("20px"),
            length_parse("20px"),
        ]);

        let sample_4 = dasharray(&[
            length_parse("25"),
            length_parse("5"),
            length_parse("5"),
            length_parse("5"),
        ]);

        let sample_5 = dasharray(&[length_parse("3.1415926"), length_parse("8")]);
        let sample_6 = dasharray(&[length_parse("5"), length_parse("3.14")]);
        let sample_7 = dasharray(&[length_parse("2")]);

        assert_eq!(Dasharray::parse_str("none").unwrap(), Dasharray::None);
        assert_eq!(Dasharray::parse_str("1 2in,3 4%").unwrap(), expected);
        assert_eq!(Dasharray::parse_str("10,6").unwrap(), sample_1);
        assert_eq!(Dasharray::parse_str("5,5,20").unwrap(), sample_2);
        assert_eq!(Dasharray::parse_str("10px 20px 20px").unwrap(), sample_3);
        assert_eq!(Dasharray::parse_str("25  5 , 5 5").unwrap(), sample_4);
        assert_eq!(Dasharray::parse_str("3.1415926,8").unwrap(), sample_5);
        assert_eq!(Dasharray::parse_str("5, 3.14").unwrap(), sample_6);
        assert_eq!(Dasharray::parse_str("2").unwrap(), sample_7);

        // Negative numbers
        assert!(Dasharray::parse_str("20,40,-20").is_err());

        // Empty dash_array
        assert!(Dasharray::parse_str("").is_err());
        assert!(Dasharray::parse_str("\t  \n     ").is_err());
        assert!(Dasharray::parse_str(",,,").is_err());
        assert!(Dasharray::parse_str("10,  \t, 20 \n").is_err());

        // No trailing commas allowed, parse error
        assert!(Dasharray::parse_str("10,").is_err());

        // A comma should be followed by a number
        assert!(Dasharray::parse_str("20,,10").is_err());
    }
}