eleven / messenger Public

ios+mac: emphasis markers stay out of hrefs — the RichText twin of PR #530 (#532)

The Swift tokenizer's trailingRE had the identical gap as format.js: a bot's
"**github.com/apps/x**" swallowed the closing ** into the link. Same one-class
fix, same regression test as the web twin.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Paul Campbell c359762e4242ab9c48b57114cc44b9371bfa9180 parent 568066d
2 files changed, +18 −1
  • ios/LChatCore/Sources/LChatCore/RichText.swift +3 −1
  • ios/LChatCore/Tests/LChatCoreTests/RichTextTests.swift +15 −0
diff --git a/ios/LChatCore/Sources/LChatCore/RichText.swift b/ios/LChatCore/Sources/LChatCore/RichText.swift
index 9718ac8..319d4ac 100644
--- a/ios/LChatCore/Sources/LChatCore/RichText.swift
+++ b/ios/LChatCore/Sources/LChatCore/RichText.swift
@@ -21,7 +21,9 @@ public enum RichToken: Equatable {
private let urlRE = try! NSRegularExpression(
pattern: "https?://[^\\s<]+|www\\.[^\\s<]+|[a-z0-9][a-z0-9-]*(?:\\.[a-z0-9-]+)+(?:/[^\\s<]*)?",
options: [.caseInsensitive])
-private let trailingRE = try! NSRegularExpression(pattern: "[.,!?;:'\")\\]}]+$")
+// Emphasis markers join the strip (format.js twin): "**github.com/apps/x**"
+// must not get the closing ** swallowed into the href.
+private let trailingRE = try! NSRegularExpression(pattern: "[.,!?;:'\")\\]}*_~`]+$")
private let formatRE = try! NSRegularExpression(
pattern: "`([^`\\n]+)`|\\*\\*\\*([^*\\n]+)\\*\\*\\*|\\*\\*([^*\\n]+)\\*\\*|\\*([^*\\n]+)\\*|(?<![\\w])_([^_\\n]+)_(?![\\w])|~([^~\\n]+)~")
diff --git a/ios/LChatCore/Tests/LChatCoreTests/RichTextTests.swift b/ios/LChatCore/Tests/LChatCoreTests/RichTextTests.swift
index 65511b1..77c5db0 100644
--- a/ios/LChatCore/Tests/LChatCoreTests/RichTextTests.swift
+++ b/ios/LChatCore/Tests/LChatCoreTests/RichTextTests.swift
@@ -156,4 +156,19 @@ final class RichTextTests: XCTestCase {
XCTAssertEqual(emojiOnlyCount("plain text"), 0)
XCTAssertEqual(emojiOnlyCount(""), 0)
}
+
+ // The web twin's regression (format.test.mjs): emphasis markers around a
+ // URL must never ride into the href — "**github.com/apps/x**" gets a
+ // clean link, stars left as literal text.
+ func testEmphasisMarkersStayOutOfHrefs() {
+ let toks = tokenizeText("go to **github.com/apps/eleven-messenger** and install")
+ let link = toks.first(where: { $0.href != nil })
+ XCTAssertEqual(link?.text, "github.com/apps/eleven-messenger")
+ XCTAssertEqual(link?.href?.absoluteString.contains("*"), false)
+ XCTAssertEqual(toks.map(\.text).joined(), "go to **github.com/apps/eleven-messenger** and install")
+
+ let toks2 = tokenizeText("see https://docs.github.com/rest/issues#list**")
+ let link2 = toks2.first(where: { $0.href != nil })
+ XCTAssertEqual(link2?.text, "https://docs.github.com/rest/issues#list")
+ }
}