Attempt to auto detect the document type when initializing an

attributed string and the type wasn't specified by the caller.
Carefully add base URL to the document importing options only when it
is not nil.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@30774 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Wolfgang Lux 2010-06-18 07:49:26 +00:00
parent c4ad9a9278
commit fc60476d53
2 changed files with 57 additions and 10 deletions

View file

@ -1,3 +1,14 @@
2010-06-18 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSAttributedString.m
(-initWithData:options:documentAttributes:error:): Attempt to auto
detect the document type if it wasn't specified by the caller.
Currently works for RTF and HTML documents.
* Source/NSAttributedString.m (-initWithURL:documentAttributes:,
-initWithURL:options:documentAttributes:error:): Add base URL to
the document importing options only if it is not nil.
2010-06-15 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSMenu.m (-performKeyEquivalent:): Fix Doug's previous

View file

@ -32,6 +32,7 @@
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSError.h>
#import <Foundation/NSException.h>
@ -765,7 +766,31 @@ create_error(int code, NSString* desc)
if (type == nil)
{
// FIXME: try to determine type
const void *dataBytes = [data bytes];
// The list of file types below was derived from Apache's conf/magic file
// FIXME extend the list
if (strncmp(dataBytes, "{\\rtf", 5) == 0)
{
type = NSRTFTextDocumentType;
}
else if (strncmp(dataBytes, "<!DOCTYPE HTML", 14) == 0 ||
strncmp(dataBytes, "<!doctype html", 14) == 0 ||
strncmp(dataBytes, "<HEAD", 5) == 0 ||
strncmp(dataBytes, "<head", 5) == 0 ||
strncmp(dataBytes, "<TITLE", 6) == 0 ||
strncmp(dataBytes, "<title", 6) == 0 ||
strncmp(dataBytes, "<HTML", 5) == 0 ||
strncmp(dataBytes, "<html", 5) == 0 ||
strncmp(dataBytes, "<!--", 4) == 0 ||
strncmp(dataBytes, "<h1", 3) == 0 ||
strncmp(dataBytes, "<H1", 3) == 0)
{
type = NSHTMLTextDocumentType;
}
}
if (type == nil)
{
if (error)
*error = create_error(0, NSLocalizedString(@"No type specified for data.",
@"Error description"));
@ -845,8 +870,14 @@ create_error(int code, NSString* desc)
- (id) initWithURL: (NSURL *)url
documentAttributes: (NSDictionary **)dict
{
NSDictionary *options = [NSDictionary dictionaryWithObject: [url baseURL]
forKey: NSBaseURLDocumentOption];
NSURL *baseURL = [url baseURL];
NSDictionary *options = nil;
if (baseURL != nil)
{
[NSDictionary dictionaryWithObject: baseURL
forKey: NSBaseURLDocumentOption];
}
return [self initWithURL: url
options: options
@ -859,6 +890,7 @@ documentAttributes: (NSDictionary **)dict
documentAttributes: (NSDictionary **)dict
error: (NSError **)error
{
NSURL *baseURL;
NSData *data = [url resourceDataUsingCache: YES];
if (data == nil)
@ -871,14 +903,18 @@ documentAttributes: (NSDictionary **)dict
}
// Pass on baseURL
if (options == nil)
options = [NSDictionary dictionaryWithObject: [url baseURL]
forKey: NSBaseURLDocumentOption];
else if ([options objectForKey: NSBaseURLDocumentOption] == nil)
baseURL = [url baseURL];
if (baseURL != nil)
{
options = AUTORELEASE([options mutableCopy]);
[(NSMutableDictionary*)options setObject: [url baseURL]
forKey: NSBaseURLDocumentOption];
if (options == nil)
options = [NSDictionary dictionaryWithObject: baseURL
forKey: NSBaseURLDocumentOption];
else if ([options objectForKey: NSBaseURLDocumentOption] == nil)
{
options = AUTORELEASE([options mutableCopy]);
[(NSMutableDictionary*)options setObject: baseURL
forKey: NSBaseURLDocumentOption];
}
}
return [self initWithData: data