fix(mail): Use text/plain fallback if an error occured while parsing html message

This commit is contained in:
smizrahi
2024-02-05 10:50:07 +01:00
parent ce0e650c62
commit f56910db1e
4 changed files with 112 additions and 5 deletions

View File

@@ -179,8 +179,9 @@
id info, viewer;
NSArray *parts;
NSMutableArray *renderedParts;
NSString *preferredType;
NSString *preferredType, *htmlNoTags;
NSUInteger i, max;
BOOL fallbackNeeded, hasTextPlain;
if ([self decodedFlatContent])
parts = [[self decodedFlatContent] parts];
@@ -214,6 +215,27 @@
[[self childInfo] objectForKey: @"type"],
[[self childInfo] objectForKey: @"subtype"]];
// Check if the HTML part is good
// If no, try to fallback on text/plain
fallbackNeeded = NO;
hasTextPlain = NO;
for (i = 0 ; i < [renderedParts length] ; i++) {
if ([[[[renderedParts objectAtIndex: i] objectForKey: @"contentType"] lowercaseString] isEqualToString: @"text/html"]
&& [[preferredType lowercaseString] isEqualToString: @"text/html"]) {
if ([[renderedParts objectAtIndex: i] objectForKey:@"exception"]) {
fallbackNeeded = YES;
}
}
if ([[[[renderedParts objectAtIndex: i] objectForKey: @"contentType"] lowercaseString] isEqualToString: @"text/plain"]) {
hasTextPlain = YES;
}
}
if (fallbackNeeded && hasTextPlain) {
preferredType = @"text/plain";
}
return [NSDictionary dictionaryWithObjectsAndKeys:
[self className], @"type",
preferredType, @"preferredPart",

View File

@@ -23,9 +23,10 @@
#import "UIxMailPartViewer.h"
@interface UIxMailPartHTMLViewer : UIxMailPartViewer
@interface UIxMailPartHTMLViewer : UIxMailPartViewer <SaxErrorHandler>
{
id handler;
NSException *ex;
}
- (NSString *) flatContentAsString;
@@ -35,6 +36,7 @@
@interface UIxMailPartExternalHTMLViewer : UIxMailPartViewer
{
id handler;
NSException *ex;
}
- (NSString *) flatContentAsString;

View File

@@ -750,6 +750,7 @@ _xmlCharsetForCharset (NSString *charset)
if ((self = [super init]))
{
handler = nil;
ex = nil;
}
return self;
@@ -758,6 +759,9 @@ _xmlCharsetForCharset (NSString *charset)
- (void) dealloc
{
[handler release];
if (ex) {
[ex release];
}
[super dealloc];
}
@@ -782,6 +786,8 @@ _xmlCharsetForCharset (NSString *charset)
xmlCharEncoding enc;
[self cleanException];
if ([[self decodedFlatContent] isKindOfClass: [NGMimeBodyPart class]])
preparsedContent = [[[self decodedFlatContent] body] sanitizedContentUsingVoidTags: VoidTags];
else
@@ -852,9 +858,37 @@ _xmlCharsetForCharset (NSString *charset)
[handler setContentEncoding: enc];
[parser setContentHandler: handler];
[parser setErrorHandler: self];
[parser parseFromSource: preparsedContent];
}
- (void)cleanException
{
ex = nil;
}
- (void)warning:(SaxParseException *)_exception
{
}
- (void)error:(SaxParseException *)_exception
{
}
- (void)fatalError:(SaxParseException *)_exception
{
ex = [NSException exceptionWithName:[_exception name] reason: [_exception reason] userInfo: [_exception userInfo]];
[ex retain];
}
- (NSException *)getException
{
return ex;
}
- (NSString *) cssContent
{
NSString *cssContent, *css;
@@ -890,6 +924,7 @@ _xmlCharsetForCharset (NSString *charset)
if ((self = [super init]))
{
handler = nil;
ex = nil;
}
return self;
@@ -898,6 +933,9 @@ _xmlCharsetForCharset (NSString *charset)
- (void) dealloc
{
[handler release];
if (ex) {
[ex release];
}
[super dealloc];
}
@@ -921,6 +959,8 @@ _xmlCharsetForCharset (NSString *charset)
NSString *encoding;
xmlCharEncoding enc;
[self cleanException];
parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
createXMLReaderForMimeType: @"text/html"];
@@ -966,9 +1006,36 @@ _xmlCharsetForCharset (NSString *charset)
[handler setContentEncoding: enc];
[parser setContentHandler: handler];
[parser setErrorHandler: self];
[parser parseFromSource: preparsedContent];
}
- (void)cleanException
{
ex = nil;
}
- (void)warning:(SaxParseException *)_exception
{
}
- (void)error:(SaxParseException *)_exception
{
}
- (void)fatalError:(SaxParseException *)_exception
{
ex = [NSException exceptionWithName:[_exception name] reason: [_exception reason] userInfo: [_exception userInfo]];
[ex retain];
}
- (NSException *)getException
{
return ex;
}
- (NSString *) filename
{
return [[self clientObject] filename];

View File

@@ -190,16 +190,27 @@
- (id) renderedPart
{
NSString *type;
NSString *type, *content;
NSException *e;
NSMutableDictionary *r;
e = nil;
type = [NSString stringWithFormat: @"%@/%@",
[bodyInfo objectForKey: @"type"],
[bodyInfo objectForKey: @"subtype"]];
return [NSDictionary dictionaryWithObjectsAndKeys:
content = [[[self generateResponse] contentAsString] stringWithoutHTMLInjection: NO];
if ([self respondsToSelector:@selector(getException)]) {
e = [self getException];
}
r = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[self className], @"type",
type, @"contentType",
[[[self generateResponse] contentAsString] stringWithoutHTMLInjection: NO], @"content",
content, @"content",
[self filenameForDisplay], @"filename",
[self preferredPathExtension], @"extension",
[[self sizeFormatter] stringForObjectValue: [bodyInfo objectForKey: @"size"]], @"size",
@@ -207,6 +218,11 @@
[self pathForDownload], @"downloadURL",
[NSNumber numberWithBool:_shouldDisplayAttachment], @"shouldDisplayAttachment",
nil];
if (e) {
[r setObject: [e userInfo] forKey: @"exception"];
}
return r;
}
- (NSDictionary *) attachmentIds