2021-11-26 10:39:07 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-11-26 10:39:07 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-11-26 10:39:07 +00:00
|
|
|
*/
|
2023-07-14 10:55:55 +00:00
|
|
|
|
|
|
|
import { EventListeners } from "linkifyjs";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { linkify, Type, options } from "../../src/linkify-matrix";
|
|
|
|
import dispatcher from "../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../src/dispatcher/actions";
|
2021-11-26 10:39:07 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("linkify-matrix", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const linkTypesByInitialCharacter: Record<string, string> = {
|
2022-12-12 11:24:14 +00:00
|
|
|
"#": "roomalias",
|
|
|
|
"@": "userid",
|
2022-01-18 17:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param testName Due to all the tests using the same logic underneath, it makes to generate it in a bit smarter way
|
|
|
|
* @param char
|
|
|
|
*/
|
2022-12-12 11:24:14 +00:00
|
|
|
function genTests(char: "#" | "@" | "+") {
|
2022-01-18 17:24:16 +00:00
|
|
|
const type = linkTypesByInitialCharacter[char];
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should not parse " + char + "foo without domain", () => {
|
2022-01-18 17:24:16 +00:00
|
|
|
const test = char + "foo";
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([]);
|
2022-01-18 17:24:16 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("ip v4 tests", () => {
|
|
|
|
it("should properly parse IPs v4 as the domain name", () => {
|
|
|
|
const test = char + "potato:1.2.3.4";
|
2022-01-18 17:24:16 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "potato:1.2.3.4",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + "potato:1.2.3.4",
|
|
|
|
},
|
|
|
|
]);
|
2022-01-18 17:24:16 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should properly parse IPs v4 with port as the domain name with attached", () => {
|
|
|
|
const test = char + "potato:1.2.3.4:1337";
|
2022-01-18 17:24:16 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "potato:1.2.3.4:1337",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + "potato:1.2.3.4:1337",
|
|
|
|
},
|
|
|
|
]);
|
2022-01-18 17:24:16 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should properly parse IPs v4 as the domain name while ignoring missing port", () => {
|
|
|
|
const test = char + "potato:1.2.3.4:";
|
2022-01-18 17:24:16 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "potato:1.2.3.4",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length - 1,
|
|
|
|
value: char + "potato:1.2.3.4",
|
|
|
|
},
|
|
|
|
]);
|
2022-01-18 17:24:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// Currently those tests are failing, as there's missing implementation.
|
2022-12-12 11:24:14 +00:00
|
|
|
describe.skip("ip v6 tests", () => {
|
|
|
|
it("should properly parse IPs v6 as the domain name", () => {
|
2022-01-18 17:24:16 +00:00
|
|
|
const test = char + "username:[1234:5678::abcd]";
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "username:[1234:5678::abcd]",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + "username:[1234:5678::abcd]",
|
|
|
|
},
|
2022-01-18 17:24:16 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should properly parse IPs v6 with port as the domain name", () => {
|
2022-01-18 17:24:16 +00:00
|
|
|
const test = char + "username:[1234:5678::abcd]:1337";
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "username:[1234:5678::abcd]:1337",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
value: char + "username:[1234:5678::abcd]:1337",
|
|
|
|
},
|
2022-01-18 17:24:16 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line max-len
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should properly parse IPs v6 while ignoring dangling comma when without port name as the domain name", () => {
|
2022-01-18 17:24:16 +00:00
|
|
|
const test = char + "username:[1234:5678::abcd]:";
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "username:[1234:5678::abcd]:",
|
|
|
|
type,
|
|
|
|
isLink: true,
|
|
|
|
start: 0,
|
|
|
|
end: test.length - 1,
|
|
|
|
value: char + "username:[1234:5678::abcd]:",
|
|
|
|
},
|
2022-01-18 17:24:16 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("properly parses " + char + "_foonetic_xkcd:matrix.org", () => {
|
|
|
|
const test = "" + char + "_foonetic_xkcd:matrix.org";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "_foonetic_xkcd:matrix.org",
|
|
|
|
type,
|
|
|
|
value: char + "_foonetic_xkcd:matrix.org",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2023-06-27 11:23:37 +00:00
|
|
|
it("properly parses " + char + "localhost:foo.com", () => {
|
|
|
|
const test = char + "localhost:foo.com";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "localhost:foo.com",
|
|
|
|
type,
|
|
|
|
value: char + "localhost:foo.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("properly parses " + char + "foo:localhost", () => {
|
2022-01-18 17:24:16 +00:00
|
|
|
const test = char + "foo:localhost";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:localhost",
|
|
|
|
type,
|
|
|
|
value: char + "foo:localhost",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accept " + char + "foo:bar.com", () => {
|
|
|
|
const test = "" + char + "foo:bar.com";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accept " + char + "foo:com (mostly for (TLD|DOMAIN)+ mixing)", () => {
|
|
|
|
const test = "" + char + "foo:com";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accept repeated TLDs (e.g .org.uk)", () => {
|
|
|
|
const test = "" + char + "foo:bar.org.uk";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.org.uk",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.org.uk",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accept hyphens in name " + char + "foo-bar:server.com", () => {
|
|
|
|
const test = "" + char + "foo-bar:server.com";
|
2022-01-20 17:18:47 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo-bar:server.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo-bar:server.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2022-01-20 17:18:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores trailing `:`", () => {
|
|
|
|
const test = "" + char + "foo:bar.com:";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
href: char + "foo:bar.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length - ":".length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accept :NUM (port specifier)", () => {
|
|
|
|
const test = "" + char + "foo:bar.com:2225";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.com:2225",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com:2225",
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2023-06-27 11:23:37 +00:00
|
|
|
it("ignores duplicate :NUM (double port specifier)", () => {
|
|
|
|
const test = "" + char + "foo:bar.com:2225:1234";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.com:2225",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com:2225",
|
|
|
|
start: 0,
|
|
|
|
end: 17,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores all the trailing :", () => {
|
|
|
|
const test = "" + char + "foo:bar.com::::";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com",
|
|
|
|
end: test.length - 4,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("properly parses room alias with dots in name", () => {
|
|
|
|
const test = "" + char + "foo.asdf:bar.com::::";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo.asdf:bar.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo.asdf:bar.com",
|
|
|
|
start: 0,
|
|
|
|
end: test.length - ":".repeat(4).length,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not parse room alias with too many separators", () => {
|
|
|
|
const test = "" + char + "foo:::bar.com";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: "http://bar.com",
|
|
|
|
type: "url",
|
|
|
|
value: "bar.com",
|
|
|
|
isLink: true,
|
|
|
|
start: 7,
|
|
|
|
end: test.length,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2023-06-27 11:23:37 +00:00
|
|
|
it("properly parses room alias with hyphen in domain part", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const test = "" + char + "foo:bar.com-baz.com";
|
2021-11-26 10:39:07 +00:00
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: char + "foo:bar.com-baz.com",
|
|
|
|
type,
|
|
|
|
value: char + "foo:bar.com-baz.com",
|
|
|
|
end: 20,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-01-18 17:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("roomalias plugin", () => {
|
|
|
|
genTests("#");
|
2023-07-14 10:55:55 +00:00
|
|
|
|
|
|
|
it("should intercept clicks with a ViewRoom dispatch", () => {
|
|
|
|
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
|
|
|
|
|
|
|
|
const handlers = (options.events as (href: string, type: string) => EventListeners)(
|
|
|
|
"#room:server.com",
|
|
|
|
"roomalias",
|
|
|
|
);
|
|
|
|
|
|
|
|
const event = new MouseEvent("mousedown");
|
|
|
|
event.preventDefault = jest.fn();
|
2024-11-26 18:11:39 +00:00
|
|
|
handlers!.click(event);
|
2023-07-14 10:55:55 +00:00
|
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_alias: "#room:server.com",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("userid plugin", () => {
|
|
|
|
genTests("@");
|
2023-05-09 07:46:19 +00:00
|
|
|
|
|
|
|
it("allows dots in localparts", () => {
|
|
|
|
const test = "@test.:matrix.org";
|
|
|
|
const found = linkify.find(test);
|
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: test,
|
|
|
|
type: "userid",
|
|
|
|
value: test,
|
|
|
|
start: 0,
|
|
|
|
end: test.length,
|
|
|
|
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2023-07-14 10:55:55 +00:00
|
|
|
|
|
|
|
it("should intercept clicks with a ViewUser dispatch", () => {
|
|
|
|
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
|
|
|
|
|
|
|
|
const handlers = (options.events as (href: string, type: string) => EventListeners)(
|
|
|
|
"@localpart:server.com",
|
|
|
|
"userid",
|
|
|
|
);
|
|
|
|
|
|
|
|
const event = new MouseEvent("mousedown");
|
|
|
|
event.preventDefault = jest.fn();
|
2024-11-26 18:11:39 +00:00
|
|
|
handlers!.click(event);
|
2023-07-14 10:55:55 +00:00
|
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
action: Action.ViewUser,
|
|
|
|
member: expect.objectContaining({
|
|
|
|
userId: "@localpart:server.com",
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|
2022-01-20 17:18:47 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("matrix uri", () => {
|
2022-01-25 17:37:54 +00:00
|
|
|
const acceptedMatrixUris = [
|
2022-12-12 11:24:14 +00:00
|
|
|
"matrix:u/foo_bar:server.uk",
|
|
|
|
"matrix:r/foo-bar:server.uk",
|
|
|
|
"matrix:roomid/somewhere:example.org?via=elsewhere.ca",
|
|
|
|
"matrix:r/somewhere:example.org",
|
|
|
|
"matrix:r/somewhere:example.org/e/event",
|
|
|
|
"matrix:roomid/somewhere:example.org/e/event?via=elsewhere.ca",
|
|
|
|
"matrix:u/alice:example.org?action=chat",
|
2022-01-20 17:18:47 +00:00
|
|
|
];
|
2022-01-25 17:37:54 +00:00
|
|
|
for (const matrixUri of acceptedMatrixUris) {
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accepts " + matrixUri, () => {
|
2022-01-20 17:18:47 +00:00
|
|
|
const test = matrixUri;
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: matrixUri,
|
|
|
|
type: Type.URL,
|
|
|
|
value: matrixUri,
|
|
|
|
end: matrixUri.length,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2022-01-20 17:18:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-01-25 17:37:54 +00:00
|
|
|
|
|
|
|
describe("matrix-prefixed domains", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const acceptedDomains = ["matrix.org", "matrix.to", "matrix-help.org", "matrix123.org"];
|
2022-01-25 17:37:54 +00:00
|
|
|
for (const domain of acceptedDomains) {
|
2022-12-12 11:24:14 +00:00
|
|
|
it("accepts " + domain, () => {
|
2022-01-25 17:37:54 +00:00
|
|
|
const test = domain;
|
|
|
|
const found = linkify.find(test);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(found).toEqual([
|
|
|
|
{
|
|
|
|
href: `http://${domain}`,
|
|
|
|
type: Type.URL,
|
|
|
|
value: domain,
|
|
|
|
end: domain.length,
|
|
|
|
start: 0,
|
|
|
|
isLink: true,
|
|
|
|
},
|
|
|
|
]);
|
2022-01-25 17:37:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-11-26 10:39:07 +00:00
|
|
|
});
|