Skip to content

Commit

Permalink
Fix native postMessage test
Browse files Browse the repository at this point in the history
The current way we're testing if postMessage has been changed is no
longer valid. Lodash has already changed the way they test agianst this,
as it no longer works in blink (thow continiues to work in webkit)

In chrome browser (Version 65.0.3325.181 (Official Build) (64-bit)) console type:

```js
String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')
```

It still works in safari (Version 11.0.3 (13604.5.6))

A simplified version of how lodash are testing aginst it
https://github.com/lodash/lodash/blob/4.0.1-npm-packages/lodash.isnative/index.js

With a simpler description about this method can be found here:
https://davidwalsh.name/detect-native-function

Running:

```js
RegExp('^' +
  String(Object.prototype.toString)
  .replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&')
  .replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
).test(String(window.postMessage))
```

Works correctly in both chrome and safari.

Affected issues:

facebook#10865 - some hacks were
seggested here to sidestep this issue. This PR should fix it outright
  • Loading branch information
ivank committed Apr 16, 2018
1 parent 2e38418 commit bcd0526
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion React/Views/RCTWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
if (_messagingEnabled) {
#if RCT_DEV
// See isNative in lodash
NSString *testPostMessageNative = @"String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')";
NSString *testPostMessageNative = @"RegExp('^'+String(Object.prototype.toString).replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&').replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$').test(String(window.postMessage))";
BOOL postMessageIsNative = [
[webView stringByEvaluatingJavaScriptFromString:testPostMessageNative]
isEqualToString:@"true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void linkBridge() {
if (messagingEnabled) {
if (ReactBuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// See isNative in lodash
String testPostMessageNative = "String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')";
String testPostMessageNative = "RegExp('^'+String(Object.prototype.toString).replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&').replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$').test(String(window.postMessage))";
evaluateJavascript(testPostMessageNative, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Expand Down

0 comments on commit bcd0526

Please sign in to comment.