Skip to content

Commit

Permalink
Merge pull request #20284 from wordpress-mobile/issue/20188-skip-pars…
Browse files Browse the repository at this point in the history
…ing-in-background

Fix: Skip parsing blogging reminders schedule in background
  • Loading branch information
dvdchr authored Mar 13, 2023
2 parents cf7dfb0 + e18784e commit 4d007ba
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ private extension BloggingRemindersScheduleFormatter {
}

static func stringToAttributedString(_ string: String) -> NSAttributedString {
// When the app is in a background state, return the non-emphasized plain String instead.
// Parsing a HTML string in the background may lead to a crash.
// Refer to https://github.com/wordpress-mobile/WordPress-iOS/pull/17678 for a similar scenario and explanation.
if UIApplication.shared.applicationState == .background {
return .init(string: string.removedHTMLEmphases())
}

let htmlData = NSString(string: string).data(using: String.Encoding.unicode.rawValue) ?? Data()
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html]

Expand Down Expand Up @@ -176,3 +183,13 @@ private extension BloggingRemindersScheduleFormatter {
comment: "Short title telling the user they will receive a blogging reminder every day of the week.")
}
}

private extension String {
/// Convenient method to remove HTML emphasis tags from a given string.
func removedHTMLEmphases() -> String {
guard let expression = try? NSRegularExpression(pattern: "</?strong>", options: .caseInsensitive) else {
return self
}
return expression.stringByReplacingMatches(in: self, range: NSMakeRange(0, self.count), withTemplate: String())
}
}

0 comments on commit 4d007ba

Please sign in to comment.