diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 6db2b08fd1..6d0bfc67c0 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -19,6 +19,7 @@ limitations under the License. var React = require('react'); var sanitizeHtml = require('sanitize-html'); var highlight = require('highlight.js'); +var linkifyMatrix = require('./linkify-matrix'); var sanitizeHtmlParams = { allowedTags: [ @@ -44,8 +45,17 @@ var sanitizeHtmlParams = { allowedSchemesByTag: {}, transformTags: { // custom to matrix - // add blank targets to all hyperlinks - 'a': sanitizeHtml.simpleTransform('a', { target: '_blank'} ) + // add blank targets to all hyperlinks except vector URLs + 'a': function(tagName, attribs) { + var m = attribs.href ? attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN) : null; + if (m) { + delete attribs.target; + } + else { + attribs.target = '_blank'; + } + return attribs; + }, }, }; diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 7fb043f800..a12ef8eaf5 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -98,6 +98,16 @@ function matrixLinkify(linkify) { matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); }; matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); }; +var escapeRegExp = function(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +}; + +// we only recognise URLs which match our current URL as being the same app +// as if someone explicitly links to vector.im/develop and we're on vector.im/beta +// they may well be trying to get us to explicitly go to develop. +// FIXME: intercept matrix.to URLs as well. +matrixLinkify.VECTOR_URL_PATTERN = "^(https?:\/\/)?" + escapeRegExp(window.location.host + window.location.pathname); + matrixLinkify.options = { events: function (href, type) { switch (type) { @@ -118,14 +128,26 @@ matrixLinkify.options = { formatHref: function (href, type) { switch (type) { - case 'roomalias': - return '#/room/' + href; - case 'userid': - return '#'; - default: - return href; + case 'roomalias': + return '#/room/' + href; + case 'userid': + return '#'; + default: + return href; } - } + }, + + target: function(href, type) { + if (type === 'url') { + if (href.match(matrixLinkify.VECTOR_URL_PATTERN)) { + return null; + } + else { + return '_blank'; + } + } + return null; + }, }; module.exports = matrixLinkify;