From 4b0fe061962bcbfb4702481044921be2f408016b Mon Sep 17 00:00:00 2001 From: jahorton Date: Mon, 21 Jun 2021 16:04:54 +0700 Subject: [PATCH] change(ios): setText now uses JSON serialization --- .../Keyboard/KeymanWebViewController.swift | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Keyboard/KeymanWebViewController.swift b/ios/engine/KMEI/KeymanEngine/Classes/Keyboard/KeymanWebViewController.swift index f794907aca..94d6f8472c 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Keyboard/KeymanWebViewController.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Keyboard/KeymanWebViewController.swift @@ -217,18 +217,24 @@ extension KeymanWebViewController { func setText(_ text: String?) { var text = text ?? "" + // Remove any system-added LTR/RTL marks. text = text.replacingOccurrences(of: "\u{200e}", with: "") // Unicode's LTR codepoint text = text.replacingOccurrences(of: "\u{200f}", with: "") // Unicode's RTL codepoint (v1) text = text.replacingOccurrences(of: "\u{202e}", with: "") // Unicode's RTL codepoint (v2) - // JavaScript escape-sequence encodings. - text = text.replacingOccurrences(of: "\\", with: "\\\\") - text = text.replacingOccurrences(of: "'", with: "\\'") - text = text.replacingOccurrences(of: "\n", with: "\\n") + do { + let encodingArray = [ text ]; + let jsonString = try String(data: JSONSerialization.data(withJSONObject: encodingArray), encoding: .utf8)! + let start = jsonString.index(jsonString.startIndex, offsetBy: 2) + let end = jsonString.index(jsonString.endIndex, offsetBy: -2) + let jsonText = jsonString[start..