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 <kernelkind@gmail.com> Reviewed-by: William Casarin <jb55@jb55.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
aa1f75ad58
commit
af75eed83a
@@ -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 {
|
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
|
post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in
|
||||||
if let link = attributes[.link] as? String {
|
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
|
let normalized_link: String
|
||||||
if link.hasPrefix("damus:nostr:") {
|
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.
|
// Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted.
|
||||||
|
|||||||
@@ -142,6 +142,28 @@ final class PostViewTests: XCTestCase {
|
|||||||
checkMentionLinkEditorHandling(content: content, replacementText: "", replacementRange: NSRange(location: 5, length: 28), shouldBeAbleToChangeAutomatically: true)
|
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(
|
func checkMentionLinkEditorHandling(
|
||||||
|
|||||||
Reference in New Issue
Block a user