git » sdk » commit c8645b9

Don't expand bare domains

author Stephen Paul Weber
2026-04-12 20:58:12 UTC
committer Stephen Paul Weber
2026-04-12 20:58:12 UTC
parent d956639d5542c776c4c7f60d7bda006d46b7806a

Don't expand bare domains

It can mess up text with internal autolinked bits in weird ways

borogove/XEP0393.hx +2 -3
test/TestChatMessage.hx +19 -1
test/TestChatMessageBuilder.hx +1 -1
test/TestXEP0393.hx +7 -0

diff --git a/borogove/XEP0393.hx b/borogove/XEP0393.hx
index a8beac7..0ba958f 100644
--- a/borogove/XEP0393.hx
+++ b/borogove/XEP0393.hx
@@ -75,9 +75,8 @@ class XEP0393 {
 				endsWithNewline = rendered.endsWith("\n");
 			}
 			final text = textBuf.toString();
-			if (text == href || href.endsWith(text)) {
-				return hasOpenBracket ? href : '<$href>';
-			}
+			if (text == href) return hasOpenBracket ? href : '<$href>';
+			if (href.endsWith(text)) return text;
 			return '$text <$href>';
 		}
 
diff --git a/test/TestChatMessage.hx b/test/TestChatMessage.hx
index dfea59b..d3feba9 100644
--- a/test/TestChatMessage.hx
+++ b/test/TestChatMessage.hx
@@ -82,7 +82,7 @@ class TestChatMessage extends utest.Test {
 	}
 
 
-	public function testStyledBodyWithLink() {
+	public function testStyledBodyWithLinkBeaks() {
 		final stanza = new Stanza("message");
 		stanza.attr.set("id", "test-id-1");
 		stanza.attr.set("from", "alice@example.com");
@@ -99,4 +99,22 @@ class TestChatMessage extends utest.Test {
 				Assert.fail("Expected ChatMessageStanza");
 		}
 	}
+
+	public function testStyledBodyWithLink() {
+		final stanza = new Stanza("message");
+		stanza.attr.set("id", "test-id-1");
+		stanza.attr.set("from", "alice@example.com");
+		stanza.attr.set("to", "bob@example.com");
+		stanza.attr.set("type", "chat");
+		stanza.addChild(new Stanza("body").text("Hey example.com"));
+
+		final msg = Message.fromStanza(stanza, JID.parse("bob@example.com"));
+		switch (msg.parsed) {
+			case ChatMessageStanza(m):
+				Assert.equals("<div>Hey <a href=\"https://example.com\">example.com</a></div>", m.body().toString());
+				Assert.equals("Hey example.com", m.body().toPlainText());
+			default:
+				Assert.fail("Expected ChatMessageStanza");
+		}
+	}
 }
diff --git a/test/TestChatMessageBuilder.hx b/test/TestChatMessageBuilder.hx
index 7b4baaa..0f74653 100644
--- a/test/TestChatMessageBuilder.hx
+++ b/test/TestChatMessageBuilder.hx
@@ -48,7 +48,7 @@ class TestChatMessageBuilder extends utest.Test {
 		final msg = new ChatMessageBuilder();
 		msg.setBody(Html.fromString("hello <a href='https://www.example.com/test'>example.com/test</a>"));
 		Assert.equals(
-			"hello <https://www.example.com/test>",
+			"hello example.com/test",
 			msg.text
 		);
 	}
diff --git a/test/TestXEP0393.hx b/test/TestXEP0393.hx
index f089556..fd0e853 100644
--- a/test/TestXEP0393.hx
+++ b/test/TestXEP0393.hx
@@ -245,6 +245,13 @@ Who?")
 		);
 	}
 
+	public function testAutolinkBareDomain() {
+		Assert.equals(
+			"<div><a href=\"https://example.com\">example.com</a></div>",
+			toHtml("example.com")
+		);
+	}
+
 	public function testAutolinkNoTrailingHash() {
 		Assert.equals(
 			"<div><a href=\"https://example.com/test#\">https://example.com/test#</a> a</div>",