Fix crash on attachment-only statuses posted from pleromafe

This commit is contained in:
Shadowfacts 2019-01-13 19:46:32 -05:00
parent db68b9dee0
commit 07a79657a7
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 11 additions and 6 deletions

View File

@ -10,13 +10,13 @@ import Foundation
extension NSMutableAttributedString { extension NSMutableAttributedString {
func trimCharactersInSet(_ charSet: CharacterSet) { func trimTrailingCharactersInSet(_ charSet: CharacterSet) {
var range = (string as NSString).rangeOfCharacter(from: charSet) var range = (string as NSString).rangeOfCharacter(from: charSet)
while range.length != 0 && range.location == 0 { // while range.length != 0 && range.location == 0 {
replaceCharacters(in: range, with: "") // replaceCharacters(in: range, with: "")
range = (string as NSString).rangeOfCharacter(from: charSet) // range = (string as NSString).rangeOfCharacter(from: charSet)
} // }
range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards)
while range.length != 0 && range.length + range.location == length { while range.length != 0 && range.length + range.location == length {

View File

@ -28,7 +28,12 @@ class ContentLabel: TTTAttributedLabel {
let (attributedText, links) = attributedTextForHTMLNode(body) let (attributedText, links) = attributedTextForHTMLNode(body)
let mutAttrString = NSMutableAttributedString(attributedString: attributedText) let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
mutAttrString.trimCharactersInSet(.whitespacesAndNewlines)
// only trailing whitespace can be trimmed here
// when posting an attachment without any text, pleromafe includes U+200B ZERO WIDTH SPACE at the beginning
// this would get trimmed and cause range out of bounds crashes
mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines)
mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length)) mutAttrString.addAttribute(.font, value: font, range: NSRange(location: 0, length: mutAttrString.length))
self.text = mutAttrString self.text = mutAttrString
for (range, url) in links { for (range, url) in links {