Permalink
Browse files

.allowedSchemes feature, where anything outside the list will be conv…

…erted to the best match.
  • Loading branch information...
mikeabdullah committed Apr 12, 2012
1 parent 6af2425 commit a32a6a0a0ac5ddb24a6a60a81d6f0502ee99015b
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 27A60C0E1536E7F500EEE538 /* TestKSURLFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 27A60C0D1536E7F500EEE538 /* TestKSURLFormatter.m */; };
27DA2577148E641100209E50 /* TestKSURLUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 27DA2576148E641100209E50 /* TestKSURLUtilities.m */; };
EE0F59A313897980006045D3 /* KSPathUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = EE0F599713897980006045D3 /* KSPathUtilities.m */; };
EE0F59AA13897B20006045D3 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EE0F59A913897B20006045D3 /* WebKit.framework */; };
@@ -22,6 +23,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 27A60C0C1536E7F500EEE538 /* TestKSURLFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestKSURLFormatter.h; sourceTree = "<group>"; };
+ 27A60C0D1536E7F500EEE538 /* TestKSURLFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestKSURLFormatter.m; sourceTree = "<group>"; };
27DA2576148E641100209E50 /* TestKSURLUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestKSURLUtilities.m; sourceTree = "<group>"; };
EE0F599613897980006045D3 /* KSPathUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KSPathUtilities.h; sourceTree = SOURCE_ROOT; };
EE0F599713897980006045D3 /* KSPathUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KSPathUtilities.m; sourceTree = SOURCE_ROOT; };
@@ -126,6 +129,8 @@
EE20B4B513897F7400AE2AF9 /* TestKSPathUtilities.m */,
EE83868213897644005E0FFD /* Supporting Files */,
27DA2576148E641100209E50 /* TestKSURLUtilities.m */,
+ 27A60C0C1536E7F500EEE538 /* TestKSURLFormatter.h */,
+ 27A60C0D1536E7F500EEE538 /* TestKSURLFormatter.m */,
);
path = TestKSFileUtilities;
sourceTree = "<group>";
@@ -227,6 +232,7 @@
EE43C85313898D7B008ABD16 /* KSWebLocationPasteboardUtilities.m in Sources */,
EE43C85413898D7B008ABD16 /* KSWorkspaceUtilities.m in Sources */,
27DA2577148E641100209E50 /* TestKSURLUtilities.m in Sources */,
+ 27A60C0E1536E7F500EEE538 /* TestKSURLFormatter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -33,6 +33,7 @@
@private
BOOL _useDisplayNameForFileURLs;
NSString *_defaultScheme;
+ NSArray *_allowedSchemes;
NSString *_fallbackTopLevelDomain;
BOOL _generateStrings;
}
@@ -58,6 +59,10 @@
// If no scheme is recognisable from the string, this will be substituted. Default is http
@property(nonatomic, copy) NSString *defaultScheme;
+// Default value is nil, which means any scheme is allowed. If a URL is entered that isn't in this list, the formatter substitutes in whatever it considers to be the best match, generally favouring those nearer the start of the array
+// An empty array is not permitted
+@property(nonatomic, copy) NSArray *allowedSchemes;
+
// If the URL's host does not have a top-level domain specified, and this is non-nil, it is substituted in. Defaults is "com"
@property(nonatomic, copy) NSString *fallbackTopLevelDomain;
View
@@ -92,6 +92,7 @@ - (id)init
- (void)dealloc
{
[_defaultScheme release];
+ [_allowedSchemes release];
[_fallbackTopLevelDomain release];
[super dealloc];
}
@@ -100,6 +101,16 @@ - (void)dealloc
@synthesize useDisplayNameForFileURLs = _useDisplayNameForFileURLs;
@synthesize defaultScheme = _defaultScheme;
+
+@synthesize allowedSchemes = _allowedSchemes;
+- (void)setAllowedSchemes:(NSArray *)schemes;
+{
+ if (schemes) NSParameterAssert([schemes count] > 0);
+
+ schemes = [schemes copy];
+ [_allowedSchemes release]; _allowedSchemes = schemes;
+}
+
@synthesize fallbackTopLevelDomain = _fallbackTopLevelDomain;
#pragma mark Textual Representation of Cell Content
@@ -242,6 +253,29 @@ - (NSURL *)URLFromString:(NSString *)string;
result = [NSURL URLWithString:urlString];
}
}
+
+
+ // Make sure the scheme is allowed
+ NSArray *allowedSchemes = [self allowedSchemes];
+ if (allowedSchemes)
+ {
+ NSString *scheme = [result scheme];
+ if (scheme && ![allowedSchemes containsObject:[result scheme]])
+ {
+ // Look for the best matching scheme based on the assumption that the URL was simply pasted in missing a small number of initial characters
+ for (NSString *aScheme in allowedSchemes)
+ {
+ if ([aScheme hasSuffix:scheme])
+ {
+ result = [result ks_URLWithScheme:aScheme];
+ return result;
+ }
+ }
+
+ // Nothing matched? Alright, go for the first one then
+ result = [result ks_URLWithScheme:[allowedSchemes objectAtIndex:0]];
+ }
+ }
}
return result;
@@ -0,0 +1,13 @@
+//
+// TestKSURLFormatter.h
+// KSFileUtilities
+//
+// Created by Mike Abdullah on 12/04/2012.
+// Copyright (c) 2012 Jungle Candy Software. All rights reserved.
+//
+
+#import <SenTestingKit/SenTestingKit.h>
+
+@interface TestKSURLFormatter : SenTestCase
+
+@end
@@ -0,0 +1,51 @@
+//
+// TestKSURLFormatter.m
+// KSFileUtilities
+//
+// Created by Mike Abdullah on 12/04/2012.
+// Copyright (c) 2012 Jungle Candy Software. All rights reserved.
+//
+
+#import "TestKSURLFormatter.h"
+#import "KSURLFormatter.h"
+
+
+@implementation TestKSURLFormatter
+
+- (void)testAllowedSchemesWithString:(NSString *)urlString expectedURLString:(NSString *)expectedResult
+{
+ KSURLFormatter *formatter = [[KSURLFormatter alloc] init];
+ [formatter setAllowedSchemes:[NSArray arrayWithObjects:@"http", @"https", @"file", nil]];
+
+ NSURL *URL = [formatter URLFromString:urlString];
+ STAssertEqualObjects([URL absoluteString], expectedResult, nil);
+
+ [formatter release];
+}
+
+- (void)testAllowedSchemesPrimary;
+{
+ [self testAllowedSchemesWithString:@"http://example.com/" expectedURLString:@"http://example.com/"];
+}
+
+- (void)testAllowedSchemesSecondary
+{
+ [self testAllowedSchemesWithString:@"https://example.com/" expectedURLString:@"https://example.com/"];
+}
+
+- (void)testAllowedSchemesCloseMatchPrimary
+{
+ [self testAllowedSchemesWithString:@"ttp://example.com/" expectedURLString:@"http://example.com/"];
+}
+
+- (void)testAllowedSchemesCloseMatchSecondary
+{
+ [self testAllowedSchemesWithString:@"ttps://example.com/" expectedURLString:@"https://example.com/"];
+}
+
+- (void)testAllowedSchemesRandom
+{
+ [self testAllowedSchemesWithString:@"test://example.com/" expectedURLString:@"http://example.com/"];
+}
+
+@end

0 comments on commit a32a6a0

Please sign in to comment.