Skip to content

Commit 9d9c55d

Browse files
authored
Handle permalinks in room topic (matrix-org#11115)
* Handle permalinks in room topic Fixes: element-hq/element-web#23395 * Add test for clicking non-link
1 parent 767cd62 commit 9d9c55d

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

src/components/views/elements/RoomTopic.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import MatrixClientContext from "../../../contexts/MatrixClientContext";
3131
import AccessibleButton from "./AccessibleButton";
3232
import TooltipTarget from "./TooltipTarget";
3333
import { Linkify, topicToHtml } from "../../../HtmlUtils";
34+
import { tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
3435

3536
interface IProps extends React.HTMLProps<HTMLDivElement> {
3637
room: Room;
@@ -46,12 +47,22 @@ export default function RoomTopic({ room, ...props }: IProps): JSX.Element {
4647
const onClick = useCallback(
4748
(e: React.MouseEvent<HTMLDivElement>) => {
4849
props.onClick?.(e);
50+
4951
const target = e.target as HTMLElement;
50-
if (target.tagName.toUpperCase() === "A") {
52+
53+
if (target.tagName.toUpperCase() !== "A") {
54+
dis.fire(Action.ShowRoomTopic);
5155
return;
5256
}
5357

54-
dis.fire(Action.ShowRoomTopic);
58+
const anchor = e.target as HTMLLinkElement;
59+
const localHref = tryTransformPermalinkToLocalHref(anchor.href);
60+
61+
if (localHref !== anchor.href) {
62+
// it could be converted to a localHref -> therefore handle locally
63+
e.preventDefault();
64+
window.location.hash = localHref;
65+
}
5566
},
5667
[props],
5768
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
import { Room } from "matrix-js-sdk/src/models/room";
19+
import { fireEvent, render, screen } from "@testing-library/react";
20+
21+
import { mkEvent, stubClient } from "../../../test-utils";
22+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
23+
import RoomTopic from "../../../../src/components/views/elements/RoomTopic";
24+
import dis from "../../../../src/dispatcher/dispatcher";
25+
import { Action } from "../../../../src/dispatcher/actions";
26+
27+
jest.mock("../../../../src/dispatcher/dispatcher");
28+
29+
describe("<RoomTopic/>", () => {
30+
const originalHref = window.location.href;
31+
32+
afterEach(() => {
33+
window.location.href = originalHref;
34+
});
35+
36+
function runClickTest(topic: string, clickText: string) {
37+
stubClient();
38+
39+
const room = new Room("!pMBteVpcoJRdCJxDmn:matrix.org", MatrixClientPeg.safeGet(), "@alice:example.org");
40+
const topicEvent = mkEvent({
41+
type: "m.room.topic",
42+
room: "!pMBteVpcoJRdCJxDmn:matrix.org",
43+
user: "@alice:example.org",
44+
content: { topic },
45+
ts: 123,
46+
event: true,
47+
});
48+
49+
room.addLiveEvents([topicEvent]);
50+
51+
render(<RoomTopic room={room} />);
52+
53+
fireEvent.click(screen.getByText(clickText));
54+
}
55+
56+
it("should capture permalink clicks", () => {
57+
const permalink =
58+
"https://matrix.to/#/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
59+
const expectedHref =
60+
"http://localhost/#/room/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
61+
runClickTest(`... ${permalink} ...`, permalink);
62+
expect(window.location.href).toEqual(expectedHref);
63+
expect(dis.fire).toHaveBeenCalledTimes(0);
64+
});
65+
66+
it("should not capture non-permalink clicks", () => {
67+
const link = "https://matrix.org";
68+
const expectedHref = originalHref;
69+
runClickTest(`... ${link} ...`, link);
70+
expect(window.location.href).toEqual(expectedHref);
71+
expect(dis.fire).toHaveBeenCalledTimes(0);
72+
});
73+
74+
it("should open topic dialog when not clicking a link", () => {
75+
const topic = "foobar";
76+
const expectedHref = originalHref;
77+
runClickTest(topic, topic);
78+
expect(window.location.href).toEqual(expectedHref);
79+
expect(dis.fire).toHaveBeenCalledWith(Action.ShowRoomTopic);
80+
});
81+
});

0 commit comments

Comments
 (0)