Disallow links without protocol (e.g. starting with http(s)://) in LinkedText. (#32972)

* Disallow links without protocols in LinkedText.

* Update tests
This commit is contained in:
Will Hunt
2026-04-07 07:53:06 +00:00
committed by GitHub
parent 11fd669c26
commit cffd8cfd70
4 changed files with 21 additions and 11 deletions
@@ -49,6 +49,15 @@ describe("LinkedText", () => {
expect(container).toMatchSnapshot();
});
it("does not linkify domains without a protocol.", () => {
const { queryAllByRole } = render(
<LinkedTextContext value={{}}>
<LinkedText>Check out this link github.com</LinkedText>
</LinkedTextContext>,
);
expect(queryAllByRole("link")).toHaveLength(0);
});
it("renders a user ID", () => {
const { container } = render(<WithUserId />);
expect(container).toMatchSnapshot();
@@ -73,7 +82,7 @@ describe("LinkedText", () => {
const fn = vitest.fn();
const { getAllByRole } = render(
<LinkedTextContext value={{}}>
<LinkedText onLinkClick={fn}>Check out this link https://google.com and example.org</LinkedText>
<LinkedText onLinkClick={fn}>Check out this link https://google.com and https://example.org</LinkedText>
</LinkedTextContext>,
);
const links = getAllByRole("link");
@@ -228,10 +228,10 @@ export function generateLinkedTextOptions({
: undefined),
// By default, ignore Matrix ID types.
// Other applications may implement their own version of LinkifyComponent.
validate: (_value, type: string) =>
validate: (value, type: string) =>
!!(type === LinkifyMatrixOpaqueIdType.UserId && userIdListener) ||
!!(type === LinkifyMatrixOpaqueIdType.RoomAlias && roomAliasListener) ||
type === LinkifyMatrixOpaqueIdType.URL,
!!(type === LinkifyMatrixOpaqueIdType.URL && URL.canParse(value)),
} satisfies linkifyjs.Opts;
}