2015-11-27 23:02:32 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-09-09 06:14:06 +08:00
|
|
|
Copyright 2017 New Vector 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';
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const React = require('react');
|
|
|
|
const sanitizeHtml = require('sanitize-html');
|
|
|
|
const highlight = require('highlight.js');
|
|
|
|
const linkifyMatrix = require('./linkify-matrix');
|
2016-07-05 05:43:53 +08:00
|
|
|
import escape from 'lodash/escape';
|
2016-07-05 12:28:28 +08:00
|
|
|
import emojione from 'emojione';
|
2016-07-05 06:34:57 +08:00
|
|
|
import classNames from 'classnames';
|
2017-07-10 23:27:23 +08:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2016-07-05 06:34:57 +08:00
|
|
|
|
2016-08-10 00:10:05 +08:00
|
|
|
emojione.imagePathSVG = 'emojione/svg/';
|
2017-04-21 18:37:08 +08:00
|
|
|
// Store PNG path for displaying many flags at once (for increased performance over SVG)
|
|
|
|
emojione.imagePathPNG = 'emojione/png/';
|
|
|
|
// Use SVGs for emojis
|
2016-08-10 00:10:05 +08:00
|
|
|
emojione.imageType = 'svg';
|
|
|
|
|
2017-09-15 18:43:55 +08:00
|
|
|
// Anything outside the basic multilingual plane will be a surrogate pair
|
|
|
|
const SURROGATE_PAIR_PATTERN = /([\ud800-\udbff])([\udc00-\udfff])/;
|
|
|
|
// And there a bunch more symbol characters that emojione has within the
|
|
|
|
// BMP, so this includes the ranges from 'letterlike symbols' to
|
|
|
|
// 'miscellaneous symbols and arrows' which should catch all of them
|
|
|
|
// (with plenty of false positives, but that's OK)
|
|
|
|
const SYMBOL_PATTERN = /([\u2100-\u2bff])/;
|
|
|
|
|
|
|
|
// And this is emojione's complete regex
|
2016-07-05 12:28:28 +08:00
|
|
|
const EMOJI_REGEX = new RegExp(emojione.unicodeRegexp+"+", "gi");
|
2017-03-03 23:46:13 +08:00
|
|
|
const COLOR_REGEX = /^#[0-9a-fA-F]{6}$/;
|
2015-11-27 23:02:32 +08:00
|
|
|
|
2017-09-09 06:05:27 +08:00
|
|
|
/*
|
|
|
|
* Return true if the given string contains emoji
|
|
|
|
* Uses a much, much simpler regex than emojione's so will give false
|
|
|
|
* positives, but useful for fast-path testing strings to see if they
|
|
|
|
* need emojification.
|
|
|
|
* unicodeToImage uses this function.
|
|
|
|
*/
|
|
|
|
export function containsEmoji(str) {
|
2017-09-15 18:43:55 +08:00
|
|
|
return SURROGATE_PAIR_PATTERN.test(str) || SYMBOL_PATTERN.test(str);
|
2017-09-09 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 00:10:05 +08:00
|
|
|
/* modified from https://github.com/Ranks/emojione/blob/master/lib/js/emojione.js
|
|
|
|
* because we want to include emoji shortnames in title text
|
|
|
|
*/
|
2017-09-15 19:03:32 +08:00
|
|
|
function unicodeToImage(str) {
|
2017-07-12 01:27:35 +08:00
|
|
|
let replaceWith, unicode, alt, short, fname;
|
2016-08-10 00:10:05 +08:00
|
|
|
const mappedUnicode = emojione.mapUnicodeToShort();
|
|
|
|
|
|
|
|
str = str.replace(emojione.regUnicode, function(unicodeChar) {
|
|
|
|
if ( (typeof unicodeChar === 'undefined') || (unicodeChar === '') || (!(unicodeChar in emojione.jsEscapeMap)) ) {
|
|
|
|
// if the unicodeChar doesnt exist just return the entire match
|
|
|
|
return unicodeChar;
|
2017-10-12 00:56:17 +08:00
|
|
|
} else {
|
2016-08-10 00:10:05 +08:00
|
|
|
// get the unicode codepoint from the actual char
|
|
|
|
unicode = emojione.jsEscapeMap[unicodeChar];
|
|
|
|
|
2017-07-12 01:27:35 +08:00
|
|
|
short = mappedUnicode[unicode];
|
|
|
|
fname = emojione.emojioneList[short].fname;
|
|
|
|
|
2016-08-10 00:10:05 +08:00
|
|
|
// depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname
|
|
|
|
alt = (emojione.unicodeAlt) ? emojione.convert(unicode.toUpperCase()) : mappedUnicode[unicode];
|
|
|
|
const title = mappedUnicode[unicode];
|
|
|
|
|
2017-07-12 01:27:35 +08:00
|
|
|
replaceWith = `<img class="mx_emojione" title="${title}" alt="${alt}" src="${emojione.imagePathSVG}${fname}.svg${emojione.cacheBustParam}"/>`;
|
2016-08-10 00:10:05 +08:00
|
|
|
return replaceWith;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return str;
|
2016-09-16 23:02:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:50:13 +08:00
|
|
|
/**
|
|
|
|
* Given one or more unicode characters (represented by unicode
|
|
|
|
* character number), return an image node with the corresponding
|
|
|
|
* emoji.
|
|
|
|
*
|
|
|
|
* @param alt {string} String to use for the image alt text
|
2017-04-21 18:37:08 +08:00
|
|
|
* @param useSvg {boolean} Whether to use SVG image src. If False, PNG will be used.
|
2017-03-14 19:50:13 +08:00
|
|
|
* @param unicode {integer} One or more integers representing unicode characters
|
|
|
|
* @returns A img node with the corresponding emoji
|
|
|
|
*/
|
2017-04-21 18:37:08 +08:00
|
|
|
export function charactersToImageNode(alt, useSvg, ...unicode) {
|
2017-03-14 19:50:13 +08:00
|
|
|
const fileName = unicode.map((u) => {
|
|
|
|
return u.toString(16);
|
|
|
|
}).join('-');
|
2017-04-21 18:37:08 +08:00
|
|
|
const path = useSvg ? emojione.imagePathSVG : emojione.imagePathPNG;
|
|
|
|
const fileType = useSvg ? 'svg' : 'png';
|
|
|
|
return <img
|
|
|
|
alt={alt}
|
|
|
|
src={`${path}${fileName}.${fileType}${emojione.cacheBustParam}`}
|
|
|
|
/>;
|
2017-03-14 19:50:13 +08:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:37:08 +08:00
|
|
|
|
2017-06-28 18:49:50 +08:00
|
|
|
export function processHtmlForSending(html: string): string {
|
2016-09-16 23:02:08 +08:00
|
|
|
const contentDiv = document.createElement('div');
|
|
|
|
contentDiv.innerHTML = html;
|
|
|
|
|
|
|
|
if (contentDiv.children.length === 0) {
|
|
|
|
return contentDiv.innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
let contentHTML = "";
|
2017-06-30 01:08:57 +08:00
|
|
|
for (let i=0; i < contentDiv.children.length; i++) {
|
2016-09-16 23:02:08 +08:00
|
|
|
const element = contentDiv.children[i];
|
|
|
|
if (element.tagName.toLowerCase() === 'p') {
|
2017-06-30 01:08:57 +08:00
|
|
|
contentHTML += element.innerHTML;
|
|
|
|
// Don't add a <br /> for the last <p>
|
|
|
|
if (i !== contentDiv.children.length - 1) {
|
|
|
|
contentHTML += '<br />';
|
|
|
|
}
|
2017-06-28 21:27:24 +08:00
|
|
|
} else if (element.tagName.toLowerCase() === 'pre') {
|
2017-06-28 21:29:53 +08:00
|
|
|
// Replace "<br>\n" with "\n" within `<pre>` tags because the <br> is
|
|
|
|
// redundant. This is a workaround for a bug in draft-js-export-html:
|
2017-06-28 21:27:24 +08:00
|
|
|
// https://github.com/sstur/draft-js-export-html/issues/62
|
|
|
|
contentHTML += '<pre>' +
|
|
|
|
element.innerHTML.replace(/<br>\n/g, '\n').trim() +
|
|
|
|
'</pre>';
|
2016-09-16 23:02:08 +08:00
|
|
|
} else {
|
|
|
|
const temp = document.createElement('div');
|
|
|
|
temp.appendChild(element.cloneNode(true));
|
|
|
|
contentHTML += temp.innerHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentHTML;
|
|
|
|
}
|
2016-08-10 00:10:05 +08:00
|
|
|
|
2017-06-24 00:02:54 +08:00
|
|
|
/*
|
|
|
|
* Given an untrusted HTML string, return a React node with an sanitized version
|
|
|
|
* of that HTML.
|
|
|
|
*/
|
|
|
|
export function sanitizedHtmlNode(insaneHtml) {
|
2017-09-13 19:04:46 +08:00
|
|
|
const saneHtml = sanitizeHtml(insaneHtml, sanitizeHtmlParams);
|
2017-06-24 00:02:54 +08:00
|
|
|
|
|
|
|
return <div dangerouslySetInnerHTML={{ __html: saneHtml }} dir="auto" />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sanitizeHtmlParams = {
|
2015-11-27 23:02:32 +08:00
|
|
|
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
|
2017-09-13 19:04:46 +08:00
|
|
|
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'sup', 'sub',
|
2016-04-03 00:45:29 +08:00
|
|
|
'nl', 'li', 'b', 'i', 'u', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
|
2017-04-13 21:08:19 +08:00
|
|
|
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'span', 'img',
|
2015-11-27 23:02:32 +08:00
|
|
|
],
|
|
|
|
allowedAttributes: {
|
|
|
|
// custom ones first:
|
2017-03-02 19:36:56 +08:00
|
|
|
font: ['color', 'data-mx-bg-color', 'data-mx-color', 'style'], // custom to matrix
|
|
|
|
span: ['data-mx-bg-color', 'data-mx-color', 'style'], // custom to matrix
|
2017-01-20 22:22:27 +08:00
|
|
|
a: ['href', 'name', 'target', 'rel'], // remote target: custom to matrix
|
2017-07-24 11:00:36 +08:00
|
|
|
img: ['src', 'width', 'height', 'alt', 'title'],
|
2017-04-02 18:19:50 +08:00
|
|
|
ol: ['start'],
|
2017-06-12 08:03:38 +08:00
|
|
|
code: ['class'], // We don't actually allow all classes, we filter them in transformTags
|
2015-11-27 23:02:32 +08:00
|
|
|
},
|
|
|
|
// Lots of these won't come up by default because we don't allow them
|
2017-01-20 22:22:27 +08:00
|
|
|
selfClosing: ['img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta'],
|
2015-11-27 23:02:32 +08:00
|
|
|
// URL schemes we permit
|
2017-08-15 01:42:00 +08:00
|
|
|
allowedSchemes: ['http', 'https', 'ftp', 'mailto', 'magnet'],
|
2016-09-21 23:25:18 +08:00
|
|
|
|
2017-02-19 09:04:42 +08:00
|
|
|
allowProtocolRelative: false,
|
2016-08-16 04:37:26 +08:00
|
|
|
|
2015-11-27 23:02:32 +08:00
|
|
|
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-08-28 08:55:42 +08:00
|
|
|
if (attribs.href) {
|
|
|
|
attribs.target = '_blank'; // by default
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let m;
|
2016-08-28 08:55:42 +08:00
|
|
|
// FIXME: horrible duplication with linkify-matrix
|
|
|
|
m = attribs.href.match(linkifyMatrix.VECTOR_URL_PATTERN);
|
|
|
|
if (m) {
|
|
|
|
attribs.href = m[1];
|
|
|
|
delete attribs.target;
|
2017-10-12 00:56:17 +08:00
|
|
|
} else {
|
2017-05-15 08:32:37 +08:00
|
|
|
m = attribs.href.match(linkifyMatrix.MATRIXTO_URL_PATTERN);
|
|
|
|
if (m) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const entity = m[1];
|
2017-05-15 08:32:37 +08:00
|
|
|
if (entity[0] === '@') {
|
|
|
|
attribs.href = '#/user/' + entity;
|
2017-10-12 00:56:17 +08:00
|
|
|
} else if (entity[0] === '#' || entity[0] === '!') {
|
2017-05-15 08:32:37 +08:00
|
|
|
attribs.href = '#/room/' + entity;
|
|
|
|
}
|
|
|
|
delete attribs.target;
|
2016-08-28 08:55:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-20 11:05:07 +08:00
|
|
|
}
|
2016-08-16 04:37:26 +08:00
|
|
|
attribs.rel = 'noopener'; // https://mathiasbynens.github.io/rel-noopener/
|
2017-10-12 00:56:17 +08:00
|
|
|
return { tagName: tagName, attribs: attribs };
|
2016-03-20 11:05:07 +08:00
|
|
|
},
|
2017-07-10 23:27:23 +08:00
|
|
|
'img': function(tagName, attribs) {
|
2017-07-11 00:48:01 +08:00
|
|
|
// Strip out imgs that aren't `mxc` here instead of using allowedSchemesByTag
|
|
|
|
// because transformTags is used _before_ we filter by allowedSchemesByTag and
|
|
|
|
// we don't want to allow images with `https?` `src`s.
|
2017-07-11 00:44:49 +08:00
|
|
|
if (!attribs.src.startsWith('mxc://')) {
|
|
|
|
return { tagName, attribs: {}};
|
2017-07-10 23:27:23 +08:00
|
|
|
}
|
2017-07-11 00:44:49 +08:00
|
|
|
attribs.src = MatrixClientPeg.get().mxcUrlToHttp(
|
|
|
|
attribs.src,
|
|
|
|
attribs.width || 800,
|
|
|
|
attribs.height || 600,
|
|
|
|
);
|
2017-07-10 23:27:23 +08:00
|
|
|
return { tagName: tagName, attribs: attribs };
|
|
|
|
},
|
2017-06-12 08:03:38 +08:00
|
|
|
'code': function(tagName, attribs) {
|
|
|
|
if (typeof attribs.class !== 'undefined') {
|
|
|
|
// Filter out all classes other than ones starting with language- for syntax highlighting.
|
2017-10-12 00:56:17 +08:00
|
|
|
const classes = attribs.class.split(/\s+/).filter(function(cl) {
|
2017-06-12 08:03:38 +08:00
|
|
|
return cl.startsWith('language-');
|
|
|
|
});
|
|
|
|
attribs.class = classes.join(' ');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
tagName: tagName,
|
|
|
|
attribs: attribs,
|
|
|
|
};
|
|
|
|
},
|
2017-02-27 19:23:37 +08:00
|
|
|
'*': function(tagName, attribs) {
|
2017-03-03 02:13:01 +08:00
|
|
|
// Delete any style previously assigned, style is an allowedTag for font and span
|
|
|
|
// because attributes are stripped after transforming
|
2017-03-02 19:36:56 +08:00
|
|
|
delete attribs.style;
|
|
|
|
|
|
|
|
// Sanitise and transform data-mx-color and data-mx-bg-color to their CSS
|
|
|
|
// equivalents
|
|
|
|
const customCSSMapper = {
|
|
|
|
'data-mx-color': 'color',
|
|
|
|
'data-mx-bg-color': 'background-color',
|
|
|
|
// $customAttributeKey: $cssAttributeKey
|
|
|
|
};
|
|
|
|
|
|
|
|
let style = "";
|
|
|
|
Object.keys(customCSSMapper).forEach((customAttributeKey) => {
|
|
|
|
const cssAttributeKey = customCSSMapper[customAttributeKey];
|
|
|
|
const customAttributeValue = attribs[customAttributeKey];
|
|
|
|
if (customAttributeValue &&
|
|
|
|
typeof customAttributeValue === 'string' &&
|
2017-03-03 01:02:00 +08:00
|
|
|
COLOR_REGEX.test(customAttributeValue)
|
2017-02-27 19:23:37 +08:00
|
|
|
) {
|
2017-03-02 19:36:56 +08:00
|
|
|
style += cssAttributeKey + ":" + customAttributeValue + ";";
|
2017-03-02 19:39:40 +08:00
|
|
|
delete attribs[customAttributeKey];
|
2017-01-12 00:35:37 +08:00
|
|
|
}
|
2017-03-02 19:36:56 +08:00
|
|
|
});
|
2017-01-12 00:35:37 +08:00
|
|
|
|
2017-03-02 19:36:56 +08:00
|
|
|
if (style) {
|
|
|
|
attribs.style = style;
|
2017-01-12 00:35:37 +08:00
|
|
|
}
|
|
|
|
|
2017-02-27 19:23:37 +08:00
|
|
|
return { tagName: tagName, attribs: attribs };
|
2017-01-12 00:35:37 +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) {
|
2017-10-12 00:56:17 +08:00
|
|
|
let lastOffset = 0;
|
|
|
|
let offset;
|
|
|
|
let nodes = [];
|
2015-11-29 11:22:01 +08:00
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const 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.
|
2017-10-12 00:56:17 +08:00
|
|
|
const endOffset = offset + safeHighlight.length;
|
2016-02-18 03:50:04 +08:00
|
|
|
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
|
2016-09-16 23:02:08 +08:00
|
|
|
if (lastOffset !== safeSnippet.length) {
|
|
|
|
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));
|
2017-10-12 00:56:17 +08:00
|
|
|
} else {
|
2015-11-29 21:00:58 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let span = "<span class=\""+this.highlightClass+"\">"
|
2016-02-18 03:50:04 +08:00
|
|
|
+ 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) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const key = this._key++;
|
2015-12-24 07:50:35 +08:00
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let node =
|
|
|
|
<span key={key} className={highlight ? this.highlightClass : null}>
|
2016-02-23 01:44:34 +08:00
|
|
|
{ snippet }
|
|
|
|
</span>;
|
2016-02-18 03:50:04 +08:00
|
|
|
|
|
|
|
if (highlight && this.highlightLink) {
|
2017-10-12 00:56:17 +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
|
|
|
/* 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-07-18 08:34:26 +08:00
|
|
|
* opts.highlightLink: optional href to add to highlighted words
|
2017-10-15 02:40:45 +08:00
|
|
|
* opts.disableBigEmoji: optional argument to disable the big emoji class.
|
2015-12-24 07:50:35 +08:00
|
|
|
*/
|
2017-10-15 02:40:45 +08:00
|
|
|
export function bodyToHtml(content, highlights, opts={}) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const isHtml = (content.format === "org.matrix.custom.html");
|
|
|
|
const body = isHtml ? content.formatted_body : escape(content.body);
|
2016-09-16 23:02:08 +08:00
|
|
|
|
2017-09-15 19:03:32 +08:00
|
|
|
let bodyHasEmoji = false;
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let safeBody;
|
2016-09-16 23:02:08 +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 {
|
|
|
|
if (highlights && highlights.length > 0) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const highlighter = new HtmlHighlighter("mx_EventTile_searchHighlight", opts.highlightLink);
|
|
|
|
const safeHighlights = highlights.map(function(highlight) {
|
2016-09-16 23:02:08 +08:00
|
|
|
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-09-16 23:02:08 +08:00
|
|
|
safeBody = sanitizeHtml(body, sanitizeHtmlParams);
|
2017-09-15 19:03:32 +08:00
|
|
|
bodyHasEmoji = containsEmoji(body);
|
|
|
|
if (bodyHasEmoji) safeBody = unicodeToImage(safeBody);
|
2017-10-12 00:56:17 +08:00
|
|
|
} finally {
|
2016-09-16 23:02:08 +08:00
|
|
|
delete sanitizeHtmlParams.textFilter;
|
|
|
|
}
|
2016-07-05 06:34:57 +08:00
|
|
|
|
2017-09-15 19:03:32 +08:00
|
|
|
let emojiBody = false;
|
2017-10-15 02:40:45 +08:00
|
|
|
if (!opts.disableBigEmoji && bodyHasEmoji) {
|
2017-09-15 19:03:32 +08:00
|
|
|
EMOJI_REGEX.lastIndex = 0;
|
2017-10-12 00:56:17 +08:00
|
|
|
const contentBodyTrimmed = content.body !== undefined ? content.body.trim() : '';
|
|
|
|
const match = EMOJI_REGEX.exec(contentBodyTrimmed);
|
2017-09-15 19:03:32 +08:00
|
|
|
emojiBody = match && match[0] && match[0].length === contentBodyTrimmed.length;
|
|
|
|
}
|
2015-11-27 23:02:32 +08:00
|
|
|
|
2016-09-16 23:02:08 +08:00
|
|
|
const className = classNames({
|
|
|
|
'mx_EventTile_body': true,
|
|
|
|
'mx_EventTile_bigEmoji': emojiBody,
|
|
|
|
'markdown-body': isHtml,
|
|
|
|
});
|
2017-05-29 23:44:39 +08:00
|
|
|
return <span className={className} dangerouslySetInnerHTML={{ __html: safeBody }} dir="auto" />;
|
2016-09-16 23:02:08 +08:00
|
|
|
}
|
2015-11-27 23:02:32 +08:00
|
|
|
|
2016-09-16 23:02:08 +08:00
|
|
|
export function emojifyText(text) {
|
|
|
|
return {
|
|
|
|
__html: unicodeToImage(escape(text)),
|
|
|
|
};
|
|
|
|
}
|