From 162a5de82e335ccaebdc25dd41ee85b80420eb02 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Mon, 21 Dec 2020 12:46:29 +0000 Subject: [PATCH 1/2] Rename and reformat Element URL pattern This is just a code style cleanup. There are no behaviour changes in this commit. --- src/HtmlUtils.tsx | 2 +- src/linkify-matrix.js | 15 ++++++++------- src/utils/permalinks/Permalinks.js | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/HtmlUtils.tsx b/src/HtmlUtils.tsx index 2301ad250b..ba0781f0a4 100644 --- a/src/HtmlUtils.tsx +++ b/src/HtmlUtils.tsx @@ -163,7 +163,7 @@ const transformTags: IExtendedSanitizeOptions["transformTags"] = { // custom to attribs.target = '_blank'; // by default const transformed = tryTransformPermalinkToLocalHref(attribs.href); - if (transformed !== attribs.href || attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN)) { + if (transformed !== attribs.href || attribs.href.match(linkifyMatrix.ELEMENT_URL_PATTERN)) { attribs.href = transformed; delete attribs.target; } diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 77c62ce84d..bee8e95137 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -183,12 +183,13 @@ const escapeRegExp = function(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }; -// Recognise URLs from both our local vector and official vector as vector. -// anyone else really should be using matrix.to. -matrixLinkify.VECTOR_URL_PATTERN = "^(?:https?://)?(?:" - + escapeRegExp(window.location.host + window.location.pathname) + "|" - + "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/" - + ")(#.*)"; +// Recognise URLs from both our local and official Element deployments. +// Anyone else really should be using matrix.to. +matrixLinkify.ELEMENT_URL_PATTERN = + "^(?:https?://)?(?:" + + escapeRegExp(window.location.host + window.location.pathname) + "|" + + "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/" + + ")(#.*)"; matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)"; matrixLinkify.MATRIXTO_MD_LINK_PATTERN = @@ -253,7 +254,7 @@ matrixLinkify.options = { target: function(href, type) { if (type === 'url') { const transformed = tryTransformPermalinkToLocalHref(href); - if (transformed !== href || href.match(matrixLinkify.VECTOR_URL_PATTERN)) { + if (transformed !== href || href.match(matrixLinkify.ELEMENT_URL_PATTERN)) { return null; } else { return '_blank'; diff --git a/src/utils/permalinks/Permalinks.js b/src/utils/permalinks/Permalinks.js index 39c5776852..e157ecc55e 100644 --- a/src/utils/permalinks/Permalinks.js +++ b/src/utils/permalinks/Permalinks.js @@ -331,7 +331,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string { return permalink; } - const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN); + const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN); if (m) { return m[1]; } @@ -365,7 +365,7 @@ export function getPrimaryPermalinkEntity(permalink: string): string { // If not a permalink, try the vector patterns. if (!permalinkParts) { - const m = permalink.match(matrixLinkify.VECTOR_URL_PATTERN); + const m = permalink.match(matrixLinkify.ELEMENT_URL_PATTERN); if (m) { // A bit of a hack, but it gets the job done const handler = new ElementPermalinkConstructor("http://localhost"); From 5d78418530f9c2f90638ee567a45c2cc3c2616c9 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Mon, 21 Dec 2020 13:15:00 +0000 Subject: [PATCH 2/2] Recognise *.element.io links as Element permalinks This ensures all Elements detect permalinks to official deployments as Element and handle them internally. Fixes https://github.com/vector-im/element-web/issues/16005 --- src/linkify-matrix.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index bee8e95137..1a40fde26f 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -188,7 +188,8 @@ const escapeRegExp = function(string) { matrixLinkify.ELEMENT_URL_PATTERN = "^(?:https?://)?(?:" + escapeRegExp(window.location.host + window.location.pathname) + "|" + - "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/" + + "(?:www\\.)?(?:riot|vector)\\.im/(?:app|beta|staging|develop)/|" + + "(?:app|beta|staging|develop)\\.element\\.io/" + ")(#.*)"; matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?://)?(?:www\\.)?matrix\\.to/#/(([#@!+]).*)";