From 489689b477d1126f13ab1f83a3494e87bed96e86 Mon Sep 17 00:00:00 2001 From: Joshua Hull Date: Sun, 16 Apr 2023 00:19:40 +0000 Subject: [PATCH] Use json.Marshal instead of strconv.Quote to correctly support unicode (#2509) * Use json.Marshal instead of strconv.Quote to correctly support unicode * Add fix to changelog --------- Co-authored-by: Lea Anthony --- v2/internal/frontend/desktop/darwin/frontend.go | 7 +++++-- v2/internal/frontend/desktop/linux/frontend.go | 7 +++++-- v2/internal/frontend/desktop/windows/frontend.go | 7 +++++-- website/src/pages/changelog.mdx | 1 + 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index 7295e0ee6..a5517ffcb 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -20,7 +20,6 @@ import ( "html/template" "log" "net/url" - "strconv" "unsafe" "github.com/wailsapp/wails/v2/pkg/assetserver" @@ -337,7 +336,11 @@ func (f *Frontend) processMessage(message string) { } func (f *Frontend) Callback(message string) { - f.ExecJS(`window.wails.Callback(` + strconv.Quote(message) + `);`) + escaped, err := json.Marshal(message) + if err != nil { + panic(err) + } + f.ExecJS(`window.wails.Callback(` + string(escaped) + `);`) } func (f *Frontend) ExecJS(js string) { diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 072940d3a..f8c83eac1 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -81,7 +81,6 @@ import ( "net/url" "os" "runtime" - "strconv" "strings" "text/template" "unsafe" @@ -434,7 +433,11 @@ func (f *Frontend) processMessage(message string) { } func (f *Frontend) Callback(message string) { - f.ExecJS(`window.wails.Callback(` + strconv.Quote(message) + `);`) + escaped, err := json.Marshal(message) + if err != nil { + panic(err) + } + f.ExecJS(`window.wails.Callback(` + string(escaped) + `);`) } func (f *Frontend) startDrag() { diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index bf1c7d7c5..ed77c204c 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -13,7 +13,6 @@ import ( "net/http/httptest" "net/url" "runtime" - "strconv" "strings" "sync" "text/template" @@ -637,8 +636,12 @@ func (f *Frontend) processMessage(message string) { } func (f *Frontend) Callback(message string) { + escaped, err := json.Marshal(message) + if err != nil { + panic(err) + } f.mainWindow.Invoke(func() { - f.chromium.Eval(`window.wails.Callback(` + strconv.Quote(message) + `);`) + f.chromium.Eval(`window.wails.Callback(` + string(escaped) + `);`) }) } diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index b8a5214de..54f5344fe 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed console printing in `wails generate template`. Fixed by @misitebao in [PR](https://github.com/wailsapp/wails/pull/2483) +- Fixed unicode encoding of strings for multi-rune characters. Fixed by @joshbuddy in [PR](https://github.com/wailsapp/wails/pull/2509) - Fixed `-skipbindings` flag in `wails dev`. Fixed by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2584) - Fixed `runtime.MenuUpdateApplicationMenu` on macOS. Fixed by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2588) - Fixed add package name for `libwebkit`/`pkg-config` and use shell.RunCommandWithENV instead of shell.RunCommand in `zypper.go`. Fixed by @wgjtyu in [PR](https://github.com/wailsapp/wails/pull/2593)