From 1d7623348bfb8fa11a72fde1852ea21971c560e4 Mon Sep 17 00:00:00 2001 From: Jonathan Willing Date: Tue, 14 May 2013 01:37:22 -0500 Subject: encode javascript function parameters as json --- example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm | 13 ++++++++++++- example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js | 7 ++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'example') 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; } } -- cgit v1.2.3