damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit ff70cb7ebf37d3e48357b5d3052afd8caa70113d
parent fe82134a75b8b868d9d43a54c1ac29131bba4871
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 17 Jul 2023 10:45:05 -0700

posting: don't prepad user tag if its a newline

This fixes one more edgecase with the tag prepend logic.

Diffstat:
Mdamus/Views/Posting/UserSearch.swift | 16++++++++++++----
MdamusTests/ReplyTests.swift | 20++++++++++++++++++++
2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/damus/Views/Posting/UserSearch.swift b/damus/Views/Posting/UserSearch.swift @@ -108,9 +108,9 @@ func pad_attr_string(tag: NSAttributedString, before: Bool = true) -> NSAttribut } /// Checks if whitespace precedes a tag. Useful to add spacing if we don't have it. -func whitespace_precedes_tag(tag: NSAttributedString, post: NSMutableAttributedString, word_range: NSRange) -> Bool { +func should_prepad_tag(tag: NSAttributedString, post: NSMutableAttributedString, word_range: NSRange) -> Bool { if word_range.location == 0 { // If the range starts at the very beginning of the post, there's nothing preceding it. - return true + return false } // Range for the character preceding the tag @@ -119,8 +119,16 @@ func whitespace_precedes_tag(tag: NSAttributedString, post: NSMutableAttributedS // Get the preceding character let precedingCharacter = post.attributedSubstring(from: precedingCharacterRange) + guard let char = precedingCharacter.string.first else { + return false + } + + if char.isNewline { + return false + } + // Check if the preceding character is a whitespace character - return precedingCharacter.string.rangeOfCharacter(from: CharacterSet.whitespaces) != nil + return !char.isWhitespace } struct AppendedTag { @@ -134,7 +142,7 @@ func append_user_tag(tag: NSAttributedString, post: NSMutableAttributedString, w // If we have a non-empty post and the last character is not whitespace, append a space // This prevents issues such as users typing cc@will and have it expand to ccnostr:bech32... - let should_prepad = !whitespace_precedes_tag(tag: tag, post: post, word_range: word_range) + let should_prepad = should_prepad_tag(tag: tag, post: post, word_range: word_range) let tag = pad_attr_string(tag: tag, before: should_prepad) new_post.replaceCharacters(in: word_range, with: tag) diff --git a/damusTests/ReplyTests.swift b/damusTests/ReplyTests.swift @@ -224,6 +224,26 @@ class ReplyTests: XCTestCase { XCTAssertEqual(new_post.string, "😎 @jb55 ") } + func testComposedMentionNewline() throws { + let content = """ + + @jb55 + """ + + let profile = Profile(name: "jb55") + let tag = user_tag_attr_string(profile: profile, pubkey: "pk") + let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(1...5)) + let new_post = appended.post + + try new_post.testAttributes(conditions: [ + { let link = $0[.link] as? String; XCTAssertNil(link) }, + { let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") }, + { let link = $0[.link] as? String; XCTAssertNil(link) }, + ]) + + XCTAssertEqual(new_post.string, "\n@jb55 ") + } + func testComposedMention() throws { let content = "@jb55"