From 0c0453559974dcdd2a7fa6fb9c8d72fdbc4082c7 Mon Sep 17 00:00:00 2001
From: Pat Lathem <patl@codero.com>
Date: Sat, 24 Oct 2015 18:32:45 -0500
Subject: Remove trailing punctuation when parsing @username references

---
 web/react/utils/text_formatting.jsx | 60 +++++++++++++++++++++++++++++--------
 1 file changed, 47 insertions(+), 13 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index d79aeed68..9fd22e6b9 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -82,6 +82,7 @@ export function sanitizeHtml(text) {
     return output;
 }
 
+// Convert URLs into tokens
 function autolinkUrls(text, tokens) {
     function replaceUrlWithToken(autolinker, match) {
         const linkText = match.getMatchedText();
@@ -123,27 +124,60 @@ function autolinkUrls(text, tokens) {
 }
 
 function autolinkAtMentions(text, tokens) {
-    let output = text;
+    // Return true if provided character is punctuation
+    function isPunctuation(character) {
+        const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
+        return re.test(character);
+    }
 
-    function replaceAtMentionWithToken(fullMatch, prefix, mention, username) {
-        const usernameLower = username.toLowerCase();
-        if (Constants.SPECIAL_MENTIONS.indexOf(usernameLower) !== -1 || UserStore.getProfileByUsername(usernameLower)) {
-            const index = tokens.size;
-            const alias = `MM_ATMENTION${index}`;
-
-            tokens.set(alias, {
-                value: `<a class='mention-link' href='#' data-mention='${usernameLower}'>${mention}</a>`,
-                originalText: mention
-            });
+    // Test if provided text needs to be highlighted, special mention or current user
+    function mentionExists(u) {
+        return (Constants.SPECIAL_MENTIONS.indexOf(u) !== -1 || UserStore.getProfileByUsername(u));
+    }
+
+    function addToken(username, mention, extraText) {
+        const index = tokens.size;
+        const alias = `MM_ATMENTION${index}`;
+
+        tokens.set(alias, {
+            value: `<a class='mention-link' href='#' data-mention='${username}'>${mention}</a>${extraText}`,
+            originalText: mention
+        });
+        return alias;
+    }
 
+    function replaceAtMentionWithToken(fullMatch, prefix, mention, username) {
+        let usernameLower = username.toLowerCase();
+        
+        if (mentionExists(usernameLower)) {
+            // Exact match
+            const alias = addToken(usernameLower, mention, '');
             return prefix + alias;
+         }
+
+        // Not an exact match, attempt to truncate any punctuation to see if we can find a user
+        const originalUsername = usernameLower;
+    
+        for (let c = usernameLower.length; c > 0; c--) {
+            if (isPunctuation(usernameLower[c-1])) {
+                usernameLower = usernameLower.substring(0, c);
+
+                if (mentionExists(usernameLower)) {
+                    const extraText = originalUsername.substr(c);
+                    const alias = addToken(usernameLower, mention, extraText);
+                    return prefix + alias;
+                }
+            } else {
+                // If the last character is not punctuation, no point in going any further
+                break;
+            }
         }
 
         return fullMatch;
     }
 
-    output = output.replace(/(^|\s)(@([a-z0-9.\-_]*[a-z0-9]))/gi, replaceAtMentionWithToken);
-
+    let output = text;
+    output = output.replace(/(^|\s)(@([a-z0-9.\-_]*))/gi, replaceAtMentionWithToken);
     return output;
 }
 
-- 
cgit v1.2.3-1-g7c22


From 80e0a8db1d70ca387c654d9ac6bded0fb1e352a6 Mon Sep 17 00:00:00 2001
From: Pat Lathem <patl@codero.com>
Date: Sat, 24 Oct 2015 18:54:01 -0500
Subject: Fix off by one error

---
 web/react/utils/text_formatting.jsx | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index 9fd22e6b9..c49bdf916 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -148,23 +148,23 @@ function autolinkAtMentions(text, tokens) {
 
     function replaceAtMentionWithToken(fullMatch, prefix, mention, username) {
         let usernameLower = username.toLowerCase();
-        
+
         if (mentionExists(usernameLower)) {
             // Exact match
             const alias = addToken(usernameLower, mention, '');
             return prefix + alias;
-         }
+        }
 
         // Not an exact match, attempt to truncate any punctuation to see if we can find a user
         const originalUsername = usernameLower;
-    
+
         for (let c = usernameLower.length; c > 0; c--) {
-            if (isPunctuation(usernameLower[c-1])) {
-                usernameLower = usernameLower.substring(0, c);
+            if (isPunctuation(usernameLower[c - 1])) {
+                usernameLower = usernameLower.substring(0, c - 1);
 
                 if (mentionExists(usernameLower)) {
-                    const extraText = originalUsername.substr(c);
-                    const alias = addToken(usernameLower, mention, extraText);
+                    const extraText = originalUsername.substr(c - 1);
+                    const alias = addToken(usernameLower, '@' + usernameLower, extraText);
                     return prefix + alias;
                 }
             } else {
-- 
cgit v1.2.3-1-g7c22


From 3a588fbc18bb990e07e656a41d2f858fb9dc25e2 Mon Sep 17 00:00:00 2001
From: Pat Lathem <patl@codero.com>
Date: Sun, 25 Oct 2015 14:09:09 -0500
Subject: Fix highlighting of trailing punctuation for own username

---
 web/react/utils/text_formatting.jsx | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

(limited to 'web/react/utils/text_formatting.jsx')

diff --git a/web/react/utils/text_formatting.jsx b/web/react/utils/text_formatting.jsx
index c49bdf916..e47aca39b 100644
--- a/web/react/utils/text_formatting.jsx
+++ b/web/react/utils/text_formatting.jsx
@@ -140,8 +140,9 @@ function autolinkAtMentions(text, tokens) {
         const alias = `MM_ATMENTION${index}`;
 
         tokens.set(alias, {
-            value: `<a class='mention-link' href='#' data-mention='${username}'>${mention}</a>${extraText}`,
-            originalText: mention
+            value: `<a class='mention-link' href='#' data-mention='${username}'>${mention}</a>`,
+            originalText: mention,
+            extraText
         });
         return alias;
     }
@@ -194,10 +195,9 @@ function highlightCurrentMentions(text, tokens) {
             const newAlias = `MM_SELFMENTION${index}`;
 
             newTokens.set(newAlias, {
-                value: `<span class='mention-highlight'>${alias}</span>`,
+                value: `<span class='mention-highlight'>${alias}</span>` + token.extraText,
                 originalText: token.originalText
             });
-
             output = output.replace(alias, newAlias);
         }
     }
-- 
cgit v1.2.3-1-g7c22

