Remove now-redundant whitespace removal

This commit is contained in:
Shadowfacts 2024-02-22 23:53:27 -05:00
parent 70524dd642
commit 53260555f6
5 changed files with 4 additions and 79 deletions

View File

@ -8,23 +8,12 @@
import Foundation
private let ASCII_NEWLINE: unichar = 10
private let ASCII_SPACE: unichar = 32
extension NSAttributedString {
var fullRange: NSRange {
return NSRange(location: 0, length: self.length)
}
/// Creates a new string with the whitespace collapsed according to the CSS Text Module Level 3 rules.
/// See https://www.w3.org/TR/css-text-3/#white-space-phase-1
func collapsingWhitespace() -> NSAttributedString {
let mut = NSMutableAttributedString(attributedString: self)
mut.collapseWhitespace()
return mut
}
}
extension NSMutableAttributedString {
@ -58,56 +47,4 @@ extension NSMutableAttributedString {
}
}
/// Collapses whitespace in this string according to the CSS Text Module Level 3 rules.
/// See https://www.w3.org/TR/css-text-3/#white-space-phase-1
func collapseWhitespace() {
let str = self.mutableString
var i = 0
while i < str.length {
if str.character(at: i) == ASCII_NEWLINE {
var j: Int
if i > 0 {
// scan backwards to find beginning of space characters preceeding newline
j = i - 1
while j >= 0 {
if str.character(at: j) != ASCII_SPACE {
break
}
j -= 1
}
// add one after loop completes because start of range is _inclusive_
j += 1
} else {
j = 0
}
var k: Int
if i < str.length - 1 {
// scan forwards to find end of space characters following newline
k = i + 1
while k < str.length {
if str.character(at: k) != ASCII_SPACE {
break
}
k += 1
}
// don't need to subtract one before breaking out of loop, because end of range is _exclusive_
} else {
// range end is _exclusive_, so use whole string length that way last character is included
k = str.length
}
// if there's only one character to be replaced, that means we'd be replacing the newline with a newline, so don't bother
if k - j > 1 {
str.replaceCharacters(in: NSRange(location: j, length: k - j), with: "\n")
// continue scanning through the string starting after the newline we just inserted
i = j
}
}
i += 1
}
}
}

View File

@ -23,7 +23,7 @@ class HTMLConverter {
return style
}()
private var converter: AttributedStringConverter<Callbacks>
private let converter: AttributedStringConverter<Callbacks>
init(font: UIFont, monospaceFont: UIFont, color: UIColor, paragraphStyle: NSParagraphStyle) {
let config = AttributedStringConverterConfiguration(font: font, monospaceFont: monospaceFont, color: color, paragraphStyle: paragraphStyle)

View File

@ -29,11 +29,7 @@ struct ComposeReplyContentView: UIViewRepresentable {
view.overrideMastodonController = mastodonController
let content = TimelineStatusCollectionViewCell.htmlConverter.convert(status.content)
let collapsedContent = NSMutableAttributedString(attributedString: content)
collapsedContent.collapseWhitespace()
collapsedContent.trimLeadingCharactersInSet(.whitespacesAndNewlines)
collapsedContent.trimTrailingCharactersInSet(.whitespacesAndNewlines)
view.attributedText = collapsedContent
view.attributedText = content
return view
}

View File

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

View File

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