|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var React = require('react'); |
| 20 | +var sanitizeHtml = require('sanitize-html'); |
| 21 | +var highlight = require('highlight.js'); |
| 22 | + |
| 23 | +var sanitizeHtmlParams = { |
| 24 | + allowedTags: [ |
| 25 | + 'font', // custom to matrix. deliberately no h1/h2 to stop people shouting. |
| 26 | + 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', |
| 27 | + 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', |
| 28 | + 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre' |
| 29 | + ], |
| 30 | + allowedAttributes: { |
| 31 | + // custom ones first: |
| 32 | + font: [ 'color' ], // custom to matrix |
| 33 | + a: [ 'href', 'name', 'target' ], // remote target: custom to matrix |
| 34 | + // We don't currently allow img itself by default, but this |
| 35 | + // would make sense if we did |
| 36 | + img: [ 'src' ], |
| 37 | + }, |
| 38 | + // Lots of these won't come up by default because we don't allow them |
| 39 | + selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ], |
| 40 | + // URL schemes we permit |
| 41 | + allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ], |
| 42 | + allowedSchemesByTag: {}, |
| 43 | + |
| 44 | + transformTags: { // custom to matrix |
| 45 | + // add blank targets to all hyperlinks |
| 46 | + 'a': sanitizeHtml.simpleTransform('a', { target: '_blank'} ) |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +module.exports = { |
| 51 | + bodyToHtml: function(content, searchTerm) { |
| 52 | + var originalBody = content.body; |
| 53 | + var body; |
| 54 | + |
| 55 | + if (searchTerm) { |
| 56 | + var lastOffset = 0; |
| 57 | + var bodyList = []; |
| 58 | + var k = 0; |
| 59 | + var offset; |
| 60 | + |
| 61 | + // XXX: rather than searching for the search term in the body, |
| 62 | + // we should be looking at the match delimiters returned by the FTS engine |
| 63 | + if (content.format === "org.matrix.custom.html") { |
| 64 | + |
| 65 | + var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams); |
| 66 | + var safeSearchTerm = sanitizeHtml(searchTerm, sanitizeHtmlParams); |
| 67 | + while ((offset = safeBody.indexOf(safeSearchTerm, lastOffset)) >= 0) { |
| 68 | + // FIXME: we need to apply the search highlighting to only the text elements of HTML, which means |
| 69 | + // hooking into the sanitizer parser rather than treating it as a string. Otherwise |
| 70 | + // the act of highlighting a <b/> or whatever will break the HTML badly. |
| 71 | + bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset, offset) }} />); |
| 72 | + bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeSearchTerm }} className="mx_MessageTile_searchHighlight" />); |
| 73 | + lastOffset = offset + safeSearchTerm.length; |
| 74 | + } |
| 75 | + bodyList.push(<span className="markdown-body" key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset) }} />); |
| 76 | + } |
| 77 | + else { |
| 78 | + while ((offset = originalBody.indexOf(searchTerm, lastOffset)) >= 0) { |
| 79 | + bodyList.push(<span key={ k++ } >{ originalBody.substring(lastOffset, offset) }</span>); |
| 80 | + bodyList.push(<span key={ k++ } className="mx_MessageTile_searchHighlight">{ searchTerm }</span>); |
| 81 | + lastOffset = offset + searchTerm.length; |
| 82 | + } |
| 83 | + bodyList.push(<span key={ k++ }>{ originalBody.substring(lastOffset) }</span>); |
| 84 | + } |
| 85 | + body = bodyList; |
| 86 | + } |
| 87 | + else { |
| 88 | + if (content.format === "org.matrix.custom.html") { |
| 89 | + var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams); |
| 90 | + body = <span className="markdown-body" dangerouslySetInnerHTML={{ __html: safeBody }} />; |
| 91 | + } |
| 92 | + else { |
| 93 | + body = originalBody; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return body; |
| 98 | + }, |
| 99 | + |
| 100 | + highlightDom: function(element) { |
| 101 | + var blocks = element.getElementsByTagName("code"); |
| 102 | + for (var i = 0; i < blocks.length; i++) { |
| 103 | + highlight.highlightBlock(blocks[i]); |
| 104 | + } |
| 105 | + }, |
| 106 | + |
| 107 | +} |
| 108 | + |
0 commit comments