Merge pull request #1901 from thelounge/xpaw/fix-nicks-match

Do not match nicknames incorrectly as part of a bigger word
This commit is contained in:
Jérémie Astori 2017-12-27 14:32:59 -05:00 committed by GitHub
commit 835236f8ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 10 deletions

View File

@ -1,17 +1,25 @@
"use strict"; "use strict";
const nickRegExp = /([\w[\]\\`^{|}-]+)/g;
function findNames(text, users) { function findNames(text, users) {
const result = []; const result = [];
let index = -1;
users.forEach((nick) => { // Return early if we don't have any nicknames to find
index = text.indexOf(nick, ++index); if (users.length === 0) {
result.push({ return result;
start: index, }
end: index + nick.length,
nick: nick, let match;
}); while ((match = nickRegExp.exec(text))) {
}); if (users.indexOf(match[1]) > -1) {
result.push({
start: match.index,
end: match.index + match[1].length,
nick: match[1],
});
}
}
return result; return result;
} }

View File

@ -4,7 +4,7 @@ const Chan = require("../../models/chan");
const Msg = require("../../models/msg"); const Msg = require("../../models/msg");
const LinkPrefetch = require("./link"); const LinkPrefetch = require("./link");
const cleanIrcMessage = require("../../../client/js/libs/handlebars/ircmessageparser/cleanIrcMessage"); const cleanIrcMessage = require("../../../client/js/libs/handlebars/ircmessageparser/cleanIrcMessage");
const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]{4,})/g; const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g;
module.exports = function(irc, network) { module.exports = function(irc, network) {
const client = this; const client = this;

View File

@ -24,6 +24,36 @@ describe("findNames", () => {
expect(actual).to.deep.equal(expected); expect(actual).to.deep.equal(expected);
}); });
it("should not find nicks as part of a bigger string (issue #1776)", () => {
const input = "you're very unlucky, luck";
const expected = [
{
start: 21,
end: 25,
nick: "luck",
},
];
const nicks = ["luck"];
const actual = findNames(input, nicks);
expect(actual).to.deep.equal(expected);
});
it("should find nicks as short as one character (issue #1885)", () => {
const input = "aaa aa abc a";
const expected = [
{
start: 11,
end: 12,
nick: "a",
},
];
const nicks = ["a"];
const actual = findNames(input, nicks);
expect(actual).to.deep.equal(expected);
});
it("should find same nick multiple times", () => { it("should find same nick multiple times", () => {
const input = "xPaw xPaw xPaw"; const input = "xPaw xPaw xPaw";
const expected = [ const expected = [