瀏覽代碼

Fix #912 multibyte character breaks message (#914)

* Fix #912 multibyte character breaks message

* Add change file

* Fix clippy
Lance Erickson 5 年之前
父節點
當前提交
df70ca5196
共有 2 個文件被更改,包括 24 次插入2 次删除
  1. 5 0
      .changes/912-unicode-messages.md
  2. 19 2
      tauri/src/app/runner.rs

+ 5 - 0
.changes/912-unicode-messages.md

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

+ 19 - 2
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::<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);
+    }
+  }
 }