diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 2499baac..8f6fd689 100644 --- 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 index 51976cad..0cb68c00 100644 --- 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)) + }) +}