remove unused 'body' var; use a finally to clean up the temporary textfilter

This commit is contained in:
Matthew Hodgson 2016-02-11 14:03:48 +00:00
parent 92435c0865
commit 1c30640a92

View file

@ -136,29 +136,33 @@ module.exports = {
var isHtml = (content.format === "org.matrix.custom.html"); var isHtml = (content.format === "org.matrix.custom.html");
var safeBody, body; var safeBody;
if (isHtml) { if (isHtml) {
// XXX: We sanitize the HTML whilst also highlighting its text nodes, to avoid accidentally trying // 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 // 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 // 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 // by an attempt to search for 'foobar'. Then again, the search query probably wouldn't work either
if (highlights && highlights.length > 0) { try {
var highlighter = new Highlighter(isHtml, "mx_EventTile_searchHighlight", opts.onHighlightClick); if (highlights && highlights.length > 0) {
var safeHighlights = highlights.map(function(highlight) { var highlighter = new Highlighter(isHtml, "mx_EventTile_searchHighlight", opts.onHighlightClick);
return sanitizeHtml(highlight, sanitizeHtmlParams); 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) { // XXX: hacky bodge to temporarily apply a textFilter to the sanitizeHtmlParams structure.
return highlighter.applyHighlights(safeText, safeHighlights).map(function(span) { sanitizeHtmlParams.textFilter = function(safeText) {
// XXX: rather clunky conversion from the react nodes returned by applyHighlights return highlighter.applyHighlights(safeText, safeHighlights).map(function(span) {
// (which need to be nodes for the non-html highlighting case), to convert them // XXX: rather clunky conversion from the react nodes returned by applyHighlights
// back into raw HTML given that's what sanitize-html works in terms of. // (which need to be nodes for the non-html highlighting case), to convert them
return ReactDOMServer.renderToString(span); // back into raw HTML given that's what sanitize-html works in terms of.
}).join(''); return ReactDOMServer.renderToString(span);
}; }).join('');
};
}
safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
}
finally {
delete sanitizeHtmlParams.textFilter;
} }
safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
delete sanitizeHtmlParams.textFilter;
return <span className="markdown-body" dangerouslySetInnerHTML={{ __html: safeBody }} />; return <span className="markdown-body" dangerouslySetInnerHTML={{ __html: safeBody }} />;
} else { } else {
safeBody = content.body; safeBody = content.body;