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');
|
|
|
|
|
|
|
|
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',
|
|
|
|
'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
|
|
|
|
'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
|
|
|
|
// add blank targets to all hyperlinks
|
|
|
|
'a': sanitizeHtml.simpleTransform('a', { target: '_blank'} )
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-12-24 07:50:35 +08:00
|
|
|
class Highlighter {
|
|
|
|
constructor(html, highlightClass, onHighlightClick) {
|
|
|
|
this.html = html;
|
|
|
|
this.highlightClass = highlightClass;
|
|
|
|
this.onHighlightClick = onHighlightClick;
|
|
|
|
this._key = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyHighlights(safeSnippet, highlights) {
|
2015-11-29 11:22:01 +08:00
|
|
|
var lastOffset = 0;
|
|
|
|
var offset;
|
|
|
|
var nodes = [];
|
|
|
|
|
|
|
|
// XXX: when highlighting HTML, synapse performs the search on the plaintext body,
|
|
|
|
// but we're attempting to apply the highlights here to the HTML body. This is
|
|
|
|
// never going to end well - we really should be hooking into the sanitzer HTML
|
|
|
|
// parser to only attempt to highlight text nodes to avoid corrupting tags.
|
2016-02-09 23:07:39 +08:00
|
|
|
// If and when this happens, we'll probably have to split this method in two between
|
2015-11-29 11:22:01 +08:00
|
|
|
// HTML and plain-text highlighting.
|
|
|
|
|
2015-12-24 07:50:35 +08:00
|
|
|
var safeHighlight = this.html ? sanitizeHtml(highlights[0], sanitizeHtmlParams) : highlights[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);
|
|
|
|
nodes = nodes.concat(this._applySubHighlights(subSnippet, highlights));
|
2015-11-29 11:22:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// do highlight
|
2015-12-24 07:50:35 +08:00
|
|
|
nodes.push(this._createSpan(safeHighlight, true));
|
2015-11-29 11:22:01 +08:00
|
|
|
|
|
|
|
lastOffset = offset + safeHighlight.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle postamble
|
|
|
|
if (lastOffset != safeSnippet.length) {
|
2015-12-24 07:50:35 +08:00
|
|
|
var subSnippet = safeSnippet.substring(lastOffset, undefined);
|
|
|
|
nodes = nodes.concat(this._applySubHighlights(subSnippet, highlights));
|
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
|
|
|
|
2015-12-24 07:50:35 +08:00
|
|
|
_applySubHighlights(safeSnippet, highlights) {
|
2015-11-29 21:00:58 +08:00
|
|
|
if (highlights[1]) {
|
|
|
|
// recurse into this range to check for the next set of highlight matches
|
2015-12-24 07:50:35 +08:00
|
|
|
return this.applyHighlights(safeSnippet, highlights.slice(1));
|
2015-11-29 21:00:58 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no more highlights to be found, just return the unhighlighted string
|
2015-12-24 07:50:35 +08:00
|
|
|
return [this._createSpan(safeSnippet, false)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a <span> node to hold the given content
|
|
|
|
*
|
|
|
|
* spanBody: content of the span. If html, must have been sanitised
|
|
|
|
* highlight: true to highlight as a search match
|
|
|
|
*/
|
|
|
|
_createSpan(spanBody, highlight) {
|
|
|
|
var spanProps = {
|
|
|
|
key: this._key++,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (highlight) {
|
|
|
|
spanProps.onClick = this.onHighlightClick;
|
|
|
|
spanProps.className = this.highlightClass;
|
2015-11-29 11:22:01 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 07:50:35 +08:00
|
|
|
if (this.html) {
|
|
|
|
return (<span {...spanProps} dangerouslySetInnerHTML={{ __html: spanBody }} />);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (<span {...spanProps}>{ spanBody }</span>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
* highlights: optional list of words to highlight
|
|
|
|
*
|
|
|
|
* opts.onHighlightClick: optional callback function to be called when a
|
|
|
|
* highlighted word is clicked
|
|
|
|
*/
|
|
|
|
bodyToHtml: function(content, highlights, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
var isHtml = (content.format === "org.matrix.custom.html");
|
|
|
|
|
|
|
|
var safeBody;
|
|
|
|
if (isHtml) {
|
|
|
|
safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
|
|
|
|
} else {
|
|
|
|
safeBody = content.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
var body;
|
|
|
|
if (highlights && highlights.length > 0) {
|
2016-01-03 08:11:11 +08:00
|
|
|
var highlighter = new Highlighter(isHtml, "mx_EventTile_searchHighlight", opts.onHighlightClick);
|
2015-12-24 07:50:35 +08:00
|
|
|
body = highlighter.applyHighlights(safeBody, highlights);
|
2015-11-27 23:02:32 +08:00
|
|
|
}
|
|
|
|
else {
|
2015-12-24 07:50:35 +08:00
|
|
|
if (isHtml) {
|
2015-11-27 23:02:32 +08:00
|
|
|
body = <span className="markdown-body" dangerouslySetInnerHTML={{ __html: safeBody }} />;
|
|
|
|
}
|
|
|
|
else {
|
2015-12-24 07:50:35 +08:00
|
|
|
body = safeBody;
|
2015-11-27 23:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
},
|
|
|
|
|
|
|
|
highlightDom: function(element) {
|
|
|
|
var blocks = element.getElementsByTagName("code");
|
|
|
|
for (var i = 0; i < blocks.length; i++) {
|
|
|
|
highlight.highlightBlock(blocks[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|