Fix whitespace in statuses not being trimmed

This commit is contained in:
Shadowfacts 2024-01-17 15:51:54 -05:00
parent 534f83e716
commit 65c3c8026d
5 changed files with 64 additions and 12 deletions

View File

@ -2451,6 +2451,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
INFOPLIST_FILE = TuskerUITests/Info.plist; INFOPLIST_FILE = TuskerUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = ( LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
@ -2804,6 +2805,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
INFOPLIST_FILE = TuskerUITests/Info.plist; INFOPLIST_FILE = TuskerUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = ( LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
@ -2823,6 +2825,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
INFOPLIST_FILE = TuskerUITests/Info.plist; INFOPLIST_FILE = TuskerUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = ( LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",

View File

@ -30,20 +30,31 @@ extension NSAttributedString {
extension NSMutableAttributedString { extension NSMutableAttributedString {
func trimLeadingCharactersInSet(_ charSet: CharacterSet) { func trimLeadingCharactersInSet(_ charSet: CharacterSet) {
var range = (string as NSString).rangeOfCharacter(from: charSet) var end = string.startIndex
while end < string.endIndex && charSet.contains(string.unicodeScalars[end]) {
while range.length != 0 && range.location == 0 { end = string.unicodeScalars.index(after: end)
replaceCharacters(in: range, with: "") }
range = (string as NSString).rangeOfCharacter(from: charSet) if end > string.startIndex {
let length = string.utf16.distance(from: string.startIndex, to: end)
replaceCharacters(in: NSRange(location: 0, length: length), with: "")
} }
} }
func trimTrailingCharactersInSet(_ charSet: CharacterSet) { func trimTrailingCharactersInSet(_ charSet: CharacterSet) {
var range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) if string.isEmpty {
return
while range.length != 0 && range.length + range.location == length { }
replaceCharacters(in: range, with: "") var start = string.index(before: string.endIndex)
range = (string as NSString).rangeOfCharacter(from: charSet, options: .backwards) while start > string.startIndex && charSet.contains(string.unicodeScalars[start]) {
start = string.unicodeScalars.index(before: start)
}
if start < string.endIndex {
if start != string.startIndex || !charSet.contains(string.unicodeScalars[start]) {
start = string.unicodeScalars.index(after: start)
}
let location = string.utf16.distance(from: string.startIndex, to: start)
let length = string.utf16.distance(from: start, to: string.endIndex)
replaceCharacters(in: NSRange(location: location, length: length), with: "")
} }
} }

View File

@ -388,7 +388,11 @@ class ConversationMainStatusCollectionViewCell: UICollectionViewListCell, Status
let html = translation?.content ?? status.content let html = translation?.content ?? status.content
let attributedContent = ConversationMainStatusCollectionViewCell.htmlConverter.convert(html) let attributedContent = ConversationMainStatusCollectionViewCell.htmlConverter.convert(html)
doUpdateUI(status: status, content: attributedContent) let collapsedContent = NSMutableAttributedString(attributedString: attributedContent)
collapsedContent.collapseWhitespace()
collapsedContent.trimLeadingCharactersInSet(.whitespacesAndNewlines)
collapsedContent.trimTrailingCharactersInSet(.whitespacesAndNewlines)
doUpdateUI(status: status, content: collapsedContent)
if !status.spoilerText.isEmpty, if !status.spoilerText.isEmpty,
let translated = translation?.spoilerText { let translated = translation?.spoilerText {

View File

@ -616,7 +616,11 @@ class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollecti
} }
let content = precomputedContent ?? TimelineStatusCollectionViewCell.htmlConverter.convert(status.content) let content = precomputedContent ?? TimelineStatusCollectionViewCell.htmlConverter.convert(status.content)
doUpdateUI(status: status, content: content) let collapsedContent = NSMutableAttributedString(attributedString: content)
collapsedContent.collapseWhitespace()
collapsedContent.trimLeadingCharactersInSet(.whitespacesAndNewlines)
collapsedContent.trimTrailingCharactersInSet(.whitespacesAndNewlines)
doUpdateUI(status: status, content: collapsedContent)
doUpdateTimestamp(status: status) doUpdateTimestamp(status: status)
timestampLabel.isHidden = showPinned timestampLabel.isHidden = showPinned

View File

@ -17,6 +17,36 @@ class AttributedStringHelperTests: XCTestCase {
override func tearDown() { override func tearDown() {
} }
func testTrimLeading() {
let a = NSMutableAttributedString(string: " a ")
a.trimLeadingCharactersInSet(.whitespaces)
XCTAssertEqual(a, NSAttributedString(string: "a "))
let b = NSMutableAttributedString(string: " ")
b.trimLeadingCharactersInSet(.whitespaces)
XCTAssertEqual(b, NSAttributedString(string: ""))
let c = NSMutableAttributedString(string: "")
c.trimLeadingCharactersInSet(.whitespaces)
XCTAssertEqual(c, NSAttributedString(string: ""))
let d = NSMutableAttributedString(string: "abc")
d.trimLeadingCharactersInSet(.whitespaces)
XCTAssertEqual(d, NSAttributedString(string: "abc"))
}
func testTrimTrailing() {
let a = NSMutableAttributedString(string: " a ")
a.trimTrailingCharactersInSet(.whitespaces)
XCTAssertEqual(a, NSAttributedString(string: " a"))
let b = NSMutableAttributedString(string: " ")
b.trimTrailingCharactersInSet(.whitespaces)
XCTAssertEqual(b, NSAttributedString(string: ""))
let c = NSMutableAttributedString(string: "")
c.trimTrailingCharactersInSet(.whitespaces)
XCTAssertEqual(c, NSAttributedString(string: ""))
let d = NSMutableAttributedString(string: "abc")
d.trimTrailingCharactersInSet(.whitespaces)
XCTAssertEqual(d, NSAttributedString(string: "abc"))
}
func testCollapsingWhitespace() { func testCollapsingWhitespace() {
var str = NSAttributedString(string: "test 1\n") var str = NSAttributedString(string: "test 1\n")
XCTAssertEqual(str.collapsingWhitespace(), NSAttributedString(string: "test 1\n")) XCTAssertEqual(str.collapsingWhitespace(), NSAttributedString(string: "test 1\n"))