Browse Source

Merge pull request #41 from react-native-community/import/20366-escape-android

Imported https://github.com/facebook/react-native/pull/20366
Thibault Malbranche 6 years ago
parent
commit
619ff5aa0a
No account linked to committer's email address

+ 21
- 4
android/src/main/java/com/reactnativecommunity/webview/RCTWebViewManager.java View File

@@ -9,6 +9,7 @@ import java.util.regex.Pattern;
9 9
 import javax.annotation.Nullable;
10 10
 
11 11
 import java.io.UnsupportedEncodingException;
12
+import java.net.URLEncoder;
12 13
 import java.util.ArrayList;
13 14
 import java.util.HashMap;
14 15
 import java.util.Locale;
@@ -318,11 +319,25 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
318 319
       }
319 320
     }
320 321
 
322
+    protected void evaluateJavascriptWithFallback(String script) {
323
+      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
324
+        evaluateJavascript(script, null);
325
+        return;
326
+      }
327
+
328
+      try {
329
+        loadUrl("javascript:" + URLEncoder.encode(script, "UTF-8"));
330
+      } catch (UnsupportedEncodingException e) {
331
+        // UTF-8 should always be supported
332
+        throw new RuntimeException(e);
333
+      }
334
+    }
335
+
321 336
     public void callInjectedJavaScript() {
322 337
       if (getSettings().getJavaScriptEnabled() &&
323 338
           injectedJS != null &&
324 339
           !TextUtils.isEmpty(injectedJS)) {
325
-        loadUrl("javascript:(function() {\n" + injectedJS + ";\n})();");
340
+        evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
326 341
       }
327 342
     }
328 343
 
@@ -341,7 +356,7 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
341 356
           });
342 357
         }
343 358
 
344
-        loadUrl("javascript:(" +
359
+        evaluateJavascriptWithFallback("(" +
345 360
           "window.originalPostMessage = window.postMessage," +
346 361
           "window.postMessage = function(data) {" +
347 362
             BRIDGE_NAME + ".postMessage(String(data));" +
@@ -630,9 +645,10 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
630 645
         break;
631 646
       case COMMAND_POST_MESSAGE:
632 647
         try {
648
+          ReactWebView reactWebView = (ReactWebView) root;
633 649
           JSONObject eventInitDict = new JSONObject();
634 650
           eventInitDict.put("data", args.getString(0));
635
-          root.loadUrl("javascript:(function () {" +
651
+          reactWebView.evaluateJavascriptWithFallback("(function () {" +
636 652
             "var event;" +
637 653
             "var data = " + eventInitDict.toString() + ";" +
638 654
             "try {" +
@@ -648,7 +664,8 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
648 664
         }
649 665
         break;
650 666
       case COMMAND_INJECT_JAVASCRIPT:
651
-        root.loadUrl("javascript:" + args.getString(0));
667
+        ReactWebView reactWebView = (ReactWebView) root;
668
+        reactWebView.evaluateJavascriptWithFallback(args.getString(0));
652 669
         break;
653 670
     }
654 671
   }