2015-07-10 15:37:13 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function matrixLinkify(linkify) {
|
|
|
|
// Text tokens
|
2015-09-08 17:17:06 +02:00
|
|
|
var TT = linkify.scanner.TOKENS;
|
2015-07-10 15:37:13 +02:00
|
|
|
// Multi tokens
|
2015-09-08 17:17:06 +02:00
|
|
|
var MT = linkify.parser.TOKENS;
|
|
|
|
var MultiToken = MT.Base;
|
2015-07-10 15:37:13 +02:00
|
|
|
var S_START = linkify.parser.start;
|
|
|
|
|
2016-09-27 11:38:01 +02:00
|
|
|
if (TT.UNDERSCORE === undefined) {
|
|
|
|
throw new Error("linkify-matrix requires linkifyjs 2.1.1: this version is too old.");
|
|
|
|
}
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2016-09-13 20:36:46 +02:00
|
|
|
var ROOMALIAS = function(value) {
|
|
|
|
MultiToken.call(this, value);
|
|
|
|
this.type = 'roomalias';
|
|
|
|
this.isLink = true;
|
|
|
|
};
|
|
|
|
ROOMALIAS.prototype = new MultiToken();
|
|
|
|
|
|
|
|
var S_HASH = new linkify.parser.State();
|
2015-09-08 17:17:06 +02:00
|
|
|
var S_HASH_NAME = new linkify.parser.State();
|
|
|
|
var S_HASH_NAME_COLON = new linkify.parser.State();
|
2016-09-13 20:36:46 +02:00
|
|
|
var S_HASH_NAME_COLON_DOMAIN = new linkify.parser.State();
|
|
|
|
var S_HASH_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
2015-09-08 17:17:06 +02:00
|
|
|
var S_ROOMALIAS = new linkify.parser.State(ROOMALIAS);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
var roomname_tokens = [
|
|
|
|
TT.DOT,
|
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
2016-09-21 15:11:43 +02:00
|
|
|
TT.TLD,
|
|
|
|
TT.UNDERSCORE,
|
|
|
|
TT.POUND,
|
2016-09-23 15:02:14 +02:00
|
|
|
|
|
|
|
// because 'localhost' is tokenised to the localhost token,
|
|
|
|
// usernames @localhost:foo.com are otherwise not matched!
|
|
|
|
TT.LOCALHOST,
|
2015-07-10 15:37:13 +02:00
|
|
|
];
|
|
|
|
|
2015-09-08 17:17:06 +02:00
|
|
|
S_START.on(TT.POUND, S_HASH);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2015-09-08 17:17:06 +02:00
|
|
|
S_HASH.on(roomname_tokens, S_HASH_NAME);
|
|
|
|
S_HASH_NAME.on(roomname_tokens, S_HASH_NAME);
|
2016-09-13 20:36:46 +02:00
|
|
|
S_HASH_NAME.on(TT.DOMAIN, S_HASH_NAME);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
S_HASH_NAME.on(TT.COLON, S_HASH_NAME_COLON);
|
|
|
|
|
2016-09-13 20:36:46 +02:00
|
|
|
S_HASH_NAME_COLON.on(TT.DOMAIN, S_HASH_NAME_COLON_DOMAIN);
|
2016-09-23 15:02:14 +02:00
|
|
|
S_HASH_NAME_COLON.on(TT.LOCALHOST, S_ROOMALIAS); // accept #foo:localhost
|
2016-09-13 20:36:46 +02:00
|
|
|
S_HASH_NAME_COLON_DOMAIN.on(TT.DOT, S_HASH_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_HASH_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_HASH_NAME_COLON_DOMAIN);
|
|
|
|
S_HASH_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_ROOMALIAS);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
var USERID = function(value) {
|
2016-09-13 20:36:46 +02:00
|
|
|
MultiToken.call(this, value);
|
|
|
|
this.type = 'userid';
|
|
|
|
this.isLink = true;
|
2015-07-10 15:37:13 +02:00
|
|
|
};
|
2016-09-13 20:36:46 +02:00
|
|
|
USERID.prototype = new MultiToken();
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2015-09-08 17:17:06 +02:00
|
|
|
var S_AT = new linkify.parser.State();
|
|
|
|
var S_AT_NAME = new linkify.parser.State();
|
|
|
|
var S_AT_NAME_COLON = new linkify.parser.State();
|
2016-09-13 20:36:46 +02:00
|
|
|
var S_AT_NAME_COLON_DOMAIN = new linkify.parser.State();
|
2015-09-08 17:17:06 +02:00
|
|
|
var S_AT_NAME_COLON_DOMAIN_DOT = new linkify.parser.State();
|
|
|
|
var S_USERID = new linkify.parser.State(USERID);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
var username_tokens = [
|
|
|
|
TT.DOT,
|
2016-09-23 15:02:14 +02:00
|
|
|
TT.UNDERSCORE,
|
2015-07-10 15:37:13 +02:00
|
|
|
TT.PLUS,
|
|
|
|
TT.NUM,
|
|
|
|
TT.DOMAIN,
|
2016-09-23 15:02:14 +02:00
|
|
|
TT.TLD,
|
|
|
|
|
|
|
|
// as in roomname_tokens
|
|
|
|
TT.LOCALHOST,
|
2015-07-10 15:37:13 +02:00
|
|
|
];
|
|
|
|
|
2015-09-08 17:17:06 +02:00
|
|
|
S_START.on(TT.AT, S_AT);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
2015-09-08 17:17:06 +02:00
|
|
|
S_AT.on(username_tokens, S_AT_NAME);
|
|
|
|
S_AT_NAME.on(username_tokens, S_AT_NAME);
|
2016-09-13 20:36:46 +02:00
|
|
|
S_AT_NAME.on(TT.DOMAIN, S_AT_NAME);
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
S_AT_NAME.on(TT.COLON, S_AT_NAME_COLON);
|
|
|
|
|
2016-09-13 20:36:46 +02:00
|
|
|
S_AT_NAME_COLON.on(TT.DOMAIN, S_AT_NAME_COLON_DOMAIN);
|
2016-09-23 15:02:14 +02:00
|
|
|
S_AT_NAME_COLON.on(TT.LOCALHOST, S_USERID); // accept @foo:localhost
|
2016-09-13 20:36:46 +02:00
|
|
|
S_AT_NAME_COLON_DOMAIN.on(TT.DOT, S_AT_NAME_COLON_DOMAIN_DOT);
|
|
|
|
S_AT_NAME_COLON_DOMAIN_DOT.on(TT.DOMAIN, S_AT_NAME_COLON_DOMAIN);
|
|
|
|
S_AT_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_USERID);
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 02:55:42 +02:00
|
|
|
// stubs, overwritten in MatrixChat's componentDidMount
|
2016-08-28 03:05:23 +02:00
|
|
|
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
|
|
|
|
matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
|
2015-10-26 18:59:49 +01:00
|
|
|
|
2016-03-20 13:31:30 +01:00
|
|
|
var escapeRegExp = function(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
};
|
|
|
|
|
2016-08-28 02:55:42 +02:00
|
|
|
// 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) + "|"
|
2017-04-10 13:07:39 +02:00
|
|
|
+ "(?:www\\.)?(?:riot|vector)\\.im/(?:beta|staging|develop)/"
|
2016-08-28 02:55:42 +02:00
|
|
|
+ ")(#.*)";
|
|
|
|
|
2016-08-28 03:12:18 +02:00
|
|
|
matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/((#|@|!).*)";
|
2016-11-04 12:38:28 +01:00
|
|
|
matrixLinkify.MATRIXTO_BASE_URL= "https://matrix.to";
|
2016-03-20 04:05:07 +01:00
|
|
|
|
2015-07-10 15:37:13 +02:00
|
|
|
matrixLinkify.options = {
|
2017-01-20 15:22:27 +01:00
|
|
|
events: function(href, type) {
|
2015-07-10 15:37:13 +02:00
|
|
|
switch (type) {
|
2015-10-27 10:58:55 +01:00
|
|
|
case "userid":
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onUserClick(e, href);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
case "roomalias":
|
|
|
|
return {
|
|
|
|
click: function(e) {
|
|
|
|
matrixLinkify.onAliasClick(e, href);
|
|
|
|
}
|
|
|
|
};
|
2015-07-10 15:37:13 +02:00
|
|
|
}
|
2016-02-18 19:16:39 +01:00
|
|
|
},
|
2016-08-28 03:05:23 +02:00
|
|
|
|
2017-01-20 15:22:27 +01:00
|
|
|
formatHref: function(href, type) {
|
2016-02-18 19:16:39 +01:00
|
|
|
switch (type) {
|
2016-03-20 04:05:07 +01:00
|
|
|
case 'roomalias':
|
|
|
|
case 'userid':
|
2016-11-04 12:38:28 +01:00
|
|
|
return matrixLinkify.MATRIXTO_BASE_URL + '/#/' + href;
|
2016-03-20 04:05:07 +01:00
|
|
|
default:
|
2016-08-28 02:55:42 +02:00
|
|
|
var m;
|
|
|
|
// FIXME: horrible duplication with HtmlUtils' transform tags
|
|
|
|
m = href.match(matrixLinkify.VECTOR_URL_PATTERN);
|
|
|
|
if (m) {
|
|
|
|
return m[1];
|
|
|
|
}
|
|
|
|
m = href.match(matrixLinkify.MATRIXTO_URL_PATTERN);
|
|
|
|
if (m) {
|
|
|
|
var entity = m[1];
|
|
|
|
if (entity[0] === '@') {
|
2016-08-28 03:05:23 +02:00
|
|
|
return '#/user/' + entity;
|
2016-08-28 02:55:42 +02:00
|
|
|
}
|
2016-08-28 03:05:23 +02:00
|
|
|
else if (entity[0] === '#' || entity[0] === '!') {
|
2016-08-28 02:55:42 +02:00
|
|
|
return '#/room/' + entity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:05:07 +01:00
|
|
|
return href;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-15 22:37:26 +02:00
|
|
|
linkAttributes: {
|
|
|
|
rel: 'noopener',
|
|
|
|
},
|
|
|
|
|
2016-03-20 04:05:07 +01:00
|
|
|
target: function(href, type) {
|
|
|
|
if (type === 'url') {
|
2016-08-28 02:55:42 +02:00
|
|
|
if (href.match(matrixLinkify.VECTOR_URL_PATTERN) ||
|
|
|
|
href.match(matrixLinkify.MATRIXTO_URL_PATTERN))
|
|
|
|
{
|
2016-03-20 04:05:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return '_blank';
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 16:45:04 +01:00
|
|
|
return null;
|
2016-03-20 04:05:07 +01:00
|
|
|
},
|
2015-10-26 18:59:49 +01:00
|
|
|
};
|
2015-07-10 15:37:13 +02:00
|
|
|
|
|
|
|
module.exports = matrixLinkify;
|