Expand description
§nth-check — parse and test the CSS An+B microsyntax
Parse the An+B microsyntax
used by CSS selectors like :nth-child(2n+1), :nth-of-type(odd), and
:nth-last-child(-n+3), then test whether a 1-based position matches. The Rust
counterpart of the nth-check npm
package. Zero dependencies and #![no_std] (no alloc either).
Parsing follows the CSS Syntax grammar strictly — the offset must carry an
explicit sign (2n+7, never 2n7), as browsers require. This is slightly
stricter than node-nth-check’s lenient regex; for every input both accept, the
coefficients and position matching are identical.
use nth_check::parse;
let odd = parse("2n+1").unwrap(); // or parse("odd")
assert!(odd.matches(1) && odd.matches(3));
assert!(!odd.matches(2));
let first_three = parse("-n+3").unwrap();
assert!(first_three.matches(1) && first_three.matches(3));
assert!(!first_three.matches(4));Positions are 1-based, matching the CSS :nth-child convention (the first
element is position 1).
Structs§
- Nth
- A parsed
An+Bexpression: the coefficients ofa·n + b. - Parse
Error - The error returned when a string is not a valid
An+Bexpression.
Functions§
- parse
- Parse an
An+Bexpression such as"2n+1","odd","even","-n+3", or"5".