Skip to content

Commit

Permalink
Merge pull request #7 from 5225225/fix-parse-error
Browse files Browse the repository at this point in the history
Fix error when parsing hex value with non-ASCII
  • Loading branch information
mazznoer authored May 14, 2022
2 parents a0e0671 + bca33b5 commit 7ac8b28
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ pub fn parse(s: &str) -> Result<Color, ParseColorError> {
}

fn parse_hex(s: &str) -> Result<Color, Box<dyn error::Error>> {
if !s.is_ascii() {
return Err(Box::new(ParseColorError::InvalidHex));
}

let n = s.len();

let (r, g, b, a) = if n == 3 || n == 4 {
Expand Down
6 changes: 5 additions & 1 deletion tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn lime_alpha() {
}
}

#[cfg(all(feature = "named-colors", features = "lab"))]
#[cfg(all(feature = "named-colors", feature = "lab"))]
#[test]
fn invalid_format() {
let test_data = vec![
Expand Down Expand Up @@ -240,6 +240,10 @@ fn invalid_format() {
("cmyk(0,0,0,0)", "Invalid color function."),
("blood", "Invalid unknown format."),
("rgb(255,0,0", "Invalid unknown format."),
("x£", "Invalid unknown format."),
("x£x", "Invalid unknown format."),
("xxx£x", "Invalid unknown format."),
("xxxxx£x", "Invalid unknown format."),
];

for (s, err_msg) in test_data {
Expand Down

0 comments on commit 7ac8b28

Please sign in to comment.