Skip to content

Commit

Permalink
[ReactNative] RCTWebViewExecutor - Fix string containing script tag s…
Browse files Browse the repository at this point in the history
…upport
  • Loading branch information
tadeuzagallo committed May 7, 2015
1 parent d3c0029 commit 8d83b7f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions React/Executors/RCTWebViewExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ @implementation RCTWebViewExecutor
UIWebView *_webView;
NSMutableDictionary *_objectsToInject;
NSRegularExpression *_commentsRegex;
NSRegularExpression *_scriptTagsRegex;
}

@synthesize valid = _valid;
Expand All @@ -52,7 +53,8 @@ - (instancetype)initWithWebView:(UIWebView *)webView
if ((self = [super init])) {
_objectsToInject = [[NSMutableDictionary alloc] init];
_webView = webView ?: [[UIWebView alloc] init];
_commentsRegex = [NSRegularExpression regularExpressionWithPattern:@"(^ *?\\/\\/.*?$|\\/\\*\\*[\\s\\S]+?\\*\\/)" options:NSRegularExpressionAnchorsMatchLines error:NULL];
_commentsRegex = [NSRegularExpression regularExpressionWithPattern:@"(^ *?\\/\\/.*?$|\\/\\*\\*[\\s\\S]*?\\*\\/)" options:NSRegularExpressionAnchorsMatchLines error:NULL],
_scriptTagsRegex = [NSRegularExpression regularExpressionWithPattern:@"<(\\/?script[^>]*?)>" options:0 error:NULL],
_webView.delegate = self;
}
return self;
Expand Down Expand Up @@ -139,11 +141,6 @@ - (void)executeApplicationScript:(NSString *)script
onComplete(error);
};

script = [_commentsRegex stringByReplacingMatchesInString:script
options:0
range:NSMakeRange(0, script.length)
withTemplate:@""];

if (_objectsToInject.count > 0) {
NSMutableString *scriptWithInjections = [[NSMutableString alloc] initWithString:@"/* BEGIN NATIVELY INJECTED OBJECTS */\n"];
[_objectsToInject enumerateKeysAndObjectsUsingBlock:^(NSString *objectName, NSString *blockScript, BOOL *stop) {
Expand All @@ -158,6 +155,15 @@ - (void)executeApplicationScript:(NSString *)script
script = scriptWithInjections;
}

script = [_commentsRegex stringByReplacingMatchesInString:script
options:0
range:NSMakeRange(0, script.length)
withTemplate:@""];
script = [_scriptTagsRegex stringByReplacingMatchesInString:script
options:0
range:NSMakeRange(0, script.length)
withTemplate:@"\\\\<$1\\\\>"];

NSString *runScript =
[NSString
stringWithFormat:@"<html><head></head><body><script type='text/javascript'>%@</script></body></html>",
Expand Down

0 comments on commit 8d83b7f

Please sign in to comment.