Fix #912 multibyte character breaks message (#914)

* Fix #912 multibyte character breaks message

* Add change file

* Fix clippy
This commit is contained in:
Lance Erickson
2020-08-01 18:34:50 -05:00
committed by GitHub
parent cc67680fca
commit df70ca5196
2 changed files with 24 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Adjust payload formatting to handle multibyte characters in front-end message
payloads.

View File

@@ -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::<String>();
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::<String>()
}
#[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);
}
}
}