Lowercase the supported URL schemes, and use scheme list to determine if URL is internal

If a URL has no scheme, it is considered an internal URL. If it has a scheme, we need to lop off the trailing ":" from the protocol and then compare it against the entries in the supported scheme list.

Note that Cocoa does not automatically lowercase the supported schemes (we do this in native though) and Node's url library does.
This commit is contained in:
James Ide committed 2015-04-30 15:05:17 -07:00
1 parent 83bfe6cbcc
commit de7ea51fd4
2 files changed
+18 -5

No files matched your search

+15 -4
View File
@@ -46,10 +46,8 @@ var URLHandler = {
* without a host are treated as in-app URLs.
*/
openURL(targetURL: string) {
// Parse the query string and have "//" denote the hostname
var components = url.parse(targetURL, false, true);
if (components.protocol === 'xxx:') {
emitter.emit('request', url);
if (this.isInternalURL(targetURL)) {
emitter.emit('request', targetURL);
} else {
NTURLHandler.openURL(targetURL, () => {}, (error) => {
console.error('Error opening URL: ' + error.stack);
@@ -57,6 +55,19 @@ var URLHandler = {
}
},
/**
* Returns whether the given URL is an in-app URL or an external URL.
*/
isInternalURL(targetURL: string): bool {
// Parse the query string and have "//" denote the hostname
var {protocol} = url.parse(targetURL, false, true);
if (!protocol) {
return true;
}
var scheme = protocol.substring(0, protocol.length - 1);
return this.schemes.indexOf(scheme) !== -1;
},
/**
* Adds a listener that receives an object with URL information when the app
* has been instructed to open a URL. See the Request type.
+3 -1
View File
@@ -74,7 +74,9 @@ static NSString * const NTURLHandlerOpenURLNotification = @"NTURLHandlerOpenURL"
NSMutableSet *schemes = [NSMutableSet set];
NSDictionary *info = [NSBundle mainBundle].infoDictionary;
for (NSDictionary *urlType in info[@"CFBundleURLTypes"]) {
[schemes addObjectsFromArray:urlType[@"CFBundleURLSchemes"]];
for (NSString *scheme in urlType[@"CFBundleURLSchemes"]) {
[schemes addObject:scheme.lowercaseString];
}
}
return schemes;
}