@@ -32,45 +32,49 @@ module.exports = React.createClass({
3232 displayName : 'MTextTile' ,
3333 mixins : [ MTextTileController ] ,
3434
35+ // FIXME: this entire class is copy-pasted from MTextTile :(
3536 render : function ( ) {
3637 var content = this . props . mxEvent . getContent ( ) ;
37- var body = content . body ;
38-
39- if ( content . format === "org.matrix.custom.html" ) {
40- body = sanitizeHtml ( content . formatted_body , sanitizeHtmlParams ) ;
41- }
38+ var originalBody = content . body ;
4239
4340 if ( this . props . searchTerm ) {
4441 var lastOffset = 0 ;
4542 var bodyList = [ ] ;
4643 var k = 0 ;
4744 var offset ;
48- // XXX: this probably doesn't handle stemming very well.
49- while ( ( offset = body . indexOf ( this . props . searchTerm , lastOffset ) ) >= 0 ) {
50- if ( content . format === "org.matrix.custom.html" ) {
45+
46+ // XXX: rather than searching for the search term in the body,
47+ // we should be looking at the match delimiters returned by the FTS engine
48+ if ( content . format === "org.matrix.custom.html" ) {
49+ var safeBody = sanitizeHtml ( content . formatted_body , sanitizeHtmlParams ) ;
50+ var safeSearchTerm = sanitizeHtml ( this . props . searchTerm , sanitizeHtmlParams ) ;
51+ while ( ( offset = safeBody . indexOf ( safeSearchTerm , lastOffset ) ) >= 0 ) {
5152 // FIXME: we need to apply the search highlighting to only the text elements of HTML, which means
5253 // hooking into the sanitizer parser rather than treating it as a string. Otherwise
5354 // the act of highlighting a <b/> or whatever will break the HTML badly.
54- bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : body . substring ( lastOffset , offset ) } } /> ) ;
55- bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : this . props . searchTerm } } className = "mx_MessageTile_searchHighlight" /> ) ;
56- }
57- else {
58- bodyList . push ( < span key = { k ++ } > { body . substring ( lastOffset , offset ) } </ span > ) ;
59- bodyList . push ( < span key = { k ++ } className = "mx_MessageTile_searchHighlight" > { this . props . searchTerm } </ span > ) ;
55+ bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : safeBody . substring ( lastOffset , offset ) } } /> ) ;
56+ bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : safeSearchTerm } } className = "mx_MessageTile_searchHighlight" /> ) ;
57+ lastOffset = offset + safeSearchTerm . length ;
6058 }
61- lastOffset = offset + this . props . searchTerm . length ;
62- }
63- if ( content . format === "org.matrix.custom.html" ) {
64- bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : body . substring ( lastOffset ) } } /> ) ;
59+ bodyList . push ( < span key = { k ++ } dangerouslySetInnerHTML = { { __html : safeBody . substring ( lastOffset ) } } /> ) ;
6560 }
6661 else {
67- bodyList . push ( < span key = { k ++ } > { body . substring ( lastOffset ) } </ span > ) ;
62+ while ( ( offset = originalBody . indexOf ( this . props . searchTerm , lastOffset ) ) >= 0 ) {
63+ bodyList . push ( < span key = { k ++ } > { originalBody . substring ( lastOffset , offset ) } </ span > ) ;
64+ bodyList . push ( < span key = { k ++ } className = "mx_MessageTile_searchHighlight" > { this . props . searchTerm } </ span > ) ;
65+ lastOffset = offset + this . props . searchTerm . length ;
66+ }
67+ bodyList . push ( < span key = { k ++ } > { originalBody . substring ( lastOffset ) } </ span > ) ;
6868 }
6969 body = bodyList ;
7070 }
7171 else {
7272 if ( content . format === "org.matrix.custom.html" ) {
73- body = < span dangerouslySetInnerHTML = { { __html : body } } /> ;
73+ var safeBody = sanitizeHtml ( content . formatted_body , sanitizeHtmlParams ) ;
74+ body = < span dangerouslySetInnerHTML = { { __html : safeBody } } /> ;
75+ }
76+ else {
77+ body = originalBody ;
7478 }
7579 }
7680
0 commit comments