Skip to content

Saturate if nth-child value is out of range #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.21.1"
version = "0.21.2"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
19 changes: 17 additions & 2 deletions src/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -93,8 +93,23 @@ fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
&& 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<i32, ()> {
let mut input = ParserInput::new(string);
let mut parser = Parser::new(&mut input);
let int = if let Ok(&Token::Number {int_value: Some(int), ..})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure here feels a little weird. How about:

  let next = parser.next_including_whitespace_and_comments();
  if let Ok(&Token::Number {int_value: Some(int), ..}) = next {
    if !parser.is_exhausted() {
      return Ok(int);
    }
  }
  Err(())

unless you're keen on having the Ok return at the end of the function. Either way, probably nicer to have a separate let next = ....

= parser.next_including_whitespace_and_comments() {
int
} else {
return Err(())
};
if !parser.is_exhausted() {
return Err(())
}
Ok(int)
}