diff --git a/Mail/Views/Thread/WebView/WebViewModel.swift b/Mail/Views/Thread/WebView/WebViewModel.swift index 5e6760225..e03e7e589 100644 --- a/Mail/Views/Thread/WebView/WebViewModel.swift +++ b/Mail/Views/Thread/WebView/WebViewModel.swift @@ -67,10 +67,6 @@ final class WebViewModel: NSObject, ObservableObject { func loadBody(presentableBody: PresentableBody, blockRemoteContent: Bool) async throws -> LoadResult { var messageBody = showBlockQuote ? presentableBody.body?.value : presentableBody.compactBody - if let body = messageBody, presentableBody.body?.type == "text/plain" { - messageBody = try await MessageWebViewUtils.createHTMLForPlainText(text: body) - } - let loadResult = await loadHTMLString(value: messageBody, blockRemoteContent: blockRemoteContent) return loadResult } diff --git a/MailCore/Utils/MessageBodyUtils.swift b/MailCore/Utils/MessageBodyUtils.swift index 98031f525..bc851cab6 100644 --- a/MailCore/Utils/MessageBodyUtils.swift +++ b/MailCore/Utils/MessageBodyUtils.swift @@ -49,15 +49,15 @@ public enum MessageBodyUtils { guard let messageBody = message.body else { return nil } - let bodyValue = messageBody.value ?? "" + var bodyValue = messageBody.value ?? "" do { - let printHeader = try createPrintHeader(message: message) - let originalDocument = try await SwiftSoup.parse(bodyValue).prependChild(printHeader) - - let bodyFromDoc = try originalDocument.outerHtml() + if messageBody.type == "text/plain" { + bodyValue = try await MessageWebViewUtils.createHTMLForPlainText(text: bodyValue) + } + bodyValue = try await prependPrintHeader(to: bodyValue, with: message) - let messageBodyQuote = await splitBodyAndQuote(messageBody: bodyFromDoc) + let messageBodyQuote = await splitBodyAndQuote(messageBody: bodyValue) return PresentableBody( body: messageBody, @@ -215,6 +215,13 @@ public enum MessageBodyUtils { return try element.prependChild(recipientsField) } + public static func prependPrintHeader(to body: String, with message: Message) async throws -> String { + let parsedBody = try await SwiftSoup.parse(body) + let printHeader = try createPrintHeader(message: message) + let originalDocument = try parsedBody.prependChild(printHeader) + return try originalDocument.outerHtml() + } + // MARK: - Utils /// Some email clients rename css classes to prefix them for example.