From 43cc2792c9f9e7a10e1231c8337c4f094b3b82f6 Mon Sep 17 00:00:00 2001 From: Jan Visser Date: Fri, 21 Jul 2017 10:35:05 +0200 Subject: [PATCH] Take into account wordboundaries for custom highlighting --- client/js/options.js | 10 ++++++++++ client/js/render.js | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/client/js/options.js b/client/js/options.js index 20f94289..798da506 100644 --- a/client/js/options.js +++ b/client/js/options.js @@ -1,6 +1,7 @@ "use strict"; const $ = require("jquery"); +const escapeRegExp = require("lodash/escapeRegExp"); const settings = $("#settings"); const userStyles = $("#user-specified-css"); const storage = require("./localStorage"); @@ -98,6 +99,15 @@ settings.on("change", "input, select, textarea", function() { // otherwise, users get notifications for everything return h !== ""; }); + // Construct regex with wordboundary for every highlight item + const highlightsTokens = options.highlights.map(function(h) { + return escapeRegExp(h); + }); + if (highlightsTokens && highlightsTokens.length) { + module.exports.highlightsRE = new RegExp("\\b(?:" + highlightsTokens.join("|") + ")\\b", "i"); + } else { + module.exports.highlightsRE = null; + } } else if (name === "showSeconds") { chat.find(".msg > .time").each(function() { $(this).text(tz($(this).parent().data("time"))); diff --git a/client/js/render.js b/client/js/render.js index 246e62b2..e3e66bc8 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -65,9 +65,11 @@ function buildChatMessage(data) { const chan = chat.find(target); let template = "msg"; - if (!data.msg.highlight && !data.msg.self && (type === "message" || type === "notice") && options.highlights.some(function(h) { - return data.msg.text.toLocaleLowerCase().indexOf(h.toLocaleLowerCase()) > -1; - })) { + // See if any of the custom highlight regexes match + if (!data.msg.highlight && !data.msg.self + && options.highlightsRE + && (type === "message" || type === "notice") + && options.highlightsRE.exec(data.msg.text)) { data.msg.highlight = true; }