Keep the original URL when deserialising markdown links (#34488)

HTMLAnchorElement.href is the resolved, serialised URL, so editing a message
containing [a](http://example.com) put [a](http://example.com/) in the composer
and re-wrapped a plain autolinked URL as a markdown link. Read the href
attribute instead so the composer shows what the author wrote.

Co-authored-by: David Baker <dbkr@users.noreply.github.com>
This commit is contained in:
hayyaksi
2026-08-12 14:27:05 +03:00
committed by GitHub
parent 03faca3626
commit 45d0d336e1
2 changed files with 22 additions and 1 deletions
+4 -1
View File
@@ -64,7 +64,10 @@ function parseAtRoomMentions(text: string, pc: PartCreator, opts: IParseOptions)
}
function parseLink(n: Node, pc: PartCreator, opts: IParseOptions): Part[] {
const { href } = n as HTMLAnchorElement;
// Read the attribute rather than the `href` property: the property is the resolved, serialised
// URL, so it appends a trailing slash to an origin-only link and absolutises a relative one.
// The composer should show the author what they wrote.
const href = (n as HTMLAnchorElement).getAttribute("href") ?? "";
const resourceId = getPrimaryPermalinkEntity(href); // The room/user ID
switch (resourceId?.[0]) {
@@ -109,6 +109,24 @@ describe("editor/deserialize", function () {
expect(parts.length).toBe(1);
expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](http://example.com/)!" });
});
it("hyperlink without a trailing slash keeps the original URL", function () {
const html = 'click <a href="http://example.com">this</a>!';
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
expect(parts.length).toBe(1);
expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](http://example.com)!" });
});
it("hyperlink with a relative URL keeps the original URL", function () {
const html = 'click <a href="/local/path">this</a>!';
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
expect(parts.length).toBe(1);
expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](/local/path)!" });
});
it("autolinked URL is not rewritten as a markdown link", function () {
const html = '<a href="http://example.com">http://example.com</a>';
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
expect(parts.length).toBe(1);
expect(parts[0]).toStrictEqual({ type: "plain", text: "http://example.com" });
});
it("multiple lines with paragraphs", function () {
const html = "<p>hello</p><p>world</p>";
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));