Skip to content

Commit

Permalink
fix(android/engine): URIEncode string for updateText()
Browse files Browse the repository at this point in the history
  • Loading branch information
darcywong00 committed Apr 22, 2024
1 parent 99ce1e9 commit 7d18c12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 2 additions & 4 deletions android/KMEA/app/src/main/assets/android-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,8 @@ function setNumericLayer() {
}
}

function updateKMText(text) {
if(text == undefined) {
text = '';
}
function updateKMText(k) {
var text = (k == undefined) || !k.text ? '' : k.text;

console_debug('updateKMText(text=' + text + ') with: \n' + build_context_string(keyman.context));

Expand Down
18 changes: 11 additions & 7 deletions android/KMEA/app/src/main/java/com/keyman/engine/KMKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
Expand Down Expand Up @@ -149,17 +150,19 @@ protected void setShouldIgnoreSelectionChange(boolean ignore) {

protected boolean updateText(String text) {
boolean result = false;
JSONObject reg = new JSONObject();
String kmText = "";
if (text != null) {
// Replace special literal characters to pass to Javascript
kmText = text.toString().replace("\\", "\\u005C")
.replace("%", "\\u0025")
.replace("'", "\\u0027")
.replace("\n", "\\n");
// Use JSON to handle passing string to Javascript
try {
reg.put("text", text.toString());
} catch (JSONException e) {
KMLog.LogException(TAG, "", e);
}
}

if (KMManager.isKeyboardLoaded(this.keyboardType) && !shouldIgnoreTextChange) {
this.loadJavascript(KMString.format("updateKMText('%s')", kmText));
this.loadJavascript(KMString.format("updateKMText(%s)", reg.toString()));
result = true;
}

Expand Down Expand Up @@ -348,7 +351,8 @@ public void run() {
allCalls.append(";");
}

loadUrl("javascript:" + allCalls.toString());
// Ensure strings save for Javascript. TBD: Spacebartext, font
loadUrl("javascript:" + Uri.encode(allCalls.toString()));

if(javascriptAfterLoad.size() > 0 && keyboardSet) {
callJavascriptAfterLoad();
Expand Down

0 comments on commit 7d18c12

Please sign in to comment.