From 1aed9ccbf416f3beccdf6d8138a3dd84d3056767 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 Mar 2016 03:05:07 +0000 Subject: [PATCH 1/4] linkify vector.im URLs directly into the app, both from HTML and non-HTML messages --- src/HtmlUtils.js | 14 ++++++++++++-- src/linkify-matrix.js | 35 ++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 6db2b08fd1..1412112260 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) { + // XXX: use matrix.to instead and deduplicate regexp with linkify-matrix.js + var m = attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN); + if (m) { + return { tagName: 'a', attribs: { href: m[4] } }; + } + else { + return { tagName: 'a', attribs: { href: attribs.href, target: '_blank'} }; + } + }, }, }; diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 7fb043f800..51e4af044f 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -98,6 +98,8 @@ function matrixLinkify(linkify) { matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); }; matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); }; +matrixLinkify.VECTOR_URL_PATTERN = /(https?:\/\/)?(www\.)?vector\.im\/(beta|staging|develop)?\/(#.*)/; + matrixLinkify.options = { events: function (href, type) { switch (type) { @@ -118,14 +120,33 @@ 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 '#'; + case 'url': + // intercept vector links directly into the app + // FIXME: use matrix.to asap, as this is fragile as sin + var m = href.match(matrixLinkify.VECTOR_URL_PATTERN); + return m ? m[4] : href; + default: + return href; } - } + }, + + target: function(href, type) { + if (type === 'url') { + if (href.match(matrixLinkify.VECTOR_URL_PATTERN)) { + return null; + } + else { + return '_blank'; + } + } + else { + return '_blank'; + } + }, }; module.exports = matrixLinkify; From d54a75c913817d38898dfbd12498833e6b5878da Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sun, 20 Mar 2016 12:31:30 +0000 Subject: [PATCH 2/4] actually, only intercept URLs which are explicitly referring to our current app --- src/HtmlUtils.js | 3 +-- src/linkify-matrix.js | 15 +++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 1412112260..18de137634 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -47,10 +47,9 @@ var sanitizeHtmlParams = { transformTags: { // custom to matrix // add blank targets to all hyperlinks except vector URLs 'a': function(tagName, attribs) { - // XXX: use matrix.to instead and deduplicate regexp with linkify-matrix.js var m = attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN); if (m) { - return { tagName: 'a', attribs: { href: m[4] } }; + return { tagName: 'a', attribs: { href: attribs.href } }; } else { return { tagName: 'a', attribs: { href: attribs.href, target: '_blank'} }; diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 51e4af044f..bb9b7d5c81 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -98,7 +98,15 @@ function matrixLinkify(linkify) { matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); }; matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); }; -matrixLinkify.VECTOR_URL_PATTERN = /(https?:\/\/)?(www\.)?vector\.im\/(beta|staging|develop)?\/(#.*)/; +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) { @@ -124,11 +132,6 @@ matrixLinkify.options = { return '#/room/' + href; case 'userid': return '#'; - case 'url': - // intercept vector links directly into the app - // FIXME: use matrix.to asap, as this is fragile as sin - var m = href.match(matrixLinkify.VECTOR_URL_PATTERN); - return m ? m[4] : href; default: return href; } From c3e96f8af1d4d925e562cd8b3f81262dca523078 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 21 Mar 2016 15:45:04 +0000 Subject: [PATCH 3/4] incorporate review --- src/HtmlUtils.js | 6 +++--- src/linkify-matrix.js | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 18de137634..3dc463a3d7 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -47,12 +47,12 @@ var sanitizeHtmlParams = { transformTags: { // custom to matrix // add blank targets to all hyperlinks except vector URLs 'a': function(tagName, attribs) { - var m = attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN); + var m = attribs.href ? attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN) : null; if (m) { - return { tagName: 'a', attribs: { href: attribs.href } }; + return { tagName: 'a', attribs: { href: attribs.href, name: attribs.name } }; } else { - return { tagName: 'a', attribs: { href: attribs.href, target: '_blank'} }; + return { tagName: 'a', attribs: { href: attribs.href, name: attribs.name, target: '_blank' } }; } }, }, diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index bb9b7d5c81..a12ef8eaf5 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -106,7 +106,7 @@ var escapeRegExp = function(string) { // 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.VECTOR_URL_PATTERN = "^(https?:\/\/)?" + escapeRegExp(window.location.host + window.location.pathname); matrixLinkify.options = { events: function (href, type) { @@ -146,9 +146,7 @@ matrixLinkify.options = { return '_blank'; } } - else { - return '_blank'; - } + return null; }, }; From 462ccf89d78c87800663f93fc67e23ce5d247d51 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Mon, 21 Mar 2016 15:54:02 +0000 Subject: [PATCH 4/4] inplace-edit on attribs --- src/HtmlUtils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 3dc463a3d7..6d0bfc67c0 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -49,11 +49,12 @@ var sanitizeHtmlParams = { 'a': function(tagName, attribs) { var m = attribs.href ? attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN) : null; if (m) { - return { tagName: 'a', attribs: { href: attribs.href, name: attribs.name } }; + delete attribs.target; } else { - return { tagName: 'a', attribs: { href: attribs.href, name: attribs.name, target: '_blank' } }; + attribs.target = '_blank'; } + return attribs; }, }, };