2015-11-27 23:02:32 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-27 23:02:32 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var sanitizeHtml = require('sanitize-html');
|
|
|
|
var highlight = require('highlight.js');
|
2016-03-20 11:05:07 +08:00
|
|
|
var linkifyMatrix = require('./linkify-matrix');
|
2016-07-05 05:43:53 +08:00
|
|
|
import escape from 'lodash/escape';
|
2016-07-05 06:34:57 +08:00
|
|
|
import {unicodeToImage, unicodeRegexp} from 'emojione';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const EMOJI_REGEX = new RegExp(unicodeRegexp+"+", "gi");
|
2015-11-27 23:02:32 +08:00
|
|
|
|
|
|
|
var sanitizeHtmlParams = {
|
|
|
|
allowedTags: [
|
2016-02-09 23:07:39 +08:00
|
|
|
'font', // custom to matrix for IRC-style font coloring
|
2015-11-28 20:44:10 +08:00
|
|
|
'del', // for markdown
|
2016-02-09 23:07:39 +08:00
|
|
|
// deliberately no h1/h2 to stop people shouting.
|
2015-11-27 23:02:32 +08:00
|
|
|
'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
|
2016-04-03 00:45:29 +08:00
|
|
|
'nl', 'li', 'b', 'i', 'u', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
|
2015-11-27 23:02:32 +08:00
|
|
|
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre'
|
|
|
|
],
|
|
|
|
allowedAttributes: {
|
|
|
|
// custom ones first:
|
|
|
|
font: [ 'color' ], // custom to matrix
|
|
|
|
a: [ 'href', 'name', 'target' ], // remote target: custom to matrix
|
|
|
|
// We don't currently allow img itself by default, but this
|
|
|
|
// would make sense if we did
|
|
|
|
img: [ 'src' ],
|
|
|
|
},
|
|
|
|
// Lots of these won't come up by default because we don't allow them
|
|
|
|
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
|
|
|
|
// URL schemes we permit
|
|
|
|
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ],
|
|
|
|
allowedSchemesByTag: {},
|
|
|
|
|
|
|
|
transformTags: { // custom to matrix
|
2016-03-20 11:05:07 +08:00
|
|
|
// add blank targets to all hyperlinks except vector URLs
|
|
|
|
'a': function(tagName, attribs) {
|
2016-03-21 23:45:04 +08:00
|
|
|
var m = attribs.href ? attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN) : null;
|
2016-03-20 11:05:07 +08:00
|
|
|
if (m) {
|
2016-03-21 23:54:02 +08:00
|
|
|
delete attribs.target;
|
2016-03-20 11:05:07 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-03-21 23:54:02 +08:00
|
|
|
attribs.target = '_blank';
|
2016-03-20 11:05:07 +08:00
|
|
|
}
|
2016-03-25 09:25:32 +08:00
|
|
|
return { tagName: tagName, attribs : attribs };
|
2016-03-20 11:05:07 +08:00
|
|
|
},
|
2015-11-27 23:02:32 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-02-18 03:50:04 +08:00
|
|
|
class BaseHighlighter {
|
|
|
|
constructor(highlightClass, highlightLink) {
|
2015-12-24 07:50:35 +08:00
|
|
|
this.highlightClass = highlightClass;
|
2016-02-18 03:50:04 +08:00
|
|
|
this.highlightLink = highlightLink;
|
2015-12-24 07:50:35 +08:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:50:04 +08:00
|
|
|
/**
|
|
|
|
* apply the highlights to a section of text
|
|
|
|
*
|
|
|
|
* @param {string} safeSnippet The snippet of text to apply the highlights
|
|
|
|
* to.
|
|
|
|
* @param {string[]} safeHighlights A list of substrings to highlight,
|
|
|
|
* sorted by descending length.
|
|
|
|
*
|
|
|
|
* returns a list of results (strings for HtmlHighligher, react nodes for
|
|
|
|
* TextHighlighter).
|
|
|
|
*/
|
2016-02-11 04:25:32 +08:00
|
|
|
applyHighlights(safeSnippet, safeHighlights) {
|
2015-11-29 11:22:01 +08:00
|
|
|
var lastOffset = 0;
|
|
|
|
var offset;
|
|
|
|
var nodes = [];
|
|
|
|
|
2016-02-11 04:25:32 +08:00
|
|
|
var safeHighlight = safeHighlights[0];
|
2015-12-28 11:14:50 +08:00
|
|
|
while ((offset = safeSnippet.toLowerCase().indexOf(safeHighlight.toLowerCase(), lastOffset)) >= 0) {
|
2015-11-29 11:22:01 +08:00
|
|
|
// handle preamble
|
|
|
|
if (offset > lastOffset) {
|
2015-12-24 07:50:35 +08:00
|
|
|
var subSnippet = safeSnippet.substring(lastOffset, offset);
|
2016-02-11 04:25:32 +08:00
|
|
|
nodes = nodes.concat(this._applySubHighlights(subSnippet, safeHighlights));
|
2015-11-29 11:22:01 +08:00
|
|
|
}
|
|
|
|
|
2016-02-18 03:50:04 +08:00
|
|
|
// do highlight. use the original string rather than safeHighlight
|
|
|
|
// to preserve the original casing.
|
|
|
|
var endOffset = offset + safeHighlight.length;
|
|
|
|
nodes.push(this._processSnippet(safeSnippet.substring(offset, endOffset), true));
|
2015-11-29 11:22:01 +08:00
|
|
|
|
2016-02-18 03:50:04 +08:00
|
|
|
lastOffset = endOffset;
|
2015-11-29 11:22:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle postamble
|
|
|
|
if (lastOffset != safeSnippet.length) {
|
2015-12-24 07:50:35 +08:00
|
|
|
var subSnippet = safeSnippet.substring(lastOffset, undefined);
|
2016-02-11 04:25:32 +08:00
|
|
|
nodes = nodes.concat(this._applySubHighlights(subSnippet, safeHighlights));
|
2015-11-29 21:00:58 +08:00
|
|
|
}
|
|
|
|
return nodes;
|
2015-12-24 07:50:35 +08:00
|
|
|
}
|
2015-11-29 21:00:58 +08:00
|
|
|
|
2016-02-11 04:25:32 +08:00
|
|
|
_applySubHighlights(safeSnippet, safeHighlights) {
|
|
|
|
if (safeHighlights[1]) {
|
2015-11-29 21:00:58 +08:00
|
|
|
// recurse into this range to check for the next set of highlight matches
|
2016-02-11 04:25:32 +08:00
|
|
|
return this.applyHighlights(safeSnippet, safeHighlights.slice(1));
|
2015-11-29 21:00:58 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no more highlights to be found, just return the unhighlighted string
|
2016-02-18 03:50:04 +08:00
|
|
|
return [this._processSnippet(safeSnippet, false)];
|
2015-12-24 07:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 03:50:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class HtmlHighlighter extends BaseHighlighter {
|
|
|
|
/* highlight the given snippet if required
|
|
|
|
*
|
|
|
|
* snippet: content of the span; must have been sanitised
|
|
|
|
* highlight: true to highlight as a search match
|
|
|
|
*
|
|
|
|
* returns an HTML string
|
|
|
|
*/
|
|
|
|
_processSnippet(snippet, highlight) {
|
|
|
|
if (!highlight) {
|
|
|
|
// nothing required here
|
|
|
|
return snippet;
|
|
|
|
}
|
|
|
|
|
|
|
|
var span = "<span class=\""+this.highlightClass+"\">"
|
|
|
|
+ snippet + "</span>";
|
|
|
|
|
|
|
|
if (this.highlightLink) {
|
|
|
|
span = "<a href=\""+encodeURI(this.highlightLink)+"\">"
|
|
|
|
+span+"</a>";
|
|
|
|
}
|
|
|
|
return span;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TextHighlighter extends BaseHighlighter {
|
|
|
|
constructor(highlightClass, highlightLink) {
|
|
|
|
super(highlightClass, highlightLink);
|
|
|
|
this._key = 0;
|
|
|
|
}
|
2015-12-24 07:50:35 +08:00
|
|
|
|
|
|
|
/* create a <span> node to hold the given content
|
|
|
|
*
|
2016-02-18 03:50:04 +08:00
|
|
|
* snippet: content of the span
|
2015-12-24 07:50:35 +08:00
|
|
|
* highlight: true to highlight as a search match
|
2016-02-18 03:50:04 +08:00
|
|
|
*
|
|
|
|
* returns a React node
|
2015-12-24 07:50:35 +08:00
|
|
|
*/
|
2016-02-18 03:50:04 +08:00
|
|
|
_processSnippet(snippet, highlight) {
|
2016-02-23 01:44:34 +08:00
|
|
|
var key = this._key++;
|
2015-12-24 07:50:35 +08:00
|
|
|
|
2016-02-23 01:44:34 +08:00
|
|
|
var node =
|
|
|
|
<span key={key} className={highlight ? this.highlightClass : null }>
|
|
|
|
{ snippet }
|
|
|
|
</span>;
|
2016-02-18 03:50:04 +08:00
|
|
|
|
|
|
|
if (highlight && this.highlightLink) {
|
2016-02-23 01:44:34 +08:00
|
|
|
node = <a key={key} href={this.highlightLink}>{node}</a>
|
2015-12-24 07:50:35 +08:00
|
|
|
}
|
2016-02-23 01:44:34 +08:00
|
|
|
|
2016-02-18 03:50:04 +08:00
|
|
|
return node;
|
2015-12-24 07:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-27 23:02:32 +08:00
|
|
|
|
|
|
|
|
2015-12-24 07:50:35 +08:00
|
|
|
module.exports = {
|
|
|
|
/* turn a matrix event body into html
|
|
|
|
*
|
|
|
|
* content: 'content' of the MatrixEvent
|
|
|
|
*
|
2016-02-11 04:25:32 +08:00
|
|
|
* highlights: optional list of words to highlight, ordered by longest word first
|
2015-12-24 07:50:35 +08:00
|
|
|
*
|
2016-02-18 03:50:04 +08:00
|
|
|
* opts.highlightLink: optional href to add to highlights
|
2015-12-24 07:50:35 +08:00
|
|
|
*/
|
|
|
|
bodyToHtml: function(content, highlights, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
var isHtml = (content.format === "org.matrix.custom.html");
|
2016-07-05 05:43:53 +08:00
|
|
|
let body = isHtml ? content.formatted_body : escape(content.body);
|
2015-12-24 07:50:35 +08:00
|
|
|
|
2016-02-11 22:03:48 +08:00
|
|
|
var safeBody;
|
2016-07-05 05:43:53 +08:00
|
|
|
// XXX: We sanitize the HTML whilst also highlighting its text nodes, to avoid accidentally trying
|
|
|
|
// to highlight HTML tags themselves. However, this does mean that we don't highlight textnodes which
|
|
|
|
// are interrupted by HTML tags (not that we did before) - e.g. foo<span/>bar won't get highlighted
|
|
|
|
// by an attempt to search for 'foobar'. Then again, the search query probably wouldn't work either
|
|
|
|
try {
|
2016-02-11 04:25:32 +08:00
|
|
|
if (highlights && highlights.length > 0) {
|
2016-07-05 05:43:53 +08:00
|
|
|
var highlighter = new HtmlHighlighter("mx_EventTile_searchHighlight", opts.highlightLink);
|
|
|
|
var safeHighlights = highlights.map(function(highlight) {
|
|
|
|
return sanitizeHtml(highlight, sanitizeHtmlParams);
|
|
|
|
});
|
|
|
|
// XXX: hacky bodge to temporarily apply a textFilter to the sanitizeHtmlParams structure.
|
|
|
|
sanitizeHtmlParams.textFilter = function(safeText) {
|
|
|
|
return highlighter.applyHighlights(safeText, safeHighlights).join('');
|
|
|
|
};
|
2015-11-27 23:02:32 +08:00
|
|
|
}
|
2016-07-05 05:43:53 +08:00
|
|
|
safeBody = sanitizeHtml(body, sanitizeHtmlParams);
|
|
|
|
safeBody = unicodeToImage(safeBody);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
delete sanitizeHtmlParams.textFilter;
|
2015-11-27 23:02:32 +08:00
|
|
|
}
|
2016-07-05 06:34:57 +08:00
|
|
|
|
|
|
|
EMOJI_REGEX.lastIndex = 0;
|
|
|
|
let match = EMOJI_REGEX.exec(body);
|
|
|
|
let emojiBody = match && match[0] && match[0].length === body.length;
|
|
|
|
|
|
|
|
let className = classNames('markdown-body', {
|
|
|
|
'emoji-body': emojiBody,
|
|
|
|
});
|
|
|
|
return <span className={className} dangerouslySetInnerHTML={{ __html: safeBody }} />;
|
2015-11-27 23:02:32 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
highlightDom: function(element) {
|
|
|
|
var blocks = element.getElementsByTagName("code");
|
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
highlight.highlightBlock(blocks[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|