1
0
mirror of git://jb55.com/damus synced 2024-10-01 09:20:47 +00:00

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>
This commit is contained in:
kernelkind 2024-01-25 15:22:51 -05:00 committed by William Casarin
parent deedf5577d
commit 010d71d9ed
2 changed files with 20 additions and 1 deletions

View File

@ -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)
}

View File

@ -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))
})
}