Skip to content

Commit

Permalink
Fix #912 multibyte character breaks message (#914)
Browse files Browse the repository at this point in the history
* Fix #912 multibyte character breaks message

* Add change file

* Fix clippy
  • Loading branch information
lancetarn authored Aug 1, 2020
1 parent cc67680 commit df70ca5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/912-unicode-messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---
Adjust payload formatting to handle multibyte characters in front-end message
payloads.
21 changes: 19 additions & 2 deletions tauri/src/app/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit df70ca5

Please sign in to comment.