diff --git a/ChangeLog b/ChangeLog index 2ae52699b..466c657c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2012-03-22 Eric Wasylishen + + * Source/NSAttributedString.m + (-initWithData:options:documentAttributes:error:): Fixes for TextEdit. + If no type is provided, assume plain text. If no encoding is provided, + assume UTF-8 unless a UTF-16 BOM is detected. + 2012-03-22 Eric Wasylishen * Source/GSThemeDrawing.m (-toolbarBorderColor): Change default to diff --git a/Source/NSAttributedString.m b/Source/NSAttributedString.m index 30bb12aec..54f16fbe9 100644 --- a/Source/NSAttributedString.m +++ b/Source/NSAttributedString.m @@ -797,11 +797,7 @@ create_error(int code, NSString* desc) } if (type == nil) { - if (error) - *error = create_error(0, NSLocalizedString(@"No type specified for data.", - @"Error description")); - RELEASE(self); - return nil; + type = NSPlainTextDocumentType; } converter = converter_class(type, NO); @@ -829,6 +825,30 @@ create_error(int code, NSString* desc) NSDictionary *defaultAttrs = [options objectForKey: @"DefaultAttributes"]; NSString *str; + if (encoding == GSUndefinedEncoding) + { + encoding = NSUTF8StringEncoding; + + if ([data length] >= 2) + { + static const unichar byteOrderMark = 0xFEFF; + static const unichar byteOrderMarkSwapped = 0xFFFE; + const unichar firstChar = ((const unichar *)[data bytes])[0]; + if (firstChar == byteOrderMark + || firstChar == byteOrderMarkSwapped) + { + encoding = NSUnicodeStringEncoding; + } + } + } + + if (dict != NULL) + { + *dict = [NSDictionary dictionaryWithObjectsAndKeys: + NSPlainTextDocumentType, NSDocumentTypeDocumentAttribute, + nil]; + } + str = [[NSString alloc] initWithData: data encoding: encoding]; self = [self initWithString: str