From a8f2d4f27fda47500314c0e977c6fa9293f6a660 Mon Sep 17 00:00:00 2001 From: Shawn Dempsey Date: Wed, 2 Nov 2022 19:00:07 -0700 Subject: [PATCH] Local filenames with colon should be parsed correctly (#35123) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35123 **Context** On RN Desktop, images can be copy/pasted into text inputs. When copy/pasting local files w/ semi-colons in the filename (`/downloads/devices:pre-call-IMG_346C38284B2B-1.jpeg`), RN would throw this error. {F785684529} The error was due to no having the correct asset loader due to the local file path being parsed incorrectly. To parse the url, we convert the string URI to a `NSURL` using a custom convertor fn. The conversion was matching any `:` and assuming it was a network url scheme: https://www.internalfb.com/code/archon_react_native_macos/[fde4113acd89fb13ee11636c48b59eac49c21bae]/React/Base/RCTConvert.m?lines=97-111 When an image path with `:` in the name was passed to this, it was assuming it was a valid network url and not file path. Because this is a local filepath, the image path needs to be a filesystem url. Since this was parsed as network url, it did not have the `file://` prefix and would not pass this check, causing the loader to throw.l https://www.internalfb.com/code/fbsource/%5B60d9d5e67383%5D/xplat/js/react-native-github/Libraries/Image/RCTImageLoader.mm?lines=220-230 ## Changelog [iOS] [Added] - Add support for parsing files w/ `:` in filename Reviewed By: christophpurrer, philIip Differential Revision: D40729963 fbshipit-source-id: 2f3adcabc8f0f1f22cbfca69c3484e72b1d93d25 --- React/Base/RCTConvert.m | 2 +- packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/React/Base/RCTConvert.m b/React/Base/RCTConvert.m index 33e2a094a0484a..5e4820585b0131 100644 --- a/React/Base/RCTConvert.m +++ b/React/Base/RCTConvert.m @@ -91,7 +91,7 @@ + (NSURL *)NSURL:(id)json } // Check if it has a scheme - if ([path rangeOfString:@":"].location != NSNotFound) { + if ([path rangeOfString:@"://"].location != NSNotFound) { NSMutableCharacterSet *urlAllowedCharacterSet = [NSMutableCharacterSet new]; [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLUserAllowedCharacterSet]]; [urlAllowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet URLPasswordAllowedCharacterSet]]; diff --git a/packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m b/packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m index 2eb72a7a170078..7d3dadde38fbc8 100644 --- a/packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m +++ b/packages/rn-tester/RNTesterUnitTests/RCTConvert_NSURLTests.m @@ -45,6 +45,9 @@ -(void)test_##name \ TEST_BUNDLE_PATH(imageAt2XPath, @"images/foo@2x.jpg", @"images/foo@2x.jpg") TEST_BUNDLE_PATH(imageFile, @"foo.jpg", @"foo.jpg") +TEST_BUNDLE_PATH(imageFileWithSemicolon, @"folder/foo:bar-baz.jpg", @"folder/foo:bar-baz.jpg") +TEST_URL(filePathWithSemicolon, @"/folder/foo:bar-baz.jpg", @"file:///folder/foo:bar-baz.jpg") + // User documents TEST_PATH( documentsFolder,