Fix name highlighting breaking emotes

Fix name highlighting when emotes are involved.  When the emotes were
moved to subfolders the channel name was put into the URL of the emote
image.  If a user with that name joined the chat, all of the emotes in
that folder highlighted the name in the URL, breaking the <img> tag.

This change removes the regex used to identify and replace the user's
name and instead works on whole words delimited by spaces.
This commit is contained in:
Zorchenhimer 2019-09-22 15:42:48 -04:00
parent 3ac5af4548
commit 7ac34c7d05
3 changed files with 48 additions and 12 deletions

View File

@ -255,15 +255,10 @@ func (cl *Client) Host() string {
}
func (cl *Client) setName(s string) error {
// Case-insensitive search. Match whole words only (`\b` is word boundary).
regex, err := regexp.Compile(fmt.Sprintf(`(?i)\b(%s|@%s)\b`, s, s))
if err != nil {
return fmt.Errorf("could not compile regex: %v", err)
}
cl.name = s
cl.regexName = regex
if cl.conn != nil {
cl.conn.clientName = s
}
return nil
}
@ -274,7 +269,18 @@ func (cl *Client) setColor(s string) error {
func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData {
data := chatData.Data.(common.DataMessage)
data.Message = cl.regexName.ReplaceAllString(data.Message, `<span class="mention">$1</span>`)
words := strings.Split(data.Message, " ")
newWords := []string{}
for _, word := range words {
if strings.ToLower(word) == strings.ToLower(cl.name) || strings.ToLower(word) == strings.ToLower("@"+cl.name) {
newWords = append(newWords, `<span class="mention">`+word+`</span>`)
} else {
newWords = append(newWords, word)
}
}
data.Message = strings.Join(newWords, " ")
chatData.Data = data
return chatData
}

View File

@ -1,6 +1,10 @@
package main
import "testing"
import (
"testing"
"github.com/zorchenhimer/MovieNight/common"
)
func TestClient_addSpoilerTag(t *testing.T) {
data := [][]string{
@ -21,3 +25,31 @@ func TestClient_addSpoilerTag(t *testing.T) {
}
}
}
// Name highlighting should not interfere with emotes
func TestClient_emoteHighlight(t *testing.T) {
data := [][]string{
{"zorchenhimer", `<span class="mention">zorchenhimer</span>`},
{"@zorchenhimer", `<span class="mention">@zorchenhimer</span>`},
{"Zorchenhimer", `<span class="mention">Zorchenhimer</span>`},
{"@Zorchenhimer", `<span class="mention">@Zorchenhimer</span>`},
{"hello zorchenhimer", `hello <span class="mention">zorchenhimer</span>`},
{"hello zorchenhimer ass", `hello <span class="mention">zorchenhimer</span> ass`},
{`<img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`, `<img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`},
{`zorchenhimer <img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`, `<span class="mention">zorchenhimer</span> <img src="/emotes/twitch/zorchenhimer/zorcheWhat.png" height="28px" title="zorcheWhat">`},
}
client, err := NewClient(nil, nil, "Zorchenhimer", "#9547ff")
if err != nil {
t.Errorf("Client init error: %v", err)
}
for _, d := range data {
chatData := client.replaceColorizedName(common.NewChatMessage(client.name, client.color, d[0], common.CmdlUser, common.MsgChat))
if chatData.Data.(common.DataMessage).Message != d[1] {
t.Errorf("\nExpected:\n\t%s\nReceived\n\t%s", d[1], chatData.Data.(common.DataMessage).Message)
} else {
t.Logf("Passed %s", d[0])
}
}
}

View File

@ -54,7 +54,6 @@ var ChatCommands = []ChatCommandNames{
CNStats,
// Mod
CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin,
CNSv,
CNPlaying,
CNUnmod,
@ -64,7 +63,6 @@ var ChatCommands = []ChatCommandNames{
CNPurge,
// Admin
CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, CNIP,
CNMod,
CNReloadPlayer,
CNReloadEmotes,