From 53260555f6f6410e095c8ad407741b9ee77092de Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 22 Feb 2024 23:53:27 -0500 Subject: [PATCH] Remove now-redundant whitespace removal --- .../Extensions/AttributedString+Helpers.swift | 63 ------------------- Tusker/HTMLConverter.swift | 2 +- .../Compose/ComposeReplyContentView.swift | 6 +- ...ersationMainStatusCollectionViewCell.swift | 6 +- .../TimelineStatusCollectionViewCell.swift | 6 +- 5 files changed, 4 insertions(+), 79 deletions(-) diff --git a/Tusker/Extensions/AttributedString+Helpers.swift b/Tusker/Extensions/AttributedString+Helpers.swift index 5dd038ba..05337730 100644 --- a/Tusker/Extensions/AttributedString+Helpers.swift +++ b/Tusker/Extensions/AttributedString+Helpers.swift @@ -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 - } - } - } diff --git a/Tusker/HTMLConverter.swift b/Tusker/HTMLConverter.swift index 1c22984a..bbcf72b4 100644 --- a/Tusker/HTMLConverter.swift +++ b/Tusker/HTMLConverter.swift @@ -23,7 +23,7 @@ class HTMLConverter { return style }() - private var converter: AttributedStringConverter + private let converter: AttributedStringConverter init(font: UIFont, monospaceFont: UIFont, color: UIColor, paragraphStyle: NSParagraphStyle) { let config = AttributedStringConverterConfiguration(font: font, monospaceFont: monospaceFont, color: color, paragraphStyle: paragraphStyle) diff --git a/Tusker/Screens/Compose/ComposeReplyContentView.swift b/Tusker/Screens/Compose/ComposeReplyContentView.swift index 9a37ee70..0df4eb21 100644 --- a/Tusker/Screens/Compose/ComposeReplyContentView.swift +++ b/Tusker/Screens/Compose/ComposeReplyContentView.swift @@ -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 } diff --git a/Tusker/Views/Status/ConversationMainStatusCollectionViewCell.swift b/Tusker/Views/Status/ConversationMainStatusCollectionViewCell.swift index 9cbcd5a3..5955b6c0 100644 --- a/Tusker/Views/Status/ConversationMainStatusCollectionViewCell.swift +++ b/Tusker/Views/Status/ConversationMainStatusCollectionViewCell.swift @@ -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 { diff --git a/Tusker/Views/Status/TimelineStatusCollectionViewCell.swift b/Tusker/Views/Status/TimelineStatusCollectionViewCell.swift index 1d6f8b8e..f2c13679 100644 --- a/Tusker/Views/Status/TimelineStatusCollectionViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusCollectionViewCell.swift @@ -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