Skip to content

Commit a8edcd9

Browse files
author
bors-servo
authored
Auto merge of servo#198 - Manishearth:nth-child-saturate, r=SimonSapin
Saturate if nth-child value is out of range Reusing our tokenizer because it already saturates. There is no way to reliably get the error details from `"-12034".parse()`, since the error type is opaque and the description strings may change. This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1401016 A less risky but also less correct fix is to simply return an error when `.parse()` fails. r? @SimonSapin Don't land until I can get a try run; nth-child has weird tokenization rules that we need to respect here (which is why I'm creating a new tokenizer in the first place) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/198) <!-- Reviewable:end -->
2 parents 02952ea + 07332bb commit a8edcd9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.21.1"
3+
version = "0.21.2"
44
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"

src/nth.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::ascii::AsciiExt;
66

7-
use super::{Token, Parser, BasicParseError};
7+
use super::{Token, Parser, ParserInput, BasicParseError};
88

99

1010
/// Parse the *An+B* notation, as found in the `:nth-child()` selector.
@@ -93,8 +93,23 @@ fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
9393
&& string[..2].eq_ignore_ascii_case("n-")
9494
&& string[2..].chars().all(|c| matches!(c, '0'...'9'))
9595
{
96-
Ok(string[1..].parse().unwrap()) // Include the minus sign
96+
Ok(parse_number_saturate(&string[1..]).unwrap()) // Include the minus sign
9797
} else {
9898
Err(())
9999
}
100100
}
101+
102+
fn parse_number_saturate(string: &str) -> Result<i32, ()> {
103+
let mut input = ParserInput::new(string);
104+
let mut parser = Parser::new(&mut input);
105+
let int = if let Ok(&Token::Number {int_value: Some(int), ..})
106+
= parser.next_including_whitespace_and_comments() {
107+
int
108+
} else {
109+
return Err(())
110+
};
111+
if !parser.is_exhausted() {
112+
return Err(())
113+
}
114+
Ok(int)
115+
}

0 commit comments

Comments
 (0)