diff options
author | George Goldberg <george@gberg.me> | 2017-08-07 10:10:33 +0100 |
---|---|---|
committer | Saturnino Abril <saturnino.abril@gmail.com> | 2017-08-07 17:10:33 +0800 |
commit | f4638aecec2933f20efb4b95c2e0a90c22b47b43 (patch) | |
tree | c87065524a9c82209efc569f5fc2bf98ce914d2d /webapp/utils/text_formatting.jsx | |
parent | 9eb8d93a0d958b830b1f759dd0917629bd79c8e6 (diff) | |
download | chat-f4638aecec2933f20efb4b95c2e0a90c22b47b43.tar.gz chat-f4638aecec2933f20efb4b95c2e0a90c22b47b43.tar.bz2 chat-f4638aecec2933f20efb4b95c2e0a90c22b47b43.zip |
PLT-6609: Don't highlight #hashtag.dot when searching for #hashtag (#7109)
Diffstat (limited to 'webapp/utils/text_formatting.jsx')
-rw-r--r-- | webapp/utils/text_formatting.jsx | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/webapp/utils/text_formatting.jsx b/webapp/utils/text_formatting.jsx index 33cc3242c..4602a31b2 100644 --- a/webapp/utils/text_formatting.jsx +++ b/webapp/utils/text_formatting.jsx @@ -400,7 +400,10 @@ function convertSearchTermToRegex(term) { pattern = '\\b()(' + escapeRegex(term) + ')\\b'; } - return new RegExp(pattern, 'gi'); + return { + pattern: new RegExp(pattern, 'gi'), + term + }; } export function highlightSearchTerms(text, tokens, searchPatterns) { @@ -426,7 +429,21 @@ export function highlightSearchTerms(text, tokens, searchPatterns) { // highlight existing tokens matching search terms var newTokens = new Map(); for (const [alias, token] of tokens) { - if (pattern.test(token.originalText)) { + if (pattern.pattern.test(token.originalText)) { + // If it's a Hashtag, skip it unless the search term is an exact match. + let originalText = token.originalText; + if (originalText.startsWith('#')) { + originalText = originalText.substr(1); + } + let term = pattern.term; + if (term.startsWith('#')) { + term = term.substr(1); + } + + if (alias.startsWith('$MM_HASHTAG') && originalText !== term) { + continue; + } + const index = tokens.size + newTokens.size; const newAlias = `$MM_SEARCHTERM${index}`; @@ -438,10 +455,10 @@ export function highlightSearchTerms(text, tokens, searchPatterns) { output = output.replace(alias, newAlias); } - // The pattern regexes are global, so calling pattern.test() above alters their + // The pattern regexes are global, so calling pattern.pattern.test() above alters their // state. Reset lastIndex to 0 between calls to test() to ensure it returns the // same result every time it is called with the same value of token.originalText. - pattern.lastIndex = 0; + pattern.pattern.lastIndex = 0; } // the new tokens are stashed in a separate map since we can't add objects to a map during iteration @@ -449,7 +466,7 @@ export function highlightSearchTerms(text, tokens, searchPatterns) { tokens.set(newToken[0], newToken[1]); } - output = output.replace(pattern, replaceSearchTermWithToken); + output = output.replace(pattern.pattern, replaceSearchTermWithToken); } return output; |