Parse matrix-schemed URIs (#7453)

Co-authored-by: J. Ryan Stinnett <jryans@gmail.com>
Co-authored-by: Dariusz Niemczyk <dariuszn@element.io>
Co-authored-by: Timo K <toger5@hotmail.de>

With this pr all href use matrix matrix.to links. As a consequence right-click copy link will always return get you a sharable matrix.to link.
This commit is contained in:
Travis Ralston
2022-01-20 18:18:47 +01:00
committed by GitHub
parent f59ea6d7ad
commit 6712a5b1c5
12 changed files with 254 additions and 100 deletions
@@ -245,7 +245,7 @@ describe("<TextualBody />", () => {
const content = wrapper.find(".mx_EventTile_body");
expect(content.html()).toBe(
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
'An <a href="#/room/!ZxbRYPQXDXKGmDnJNg:example.com/' +
'An <a href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com/' +
'$16085560162aNpaH:example.com?via=example.com" ' +
'rel="noreferrer noopener">event link</a> with text</span>',
);
@@ -274,7 +274,8 @@ describe("<TextualBody />", () => {
const content = wrapper.find(".mx_EventTile_body");
expect(content.html()).toBe(
'<span class="mx_EventTile_body markdown-body" dir="auto">' +
'A <span><a class="mx_Pill mx_RoomPill" href="#/room/!ZxbRYPQXDXKGmDnJNg:example.com' +
'A <span><a class="mx_Pill mx_RoomPill" ' +
'href="https://matrix.to/#/!ZxbRYPQXDXKGmDnJNg:example.com' +
'?via=example.com&amp;via=bob.com"' +
'><img class="mx_BaseAvatar mx_BaseAvatar_image" ' +
'src="mxc://avatar.url/room.png" ' +
@@ -33,7 +33,7 @@ import { createPartCreator, createRenderer } from "../../../editor/mock";
import { createTestClient, mkEvent, mkStubRoom } from "../../../test-utils";
import BasicMessageComposer from "../../../../src/components/views/rooms/BasicMessageComposer";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import SpecPermalinkConstructor from "../../../../src/utils/permalinks/SpecPermalinkConstructor";
import MatrixToPermalinkConstructor from "../../../../src/utils/permalinks/MatrixToPermalinkConstructor";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
import DocumentOffset from '../../../../src/editor/offset';
import { Layout } from '../../../../src/settings/enums/Layout';
@@ -166,7 +166,7 @@ describe('<SendMessageComposer/>', () => {
<SendMessageComposer
room={mockRoom as any}
placeholder="placeholder string"
permalinkCreator={new SpecPermalinkConstructor() as any}
permalinkCreator={new MatrixToPermalinkConstructor() as any}
/>
</RoomContext.Provider>
</MatrixClientContext.Provider>);
@@ -188,7 +188,7 @@ describe('<SendMessageComposer/>', () => {
<SendMessageComposer
room={mockRoom as any}
placeholder=""
permalinkCreator={new SpecPermalinkConstructor() as any}
permalinkCreator={new MatrixToPermalinkConstructor() as any}
replyToEvent={mockEvent}
/>
</RoomContext.Provider>
@@ -234,7 +234,7 @@ describe('<SendMessageComposer/>', () => {
<SendMessageComposer
room={mockRoom as any}
placeholder=""
permalinkCreator={new SpecPermalinkConstructor() as any}
permalinkCreator={new MatrixToPermalinkConstructor() as any}
/>
</RoomContext.Provider>
</MatrixClientContext.Provider>);
@@ -263,7 +263,7 @@ describe('<SendMessageComposer/>', () => {
<SendMessageComposer
room={mockRoom as any}
placeholder="placeholder"
permalinkCreator={new SpecPermalinkConstructor() as any}
permalinkCreator={new MatrixToPermalinkConstructor() as any}
replyToEvent={mockEvent}
/>
</RoomContext.Provider>
@@ -297,7 +297,7 @@ describe('<SendMessageComposer/>', () => {
<SendMessageComposer
room={mockRoom as any}
placeholder=""
permalinkCreator={new SpecPermalinkConstructor() as any}
permalinkCreator={new MatrixToPermalinkConstructor() as any}
relation={{
rel_type: RelationType.Thread,
event_id: "myFakeThreadId",
+39 -1
View File
@@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { linkify } from '../src/linkify-matrix';
import { linkify, Type } from '../src/linkify-matrix';
describe('linkify-matrix', () => {
const linkTypesByInitialCharacter = {
@@ -177,6 +177,18 @@ describe('linkify-matrix', () => {
isLink: true,
}]));
});
it('accept hyphens in name ' + char + 'foo-bar:server.com', () => {
const test = '' + char + 'foo-bar:server.com';
const found = linkify.find(test);
expect(found).toEqual(([{
href: char + "foo-bar:server.com",
type,
value: char + "foo-bar:server.com",
start: 0,
end: test.length,
isLink: true,
}]));
});
it('ignores trailing `:`', () => {
const test = '' + char + 'foo:bar.com:';
const found = linkify.find(test);
@@ -264,4 +276,30 @@ describe('linkify-matrix', () => {
describe('userid plugin', () => {
genTests('@');
});
describe('matrix uri', () => {
const AcceptedMatrixUris = [
'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',
];
for (const matrixUri of AcceptedMatrixUris) {
it('accepts ' + matrixUri, () => {
const test = matrixUri;
const found = linkify.find(test);
expect(found).toEqual(([{
href: matrixUri,
type: Type.URL,
value: matrixUri,
end: matrixUri.length,
start: 0,
isLink: true,
}]));
});
}
});
});