From af75eed83a2a1dd0eb33a0a27ded71c9f44dacbd Mon Sep 17 00:00:00 2001 From: kernelkind Date: Thu, 18 Jan 2024 17:56:32 -0500 Subject: [PATCH] mention: fix broken mentions when there is text is directly after If a user adds text right after a mention without a space, a space gets added so the mention can be parsed correctly after posting. Closes: https://github.com/damus-io/damus/issues/1872 Changelog-Fixed: Fix broken mentions when there is text is directly after Lightning-url: LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G Signed-off-by: kernelkind Reviewed-by: William Casarin Signed-off-by: William Casarin --- damus/Views/PostView.swift | 7 +++++++ damusTests/PostViewTests.swift | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 8fabf0fe..2499baac 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -617,6 +617,13 @@ func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts? 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 if let link = attributes[.link] as? String { + let nextCharIndex = range.upperBound + if nextCharIndex < post.length, + let nextChar = post.attributedSubstring(from: NSRange(location: nextCharIndex, length: 1)).string.first, + !nextChar.isWhitespace { + post.insert(NSAttributedString(string: " "), at: nextCharIndex) + } + let normalized_link: String if link.hasPrefix("damus:nostr:") { // Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted. diff --git a/damusTests/PostViewTests.swift b/damusTests/PostViewTests.swift index ae78c3e6..51976cad 100644 --- a/damusTests/PostViewTests.swift +++ b/damusTests/PostViewTests.swift @@ -142,6 +142,28 @@ final class PostViewTests: XCTestCase { checkMentionLinkEditorHandling(content: content, replacementText: "", replacementRange: NSRange(location: 5, length: 28), shouldBeAbleToChangeAutomatically: true) } + + func testMentionLinkEditorHandling_noWhitespaceAfterLink1_addsWhitespace() { + var content: NSMutableAttributedString + + content = NSMutableAttributedString(string: "Hello @user ") + content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5)) + checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in + XCTAssertEqual(newManuallyEditedContent.string, "Hello @user up") + XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil)) + }) + } + + func testMentionLinkEditorHandling_noWhitespaceAfterLink2_addsWhitespace() { + 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: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in + XCTAssertEqual(newManuallyEditedContent.string, "Hello @user uptest") + XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil)) + }) + } } func checkMentionLinkEditorHandling(