Skip to content

Commit

Permalink
Do not panic when trying to parse a color that is not ASCII.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Minh Kaplan authored and lilith committed Oct 5, 2023
1 parent 7b1a4f4 commit 41fd5f2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions imageflow_helpers/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ fn parse_rgba_slices(r: &str, g: &str, b: &str, a :&str) -> Result<Color32,std::
///
/// Parses #RRGGBBAA #RRGGBB #RGB #RGBA - with and without leading #, case insensitive
pub fn parse_color_hex(value: &str) -> std::result::Result<Color32, ParseColorError> {
if ! value.is_ascii() {
return Err(ParseColorError::FormatIncorrect("CSS colors must be in ASCII only"));
}
let value = match &value[0..1] {
"#" => &value[1..],
_ => value
Expand Down
3 changes: 2 additions & 1 deletion imageflow_riapi/src/ir4/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl Instructions{
i.cropyunits = p.parse_f64("cropyunits");
i.quality = p.parse_i32("quality").or_else(||p.parse_i32("jpeg.quality"));
i.zoom = p.parse_f64("zoom").or_else(|| p.parse_f64("dpr"));
i.bgcolor_srgb = p.parse_color_srgb("bgcolor").or_else(||p.parse_color_srgb("bgcolor"));
i.bgcolor_srgb = p.parse_color_srgb("bgcolor");
i.jpeg_subsampling = p.parse_subsampling("subsampling");

i.webp_quality = p.parse_f64("webp.quality");
Expand Down Expand Up @@ -1098,6 +1098,7 @@ fn test_url_parsing() {
t("s.grayscale=Y", Instructions{s_grayscale: Some(GrayscaleAlgorithm::Y), ..Default::default()}, vec![]);
t("s.grayscale=Bt709", Instructions{s_grayscale: Some(GrayscaleAlgorithm::Bt709), ..Default::default()}, vec![]);

t("bgcolor=é", Default::default(), vec![ParseWarning::ValueInvalid(("bgcolor".into(), "é".into())), ParseWarning::KeyNotSupported(("bgcolor".into(), "é".into()))]);
t("bgcolor=red", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]);
t("bgcolor=f00", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]);
t("bgcolor=f00f", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]);
Expand Down

0 comments on commit 41fd5f2

Please sign in to comment.