diff options
author | Jonathan Willing <jwilling@me.com> | 2013-05-14 01:37:22 -0500 |
---|---|---|
committer | Jonathan Willing <jwilling@me.com> | 2013-05-14 01:37:22 -0500 |
commit | 1d7623348bfb8fa11a72fde1852ea21971c560e4 (patch) | |
tree | ec29cc896649f9aca84f60108989bbb1cc76365b /example | |
parent | 027cbae7a9359b38661e6a576826f15909dc5286 (diff) |
encode javascript function parameters as json
Diffstat (limited to 'example')
-rwxr-xr-x | example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm | 13 | ||||
-rw-r--r-- | example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js | 7 |
2 files changed, 16 insertions, 4 deletions
diff --git a/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm b/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm index 5e89f33b..710c7c7a 100755 --- a/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm +++ b/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm @@ -146,7 +146,10 @@ NSString *filename = [NSString stringWithFormat:@"%lu", (unsigned long)urlString.hash]; NSURL *cacheURL = [self _cacheJPEGImageData:previewData withFilename:filename]; - NSString *replaceScript = [NSString stringWithFormat:@"replaceImageSrc(\"%@\", \"%@\")", urlString, cacheURL.absoluteString]; + NSDictionary *args = @{ @"URLKey": urlString, @"LocalPathKey": cacheURL.absoluteString }; + NSString *jsonString = [self _jsonEscapedStringFromDictionary:args]; + + NSString *replaceScript = [NSString stringWithFormat:@"replaceImageSrc(\"%@\")", jsonString]; [_webView stringByEvaluatingJavaScriptFromString:replaceScript]; }; @@ -160,6 +163,14 @@ } } +- (NSString *) _jsonEscapedStringFromDictionary:(NSDictionary *)dictionary { + NSData *json = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil]; + NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; + jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; + jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; + return jsonString; +} + - (NSURL *) _cacheJPEGImageData:(NSData *)imageData withFilename:(NSString *)filename { NSString *path = [[NSTemporaryDirectory() stringByAppendingPathComponent:filename] stringByAppendingPathExtension:@"jpg"]; diff --git a/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js b/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js index 67510c74..1078b04b 100644 --- a/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js +++ b/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js @@ -16,13 +16,14 @@ var findCIDImageURL = function() { return JSON.stringify(imgLinks); } -var replaceImageSrc = function(original, local) { +var replaceImageSrc = function(jsonString) { + var parsedJson = JSON.parse(jsonString); var images = imageElements(); for (var i = 0; i < images.length; i++) { var url = images[i].getAttribute('src'); - if (url.indexOf(original) == 0) { - images[i].setAttribute('src', local); + if (url.indexOf(parsedJson.URLKey) == 0) { + images[i].setAttribute('src', parsedJson.LocalPathKey); break; } } |