Coverage Report

Created: 2026-10-28 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/cssparser-0.37.0/src/nth.rs
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
use super::{BasicParseError, Parser, ParserInput, Token};
6
7
/// Parse the *An+B* notation, as found in the `:nth-child()` selector.
8
/// The input is typically the arguments of a function,
9
/// in which case the caller needs to check if the arguments’ parser is exhausted.
10
/// Return `Ok((A, B))`, or an `Err(..)` for a syntax error.
11
5
pub fn parse_nth<'i>(input: &mut Parser<'i, '_>) -> Result<(i32, i32), BasicParseError<'i>> {
12
5
    match *input.next()? {
13
        Token::Number {
14
0
            int_value: Some(b), ..
15
0
        } => Ok((0, b)),
16
        Token::Dimension {
17
0
            int_value: Some(a),
18
0
            ref unit,
19
            ..
20
        } => {
21
0
            match_ignore_ascii_case! {
22
0
                unit,
23
0
                "n" => parse_b(input, a),
24
0
                "n-" => parse_signless_b(input, a, -1),
25
0
                _ => match parse_n_dash_digits(unit) {
26
0
                    Ok(b) => Ok((a, b)),
27
                    Err(()) => {
28
0
                        let unit = unit.clone();
29
0
                        Err(input.new_basic_unexpected_token_error(Token::Ident(unit)))
30
                    }
31
                }
32
            }
33
        }
34
4
        Token::Ident(ref value) => {
35
4
            match_ignore_ascii_case! { value,
36
4
                "even" => Ok((2, 0)),
37
4
                "odd" => Ok((2, 1)),
38
4
                "n" => parse_b(input, 1),
39
4
                "-n" => parse_b(input, -1),
40
4
                "n-" => parse_signless_b(input, 1, -1),
41
4
                "-n-" => parse_signless_b(input, -1, -1),
42
                _ => {
43
4
                    let (slice, a) = if let Some(stripped) = value.strip_prefix('-') {
44
1
                        (stripped, -1)
45
                    } else {
46
3
                        (&**value, 1)
47
                    };
48
4
                    match parse_n_dash_digits(slice) {
49
0
                        Ok(b) => Ok((a, b)),
50
                        Err(()) => {
51
4
                            let value = value.clone();
52
4
                            Err(input.new_basic_unexpected_token_error(Token::Ident(value)))
53
                        }
54
                    }
55
                }
56
            }
57
        }
58
0
        Token::Delim('+') => match *input.next_including_whitespace()? {
59
0
            Token::Ident(ref value) => {
60
0
                match_ignore_ascii_case! { value,
61
0
                    "n" => parse_b(input, 1),
62
0
                    "n-" => parse_signless_b(input, 1, -1),
63
0
                    _ => match parse_n_dash_digits(value) {
64
0
                        Ok(b) => Ok((1, b)),
65
                        Err(()) => {
66
0
                            let value = value.clone();
67
0
                            Err(input.new_basic_unexpected_token_error(Token::Ident(value)))
68
                        }
69
                    }
70
                }
71
            }
72
0
            ref token => {
73
0
                let token = token.clone();
74
0
                Err(input.new_basic_unexpected_token_error(token))
75
            }
76
        },
77
1
        ref token => {
78
1
            let token = token.clone();
79
1
            Err(input.new_basic_unexpected_token_error(token))
80
        }
81
    }
82
5
}
83
84
0
fn parse_b<'i>(input: &mut Parser<'i, '_>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
85
0
    let start = input.state();
86
0
    match input.next() {
87
0
        Ok(&Token::Delim('+')) => parse_signless_b(input, a, 1),
88
0
        Ok(&Token::Delim('-')) => parse_signless_b(input, a, -1),
89
        Ok(&Token::Number {
90
            has_sign: true,
91
0
            int_value: Some(b),
92
            ..
93
0
        }) => Ok((a, b)),
94
        _ => {
95
0
            input.reset(&start);
96
0
            Ok((a, 0))
97
        }
98
    }
99
0
}
100
101
0
fn parse_signless_b<'i>(
102
0
    input: &mut Parser<'i, '_>,
103
0
    a: i32,
104
0
    b_sign: i32,
105
0
) -> Result<(i32, i32), BasicParseError<'i>> {
106
    // FIXME: remove .clone() when lifetimes are non-lexical.
107
0
    match input.next()?.clone() {
108
        Token::Number {
109
            has_sign: false,
110
0
            int_value: Some(b),
111
            ..
112
0
        } => Ok((a, b_sign * b)),
113
0
        token => Err(input.new_basic_unexpected_token_error(token)),
114
    }
115
0
}
116
117
4
fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
118
4
    let bytes = string.as_bytes();
119
4
    if bytes.len() >= 3
120
2
        && bytes[..2].eq_ignore_ascii_case(b"n-")
121
0
        && bytes[2..].iter().all(|&c| c.is_ascii_digit())
122
    {
123
0
        Ok(parse_number_saturate(&string[1..]).unwrap()) // Include the minus sign
124
    } else {
125
4
        Err(())
126
    }
127
4
}
128
129
0
fn parse_number_saturate(string: &str) -> Result<i32, ()> {
130
0
    let mut input = ParserInput::new(string);
131
0
    let mut parser = Parser::new(&mut input);
132
0
    let int = if let Ok(&Token::Number {
133
0
        int_value: Some(int),
134
        ..
135
0
    }) = parser.next_including_whitespace_and_comments()
136
    {
137
0
        int
138
    } else {
139
0
        return Err(());
140
    };
141
0
    if !parser.is_exhausted() {
142
0
        return Err(());
143
0
    }
144
0
    Ok(int)
145
0
}