Properly escapes ' and \ as popup keys.

This commit is contained in:
Joshua A. Horton 2018-04-25 12:46:45 +07:00
parent 0213247260
commit e53bf7fcaf
2 changed files with 16 additions and 1 deletions

View file

@ -12,6 +12,7 @@ display_usage ( ) {
echo " -clean Removes all previously-existing build products for KMEI and the Keyman app before building."
echo " -no-kmw Uses existing keyman.js, doesn't try to build"
echo " -only-framework Builds only the KeymanEngine framework; does not attempt to build the app."
echo " -no-carthage Disables downloading and building for dependencies."
echo " -no-codesign Disables code-signing for the Keyman application, allowing it to be performed separately later."
echo " Will not construct the archive and .ipa. (includes -no-archive)"
echo " -no-archive Bypasses the archive and .ipa preparation stage."

View file

@ -78,7 +78,21 @@ extension KeymanWebViewController {
// FIXME: text is unused in the JS
func executePopupKey(id: String, text: String) {
webView.evaluateJavaScript("executePopupKey('\(id)','\(text)');", completionHandler: nil)
//var escapedText: String;
// Text must be checked for ', ", and \ characters; they must be escaped properly!
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 escapedText = jsonString[start..<end]
let cmd = "executePopupKey(\"\(id)\",\"\(escapedText)\");"
webView.evaluateJavaScript(cmd, completionHandler: nil)
} catch {
log.error(error)
return
}
}
func setOskWidth(_ width: Int) {