From 07332bbdc0610a58b7a199d827769d8e5e6aeeda Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 18 Sep 2017 16:03:35 -0700 Subject: [PATCH] Saturate if nth-child value is out of range --- Cargo.toml | 2 +- src/nth.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6be42a3..1cbb2e73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssparser" -version = "0.21.1" +version = "0.21.2" authors = [ "Simon Sapin " ] description = "Rust implementation of CSS Syntax Level 3" diff --git a/src/nth.rs b/src/nth.rs index 8ee623c8..ef0adbcd 100644 --- a/src/nth.rs +++ b/src/nth.rs @@ -4,7 +4,7 @@ use std::ascii::AsciiExt; -use super::{Token, Parser, BasicParseError}; +use super::{Token, Parser, ParserInput, BasicParseError}; /// Parse the *An+B* notation, as found in the `:nth-child()` selector. @@ -93,8 +93,23 @@ fn parse_n_dash_digits(string: &str) -> Result { && string[..2].eq_ignore_ascii_case("n-") && string[2..].chars().all(|c| matches!(c, '0'...'9')) { - Ok(string[1..].parse().unwrap()) // Include the minus sign + Ok(parse_number_saturate(&string[1..]).unwrap()) // Include the minus sign } else { Err(()) } } + +fn parse_number_saturate(string: &str) -> Result { + let mut input = ParserInput::new(string); + let mut parser = Parser::new(&mut input); + let int = if let Ok(&Token::Number {int_value: Some(int), ..}) + = parser.next_including_whitespace_and_comments() { + int + } else { + return Err(()) + }; + if !parser.is_exhausted() { + return Err(()) + } + Ok(int) +}