Skip to content

Commit

Permalink
Pasteboard reading uses NSTextCheckingResult when available for natur…
Browse files Browse the repository at this point in the history
…al language recognition of URLs.
  • Loading branch information
mikeabdullah committed Feb 7, 2012
1 parent 1d009b9 commit 4ef5615
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions KSWebLocationPasteboardUtilities.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type; - (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type;




#pragma mark URL Guessing
+ (NSURL *)URLFromPasteboard:(NSPasteboard *)pboard; // like the WebView method, but takes advantage of NSTextCheckingTypeLink on 10.6+


#pragma mark 10.5 Pasteboard Support #pragma mark 10.5 Pasteboard Support
+ (NSArray *)webLocationPasteboardTypes; + (NSArray *)webLocationPasteboardTypes;


Expand Down
33 changes: 32 additions & 1 deletion KSWebLocationPasteboardUtilities.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ + (KSWebLocation *)webLocationFromPasteboard:(NSPasteboard *)pasteboard;
{ {
KSWebLocation *result = nil; KSWebLocation *result = nil;


NSURL *URL = [WebView URLFromPasteboard:pasteboard]; NSURL *URL = [self URLFromPasteboard:pasteboard];
if (URL) if (URL)
{ {
result = [KSWebLocation webLocationWithURL:URL result = [KSWebLocation webLocationWithURL:URL
Expand All @@ -131,6 +131,37 @@ + (KSWebLocation *)webLocationFromPasteboard:(NSPasteboard *)pasteboard;
return result; return result;
} }


+ (NSURL *)URLFromPasteboard:(NSPasteboard *)pboard
{
NSURL *result = [WebView URLFromPasteboard:pboard];

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
if (!result)
{
NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
if ([spellChecker respondsToSelector:@selector(checkString:range:types:options:inSpellDocumentWithTag:orthography:wordCount:)])
{
NSString *string = [pboard stringForType:NSStringPboardType];

NSArray *checkResults = [spellChecker checkString:string
range:NSMakeRange(0, [string length])
types:NSTextCheckingTypeLink
options:nil
inSpellDocumentWithTag:0
orthography:NULL
wordCount:NULL];

if ([checkResults count])
{
result = [[checkResults objectAtIndex:0] URL];
}
}
}
#endif

return result;
}

#pragma mark Deprecated #pragma mark Deprecated


+ (NSArray *)webLocationPasteboardTypes + (NSArray *)webLocationPasteboardTypes
Expand Down

0 comments on commit 4ef5615

Please sign in to comment.