diff --git a/src/utils.rs b/src/utils.rs index db182a99..3a48628e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -341,7 +341,11 @@ impl Style { "reverse" => rv.reverse(), "hidden" => rv.hidden(), "strikethrough" => rv.strikethrough(), - on_true_color if on_true_color.starts_with("on_#") && on_true_color.len() == 10 => { + on_true_color + if on_true_color.starts_with("on_#") + && on_true_color.len() == 10 + && on_true_color.is_ascii() => + { if let (Ok(r), Ok(g), Ok(b)) = ( u8::from_str_radix(&on_true_color[4..6], 16), u8::from_str_radix(&on_true_color[6..8], 16), @@ -352,7 +356,11 @@ impl Style { continue; } } - true_color if true_color.starts_with('#') && true_color.len() == 7 => { + true_color + if true_color.starts_with('#') + && true_color.len() == 7 + && true_color.is_ascii() => + { if let (Ok(r), Ok(g), Ok(b)) = ( u8::from_str_radix(&true_color[1..3], 16), u8::from_str_radix(&true_color[3..5], 16), @@ -1195,3 +1203,27 @@ fn test_attributes_many() { assert_eq!(&attrs.attrs().collect::>(), test_attrs); } } + +#[test] +fn test_style_from_non_ascii_fg() { + // len() == 7, starts_with('#'), but slices [1..3] land mid-€ (3 bytes) + let fg = "#€€"; + assert_eq!(fg.len(), 7); + + let parsed_style = Style::from_dotted_str(fg); + + // silently ignores non-ascii + assert_eq!(parsed_style, Style::default()); +} + +#[test] +fn test_style_from_non_ascii_bg() { + // len() == 10, starts_with("on_#"), but slices [4..6] land mid-€ + let bg = "on_#€€"; + assert_eq!(bg.len(), 10); + + let parsed_style = Style::from_dotted_str(bg); + + // silently ignores non-ascii + assert_eq!(parsed_style, Style::default()); +}