Skip to content

Commit

Permalink
fix: ui test
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanmo226 committed Nov 5, 2024
1 parent db9e5b2 commit 03945ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension ComposeViewController {

func getSignature() -> String? {
let sendAs = sendAsList.first(where: { $0.sendAsEmail == contextToSend.sender })
if let signature = sendAs?.signature {
if let signature = sendAs?.signature, signature.isNotEmpty {
return "\n\n--\n\(signature.removingHtmlTags())"
}
return nil
Expand Down
35 changes: 19 additions & 16 deletions FlowCryptCommon/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,26 @@ public extension String {
}

func removingHtmlTags() -> String {
// Replace <br> and <p> tags with newlines
var stringWithLineBreaks = self.replacingOccurrences(of: "<br>", with: "\n")
stringWithLineBreaks = stringWithLineBreaks.replacingOccurrences(of: "</p>", with: "\n")
stringWithLineBreaks = stringWithLineBreaks.replacingOccurrences(of: "<p>", with: "")

// Convert the HTML string to NSAttributedString
if let data = stringWithLineBreaks.data(using: .utf8) {
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
// Return the plain string from the attributed string
return attributedString.string
}
// Pre-process: Temporarily replace existing line breaks with a unique placeholder
// Because \n line breaks are removed when converting html to plain text
let lineBreakPlaceholder = "###LINE_BREAK###"
let processedString = self
.replacingOccurrences(of: "\n", with: lineBreakPlaceholder)
.replacingOccurrences(of: "<br>", with: lineBreakPlaceholder)
.replacingOccurrences(of: "</p>", with: lineBreakPlaceholder)
.replacingOccurrences(of: "<p>", with: "")

// Convert HTML to plain text using NSAttributedString
guard let data = processedString.data(using: .utf8),
let attributedString = try? NSAttributedString(data: data, options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil) else {
return self // Fallback to the original if conversion fails
}
return self

// Restore line breaks from placeholders
return attributedString.string.replacingOccurrences(of: lineBreakPlaceholder, with: "\n")
}

func removingMailThreadQuote() -> String {
Expand Down

0 comments on commit 03945ce

Please sign in to comment.