1
0
mirror of git://jb55.com/damus synced 2024-09-16 02:03:45 +00:00

posting: don't prepad user tag if its a newline

This fixes one more edgecase with the tag prepend logic.
This commit is contained in:
William Casarin 2023-07-17 10:45:05 -07:00
parent fe82134a75
commit ff70cb7ebf
2 changed files with 32 additions and 4 deletions

View File

@ -108,9 +108,9 @@ func pad_attr_string(tag: NSAttributedString, before: Bool = true) -> NSAttribut
}
/// Checks if whitespace precedes a tag. Useful to add spacing if we don't have it.
func whitespace_precedes_tag(tag: NSAttributedString, post: NSMutableAttributedString, word_range: NSRange) -> Bool {
func should_prepad_tag(tag: NSAttributedString, post: NSMutableAttributedString, word_range: NSRange) -> Bool {
if word_range.location == 0 { // If the range starts at the very beginning of the post, there's nothing preceding it.
return true
return false
}
// Range for the character preceding the tag
@ -119,8 +119,16 @@ func whitespace_precedes_tag(tag: NSAttributedString, post: NSMutableAttributedS
// Get the preceding character
let precedingCharacter = post.attributedSubstring(from: precedingCharacterRange)
guard let char = precedingCharacter.string.first else {
return false
}
if char.isNewline {
return false
}
// Check if the preceding character is a whitespace character
return precedingCharacter.string.rangeOfCharacter(from: CharacterSet.whitespaces) != nil
return !char.isWhitespace
}
struct AppendedTag {
@ -134,7 +142,7 @@ func append_user_tag(tag: NSAttributedString, post: NSMutableAttributedString, w
// If we have a non-empty post and the last character is not whitespace, append a space
// This prevents issues such as users typing cc@will and have it expand to ccnostr:bech32...
let should_prepad = !whitespace_precedes_tag(tag: tag, post: post, word_range: word_range)
let should_prepad = should_prepad_tag(tag: tag, post: post, word_range: word_range)
let tag = pad_attr_string(tag: tag, before: should_prepad)
new_post.replaceCharacters(in: word_range, with: tag)

View File

@ -224,6 +224,26 @@ class ReplyTests: XCTestCase {
XCTAssertEqual(new_post.string, "😎 @jb55 ")
}
func testComposedMentionNewline() throws {
let content = """
@jb55
"""
let profile = Profile(name: "jb55")
let tag = user_tag_attr_string(profile: profile, pubkey: "pk")
let appended = append_user_tag(tag: tag, post: .init(string: content), word_range: .init(1...5))
let new_post = appended.post
try new_post.testAttributes(conditions: [
{ let link = $0[.link] as? String; XCTAssertNil(link) },
{ let link = $0[.link] as! String; XCTAssertEqual(link, "damus:nostr:pk") },
{ let link = $0[.link] as? String; XCTAssertNil(link) },
])
XCTAssertEqual(new_post.string, "\n@jb55 ")
}
func testComposedMention() throws {
let content = "@jb55"