Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 48ae16b

Browse files
authored
Fix pillification sometimes doubling up (#9152)
* Fix pillification sometimes doubling up * Remove redundant assignment * Add unit tests around pillification * Kill ts-ignore
1 parent 147ec49 commit 48ae16b

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

src/utils/pillify.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export function pillifyLinks(nodes: ArrayLike<Element>, mxEvent: MatrixEvent, pi
4444
while (node) {
4545
let pillified = false;
4646

47-
if (node.tagName === "PRE" || node.tagName === "CODE") {
48-
// Skip code blocks
47+
if (node.tagName === "PRE" || node.tagName === "CODE" || pills.includes(node)) {
48+
// Skip code blocks and existing pills
4949
node = node.nextSibling as Element;
5050
continue;
5151
} else if (node.tagName === "A" && node.getAttribute("href")) {

test/utils/pillify-test.tsx

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2022 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 { render } from "@testing-library/react";
19+
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
20+
import { ConditionKind, EventType, PushRuleActionName, Room, TweakName } from "matrix-js-sdk/src/matrix";
21+
22+
import { pillifyLinks } from "../../src/utils/pillify";
23+
import { stubClient } from "../test-utils";
24+
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
25+
import DMRoomMap from "../../src/utils/DMRoomMap";
26+
27+
describe("pillify", () => {
28+
const roomId = "!room:id";
29+
const event = new MatrixEvent({
30+
room_id: roomId,
31+
type: EventType.RoomMessage,
32+
content: {
33+
body: "@room",
34+
},
35+
});
36+
37+
beforeEach(() => {
38+
stubClient();
39+
const cli = MatrixClientPeg.get();
40+
(cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId()));
41+
cli.pushRules.global = {
42+
override: [
43+
{
44+
rule_id: ".m.rule.roomnotif",
45+
default: true,
46+
enabled: true,
47+
conditions: [{
48+
kind: ConditionKind.EventMatch,
49+
key: "content.body",
50+
pattern: "@room",
51+
}],
52+
actions: [
53+
PushRuleActionName.Notify,
54+
{
55+
set_tweak: TweakName.Highlight,
56+
value: true,
57+
},
58+
],
59+
},
60+
],
61+
};
62+
63+
DMRoomMap.makeShared();
64+
});
65+
66+
it("should do nothing for empty element", () => {
67+
const { container } = render(<div />);
68+
const originalHtml = container.outerHTML;
69+
const containers: Element[] = [];
70+
pillifyLinks([container], event, containers);
71+
expect(containers).toHaveLength(0);
72+
expect(container.outerHTML).toEqual(originalHtml);
73+
});
74+
75+
it("should pillify @room", () => {
76+
const { container } = render(<div>@room</div>);
77+
const containers: Element[] = [];
78+
pillifyLinks([container], event, containers);
79+
expect(containers).toHaveLength(1);
80+
expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room");
81+
});
82+
83+
it("should not double up pillification on repeated calls", () => {
84+
const { container } = render(<div>@room</div>);
85+
const containers: Element[] = [];
86+
pillifyLinks([container], event, containers);
87+
pillifyLinks([container], event, containers);
88+
pillifyLinks([container], event, containers);
89+
pillifyLinks([container], event, containers);
90+
expect(containers).toHaveLength(1);
91+
expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room");
92+
});
93+
});

0 commit comments

Comments
 (0)