Skip to content

Commit

Permalink
fix(Print): Add print header correctly to plain text message (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon authored Feb 12, 2024
2 parents 1a3be8f + 8999083 commit 778215c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
4 changes: 0 additions & 4 deletions Mail/Views/Thread/WebView/WebViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 13 additions & 6 deletions MailCore/Utils/MessageBodyUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 778215c

Please sign in to comment.