damus

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

commit 010d71d9ed6b6bc93e158121cd0b7544e9767a8f
parent deedf5577d5bdfeef934b930e8dbd333bb06c84f
Author: kernelkind <kernelkind@gmail.com>
Date:   Thu, 25 Jan 2024 15:22:51 -0500

mentions: fix regression on char handling after mention

Previously, when a post is being sent, the next character after a
mention was checked. If it wasn't a space, a space was added. This
implementation was insufficient because there are times where a
character immediately following a mention is desired, especially for
punctuation.

This was possible in the past because the C code counts anything non
alphanumeric as being part of the mention if it starts with 'nostr:'.

The fix included follows similar logic. If the character following the
mention is non alphanumeric, it allows it. Otherwise, a space is added
so that the C code can correctly parse the mention.

A test was added which verifies non alphanumeric characters after
mentions don't get whitespace added before them.

Fixes: af75eed ("mention: fix broken mentions when there is text is directly after")
Closes: https://github.com/damus-io/damus/issues/1915
Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Views/PostView.swift | 5++++-
MdamusTests/PostViewTests.swift | 16++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -613,6 +613,9 @@ func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts? } } +private func isAlphanumeric(_ char: Character) -> Bool { + return char.isLetter || char.isNumber +} func build_post(state: DamusState, post: NSMutableAttributedString, action: PostAction, uploadedMedias: [UploadedMedia], references: [RefId]) -> NostrPost { post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in @@ -620,7 +623,7 @@ func build_post(state: DamusState, post: NSMutableAttributedString, action: Post let nextCharIndex = range.upperBound if nextCharIndex < post.length, let nextChar = post.attributedSubstring(from: NSRange(location: nextCharIndex, length: 1)).string.first, - !nextChar.isWhitespace { + isAlphanumeric(nextChar) { post.insert(NSAttributedString(string: " "), at: nextCharIndex) } diff --git a/damusTests/PostViewTests.swift b/damusTests/PostViewTests.swift @@ -164,6 +164,12 @@ final class PostViewTests: XCTestCase { XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil)) }) } + + func testMentionLinkEditorHandling_nonAlphanumericAfterLink_noWhitespaceAdded() { + let nonAlphaNumerics = [" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`", "{", "|", "}", "~"] + + nonAlphaNumerics.forEach { testAddingStringAfterLink(str: $0)} + } } func checkMentionLinkEditorHandling( @@ -189,5 +195,15 @@ func checkMentionLinkEditorHandling( XCTAssertEqual(coordinator.textView(textView, shouldChangeTextIn: replacementRange, replacementText: replacementText), shouldBeAbleToChangeAutomatically, "Expected shouldChangeTextIn to return \(shouldBeAbleToChangeAutomatically), but was \(!shouldBeAbleToChangeAutomatically)") } +func testAddingStringAfterLink(str: String) { + var content: NSMutableAttributedString + + content = NSMutableAttributedString(string: "Hello @user test") + content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5)) + checkMentionLinkEditorHandling(content: content, replacementText: str, replacementRange: NSRange(location: 11, length: 0), shouldBeAbleToChangeAutomatically: false, expectedNewCursorIndex: 12, handleNewContent: { newManuallyEditedContent in + XCTAssertEqual(newManuallyEditedContent.string, "Hello @user" + str + " test") + XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil)) + }) +}