Thibault Malbranche 5 年前
父节点
当前提交
5017ba3a39
共有 4 个文件被更改,包括 17 次插入68 次删除
  1. 7
    11
      src/WebView.android.tsx
  2. 1
    6
      src/WebView.ios.tsx
  3. 8
    41
      src/WebViewShared.ts
  4. 1
    10
      src/WebViewTypes.ts

+ 7
- 11
src/WebView.android.tsx 查看文件

@@ -14,7 +14,6 @@ import {
14 14
 import invariant from 'invariant';
15 15
 
16 16
 import {
17
-  defaultOriginWhitelist,
18 17
   createOnShouldStartLoadWithRequest,
19 18
   getViewManagerConfig,
20 19
 } from './WebViewShared';
@@ -57,7 +56,6 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
57 56
     saveFormDataDisabled: false,
58 57
     cacheEnabled: true,
59 58
     androidHardwareAccelerationDisabled: false,
60
-    originWhitelist: defaultOriginWhitelist,
61 59
   };
62 60
 
63 61
   static isFileUploadSupported = async () => {
@@ -207,20 +205,20 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
207 205
     shouldStart: boolean,
208 206
     url: string,
209 207
   ) => {
210
-    if (shouldStart) {
211
-      UIManager.dispatchViewManagerCommand(
212
-        this.getWebViewHandle(),
213
-        this.getCommands().loadUrl,
214
-        [String(url)],
215
-      );
208
+    if (!shouldStart) {
209
+      return;
216 210
     }
211
+    UIManager.dispatchViewManagerCommand(
212
+      this.getWebViewHandle(),
213
+      this.getCommands().loadUrl,
214
+      [String(url)],
215
+    );
217 216
   };
218 217
 
219 218
   render() {
220 219
     const {
221 220
       onMessage,
222 221
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
223
-      originWhitelist,
224 222
       renderError,
225 223
       renderLoading,
226 224
       source,
@@ -268,8 +266,6 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
268 266
 
269 267
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
270 268
       this.onShouldStartLoadWithRequestCallback,
271
-      // casting cause it's in the default props
272
-      originWhitelist as ReadonlyArray<string>,
273 269
       onShouldStartLoadWithRequestProp,
274 270
     );
275 271
 

+ 1
- 6
src/WebView.ios.tsx 查看文件

@@ -13,7 +13,6 @@ import {
13 13
 import invariant from 'invariant';
14 14
 
15 15
 import {
16
-  defaultOriginWhitelist,
17 16
   createOnShouldStartLoadWithRequest,
18 17
   getViewManagerConfig,
19 18
 } from './WebViewShared';
@@ -82,7 +81,6 @@ class WebView extends React.Component<IOSWebViewProps, State> {
82 81
   static defaultProps = {
83 82
     useWebKit: true,
84 83
     cacheEnabled: true,
85
-    originWhitelist: defaultOriginWhitelist,
86 84
     useSharedProcessPool: true,
87 85
   };
88 86
 
@@ -299,7 +297,7 @@ class WebView extends React.Component<IOSWebViewProps, State> {
299 297
       viewManager = viewManager || RNCUIWebViewManager;
300 298
     }
301 299
     invariant(viewManager != null, 'viewManager expected to be non-null');
302
-    viewManager.startLoadWithResult(!!shouldStart, lockIdentifier);
300
+    viewManager.startLoadWithResult(shouldStart, lockIdentifier);
303 301
   };
304 302
 
305 303
   componentDidUpdate(prevProps: IOSWebViewProps) {
@@ -336,7 +334,6 @@ class WebView extends React.Component<IOSWebViewProps, State> {
336 334
       nativeConfig = {},
337 335
       onMessage,
338 336
       onShouldStartLoadWithRequest: onShouldStartLoadWithRequestProp,
339
-      originWhitelist,
340 337
       renderError,
341 338
       renderLoading,
342 339
       scalesPageToFit = this.props.useWebKit ? undefined : true,
@@ -374,8 +371,6 @@ class WebView extends React.Component<IOSWebViewProps, State> {
374 371
 
375 372
     const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
376 373
       this.onShouldStartLoadWithRequestCallback,
377
-      // casting cause it's in the default props
378
-      originWhitelist as ReadonlyArray<string>,
379 374
       onShouldStartLoadWithRequestProp,
380 375
     );
381 376
 

+ 8
- 41
src/WebViewShared.ts 查看文件

@@ -1,5 +1,4 @@
1
-import escapeStringRegexp from 'escape-string-regexp';
2
-import { Linking, UIManager as NotTypedUIManager } from 'react-native';
1
+import { UIManager as NotTypedUIManager } from 'react-native';
3 2
 import {
4 3
   WebViewNavigationEvent,
5 4
   OnShouldStartLoadWithRequest,
@@ -8,52 +7,24 @@ import {
8 7
 
9 8
 const UIManager = NotTypedUIManager as CustomUIManager;
10 9
 
11
-const defaultOriginWhitelist = ['http://*', 'https://*'];
12
-
13
-const extractOrigin = (url: string): string => {
14
-  const result = /^[A-Za-z][A-Za-z0-9+\-.]+:(\/\/)?[^/]*/.exec(url);
15
-  return result === null ? '' : result[0];
16
-};
17
-
18
-const originWhitelistToRegex = (originWhitelist: string): string =>
19
-  `^${escapeStringRegexp(originWhitelist).replace(/\\\*/g, '.*')}`;
20
-
21
-const passesWhitelist = (
22
-  compiledWhitelist: ReadonlyArray<string>,
23
-  url: string,
24
-) => {
25
-  const origin = extractOrigin(url);
26
-  return compiledWhitelist.some(x => new RegExp(x).test(origin));
27
-};
28
-
29
-const compileWhitelist = (
30
-  originWhitelist: ReadonlyArray<string>,
31
-): ReadonlyArray<string> =>
32
-  ['about:blank', ...(originWhitelist || [])].map(originWhitelistToRegex);
33
-
34 10
 const createOnShouldStartLoadWithRequest = (
35 11
   loadRequest: (
36 12
     shouldStart: boolean,
37 13
     url: string,
38 14
     lockIdentifier: number,
39 15
   ) => void,
40
-  originWhitelist: ReadonlyArray<string>,
41 16
   onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest,
42 17
 ) => {
43 18
   return ({ nativeEvent }: WebViewNavigationEvent) => {
44
-    let shouldStart = true;
45 19
     const { url, lockIdentifier } = nativeEvent;
46 20
 
47
-    if (!passesWhitelist(compileWhitelist(originWhitelist), url)) {
48
-      Linking.openURL(url);
49
-      shouldStart = false;
50
-    }
51
-
52
-    if (onShouldStartLoadWithRequest) {
53
-      shouldStart = onShouldStartLoadWithRequest(nativeEvent);
21
+    if (
22
+      onShouldStartLoadWithRequest
23
+      && !onShouldStartLoadWithRequest(nativeEvent)
24
+    ) {
25
+      loadRequest(false, url, lockIdentifier);
54 26
     }
55
-
56
-    loadRequest(shouldStart, url, lockIdentifier);
27
+    loadRequest(true, url, lockIdentifier);
57 28
   };
58 29
 };
59 30
 
@@ -66,8 +37,4 @@ const getViewManagerConfig = (
66 37
   return UIManager.getViewManagerConfig(viewManagerName);
67 38
 };
68 39
 
69
-export {
70
-  defaultOriginWhitelist,
71
-  createOnShouldStartLoadWithRequest,
72
-  getViewManagerConfig,
73
-};
40
+export { createOnShouldStartLoadWithRequest, getViewManagerConfig };

+ 1
- 10
src/WebViewTypes.ts 查看文件

@@ -182,7 +182,7 @@ export interface WebViewSourceHtml {
182 182
 export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
183 183
 
184 184
 export interface ViewManager {
185
-  startLoadWithResult: Function;
185
+  startLoadWithResult: (shouldStart: boolean, lockIdentifier: number) => void;
186 186
 }
187 187
 
188 188
 export interface WebViewNativeConfig {
@@ -591,15 +591,6 @@ export interface WebViewSharedProps extends ViewProps {
591 591
    */
592 592
   mediaPlaybackRequiresUserAction?: boolean;
593 593
 
594
-  /**
595
-   * List of origin strings to allow being navigated to. The strings allow
596
-   * wildcards and get matched against *just* the origin (not the full URL).
597
-   * If the user taps to navigate to a new page but the new page is not in
598
-   * this whitelist, we will open the URL in Safari.
599
-   * The default whitelisted origins are "http://*" and "https://*".
600
-   */
601
-  originWhitelist?: ReadonlyArray<string>;
602
-
603 594
   /**
604 595
    * Function that allows custom handling of any web view requests. Return
605 596
    * `true` from the function to continue loading the request and `false`