From e455bee39f2480782d350f156043141fb6bdab26 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 20 Sep 2016 11:59:42 -0400 Subject: PLT-4096 Fixed highlighting of hashtags in search (#4043) * PLT-4096 Fixed highlighting of hashtags in search * Added unit tests for hashtag rendering on the client --- webapp/tests/formatting_hashtags.test.jsx | 163 ++++++++++++++++++++++++++++++ webapp/utils/text_formatting.jsx | 4 +- 2 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 webapp/tests/formatting_hashtags.test.jsx diff --git a/webapp/tests/formatting_hashtags.test.jsx b/webapp/tests/formatting_hashtags.test.jsx new file mode 100644 index 000000000..37b84a4a8 --- /dev/null +++ b/webapp/tests/formatting_hashtags.test.jsx @@ -0,0 +1,163 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; + +import * as TextFormatting from 'utils/text_formatting.jsx'; + +describe('TextFormatting.Hashtags', function() { + this.timeout(10000); + + it('Not hashtags', function(done) { + assert.equal( + TextFormatting.formatText('# hashtag').trim(), + '

hashtag

' + ); + + assert.equal( + TextFormatting.formatText('#ab').trim(), + '

#ab

' + ); + + assert.equal( + TextFormatting.formatText('#123test').trim(), + '

#123test

' + ); + + done(); + }); + + it('Hashtags', function(done) { + assert.equal( + TextFormatting.formatText('#test').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('#test123').trim(), + "

#test123

" + ); + + assert.equal( + TextFormatting.formatText('#test-test').trim(), + "

#test-test

" + ); + + assert.equal( + TextFormatting.formatText('#test_test').trim(), + "

#test_test

" + ); + + assert.equal( + TextFormatting.formatText('#test.test').trim(), + "

#test.test

" + ); + + assert.equal( + TextFormatting.formatText('#test1/#test2').trim(), + "

#test1/#test2

" + ); + + assert.equal( + TextFormatting.formatText('(#test)').trim(), + "

(#test)

" + ); + + assert.equal( + TextFormatting.formatText('#test-').trim(), + "

#test-

" + ); + + assert.equal( + TextFormatting.formatText('#test.').trim(), + "

#test.

" + ); + + // Known issue, trailing underscore is captured by the clientside regex but not the serverside one + assert.equal( + TextFormatting.formatText('#test_').trim(), + "

#test_

" + ); + + assert.equal( + TextFormatting.formatText('This is a sentence #test containing a hashtag').trim(), + "

This is a sentence #test containing a hashtag

" + ); + + done(); + }); + + it('Formatted hashtags', function(done) { + assert.equal( + TextFormatting.formatText('*#test*').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('_#test_').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('**#test**').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('__#test__').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('~~#test~~').trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('`#test`').trim(), + '

' + + '' + + '' + + '#test' + + '' + + '' + + '

' + ); + + assert.equal( + TextFormatting.formatText('[this is a link #test](example.com)').trim(), + '

this is a link #test

' + ); + + done(); + }); + + it('Searching for hashtags', function(done) { + assert.equal( + TextFormatting.formatText('#test', {searchTerm: 'test'}).trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('#test', {searchTerm: '#test'}).trim(), + "

#test

" + ); + + assert.equal( + TextFormatting.formatText('#foo/#bar', {searchTerm: '#foo'}).trim(), + "

#foo/#bar

" + ); + + assert.equal( + TextFormatting.formatText('#foo/#bar', {searchTerm: 'bar'}).trim(), + "

#foo/#bar

" + ); + + assert.equal( + TextFormatting.formatText('not#test', {searchTerm: '#test'}).trim(), + '

not#test

' + ); + + done(); + }); +}); diff --git a/webapp/utils/text_formatting.jsx b/webapp/utils/text_formatting.jsx index 174620d47..23e286b45 100644 --- a/webapp/utils/text_formatting.jsx +++ b/webapp/utils/text_formatting.jsx @@ -415,8 +415,8 @@ function convertSearchTermToRegex(term) { pattern = '()(' + escapeRegex(term.replace(/\*/g, '')) + ')'; } else if (term.endsWith('*')) { pattern = '\\b()(' + escapeRegex(term.substring(0, term.length - 1)) + ')'; - } else if (term.startsWith('@')) { - // needs special handling of the first boundary because a word boundary doesn't work before an @ sign + } else if (term.startsWith('@') || term.startsWith('#')) { + // needs special handling of the first boundary because a word boundary doesn't work before a symbol pattern = '(\\W|^)(' + escapeRegex(term) + ')\\b'; } else { pattern = '\\b()(' + escapeRegex(term) + ')\\b'; -- cgit v1.2.3-1-g7c22