From df70ca51965665952a74161cc6eb1ff19eae45e2 Mon Sep 17 00:00:00 2001 From: Lance Erickson Date: Sat, 1 Aug 2020 18:34:50 -0500 Subject: [PATCH] Fix #912 multibyte character breaks message (#914) * Fix #912 multibyte character breaks message * Add change file * Fix clippy --- .changes/912-unicode-messages.md | 5 +++++ tauri/src/app/runner.rs | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changes/912-unicode-messages.md diff --git a/.changes/912-unicode-messages.md b/.changes/912-unicode-messages.md new file mode 100644 index 000000000..06ad25465 --- /dev/null +++ b/.changes/912-unicode-messages.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- +Adjust payload formatting to handle multibyte characters in front-end message +payloads. diff --git a/tauri/src/app/runner.rs b/tauri/src/app/runner.rs index 6bf66e26c..a1235009a 100644 --- a/tauri/src/app/runner.rs +++ b/tauri/src/app/runner.rs @@ -302,8 +302,7 @@ fn build_webview( let mut w = webview.clone(); webview.bind("__TAURI_INVOKE_HANDLER__", move |_, arg| { - // transform `[payload]` to `payload` - let arg = arg.chars().skip(1).take(arg.len() - 2).collect::(); + let arg = format_arg(arg); if arg == r#"{"cmd":"__initialized"}"# { let source = if has_splashscreen && !initialized_splashscreen { initialized_splashscreen = true; @@ -379,6 +378,15 @@ fn get_api_error_message(arg: &str, handler_error_message: String) -> String { ) } +// Transform `[payload]` to `payload` +fn format_arg(arg: &str) -> String { + arg + .chars() + .skip(1) + .take(arg.chars().count() - 2) + .collect::() +} + #[cfg(test)] mod test { use super::Content; @@ -477,4 +485,13 @@ mod test { } } } + + #[test] + fn test_format_arg() { + let input = &["[payload]", "[påyløad]"]; + let expected = &[String::from("payload"), String::from("påyløad")]; + for (i, e) in input.iter().zip(expected) { + assert_eq!(&super::format_arg(i), e); + } + } }