Skip to content

Commit

Permalink
Don't rely on word boundary in mentions (#45)
Browse files Browse the repository at this point in the history
They don't work reliably with unicode characters even with u or v flags.
Also, usernames can end on non-word characters.
  • Loading branch information
eltoder authored Nov 30, 2024
1 parent 54cd73d commit 84bb130
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/components/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ const useStyles = makeStyles((theme) => ({
},
}));

const makeUserRE = (username) => {
const makeMentionRE = (username) => {
username = username.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
username = username.replace(/^anonymous /i, "($&)?");
return new RegExp(`@(all|${username})\\b`, "i");
return new RegExp(`@(all|${username})(\\W|$)`, "iu");
};

/** A chat sidebar element, opens lobby chat when the `gameId` prop is not set. */
Expand Down Expand Up @@ -106,10 +106,10 @@ function Chat({
[databasePath, messageLimit]
);
const messages = useFirebaseQuery(messagesQuery);
const userRE = useMemo(() => makeUserRE(user.name), [user.name]);
const mentionRE = useMemo(() => makeMentionRE(user.name), [user.name]);

const addMentioned = (cls, message) => {
return userRE.test(message) ? `${cls} ${classes.mentioned}` : cls;
return mentionRE.test(message) ? `${cls} ${classes.mentioned}` : cls;
};

function handleSubmit(event) {
Expand Down

0 comments on commit 84bb130

Please sign in to comment.