From bc963d53c69ead5e06cfb3cf52f75b64582178af Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 30 Jan 2020 15:36:16 -0500 Subject: [PATCH] fix(mail): wrap HTML part before re-encoding content Avoid improper encoding when the HTML part is using an unsupported encoding by libxml2. --- UI/MailPartViewers/UIxMailPartHTMLViewer.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/UI/MailPartViewers/UIxMailPartHTMLViewer.m b/UI/MailPartViewers/UIxMailPartHTMLViewer.m index 187336d71..768629934 100644 --- a/UI/MailPartViewers/UIxMailPartHTMLViewer.m +++ b/UI/MailPartViewers/UIxMailPartHTMLViewer.m @@ -811,6 +811,14 @@ _xmlCharsetForCharset (NSString *charset) handler = [_UIxHTMLMailContentHandler new]; [handler setAttachmentIds: attachmentIds]; + // Some broken email messages have some additionnal content outside the main HTML tags which are + // ignored by libxml. + // We surround the whole part with additional HTML tags to render all content. + htmlContent = [NSMutableData dataWithBytes: "" length: 6]; + [htmlContent appendData: preparsedContent]; + [htmlContent appendBytes: "" length: 7]; + preparsedContent = (NSData *)htmlContent; + // We check if we got an unsupported charset. If so // we convert everything to UTF-16{LE,BE} so it passes // in libxml2 and also in characters: length: defined @@ -859,17 +867,10 @@ _xmlCharsetForCharset (NSString *charset) RELEASE(s); } - // Some broken email messages have some additionnal content outside the main HTML tags which are - // ignored by libxml. - // We surround the whole part with additional HTML tags to render all content. - htmlContent = [NSMutableData dataWithBytes: "" length: 6]; - [htmlContent appendData: preparsedContent]; - [htmlContent appendBytes: "" length: 7]; - [handler setContentEncoding: enc]; [parser setContentHandler: handler]; - [parser parseFromSource: htmlContent]; + [parser parseFromSource: preparsedContent]; } - (NSString *) cssContent