Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ports fixes to the regex chat filter that we sent upstream #5155

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions tgui/packages/tgui-panel/chat/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class ChatRenderer {
const matchWord = setting.matchWord;
const matchCase = setting.matchCase;
const allowedRegex = /^[a-z0-9_\-$/^[\s\]\\]+$/gi;
const regexEscapeCharacters = /[!#$%^&*)(+=.<>{}[\]:;'"|~`_\-\\/]/g;
const lines = String(text)
.split(',')
.map((str) => str.trim())
Expand Down Expand Up @@ -228,19 +229,27 @@ class ChatRenderer {
if (!highlightWords) {
highlightWords = [];
}
// We're not going to let regex characters fuck up our RegEx operation.
line = line.replace(regexEscapeCharacters, '\\$&');

highlightWords.push(line);
}
}
const regexStr = regexExpressions.join('|');
const flags = 'g' + (matchCase ? '' : 'i');
// setting regex overrides matchword
if (regexStr) {
highlightRegex = new RegExp('(' + regexStr + ')', flags);
} else {
const pattern = `${matchWord ? '\\b' : ''}(${lines.join('|')})${
matchWord ? '\\b' : ''
}`;
highlightRegex = new RegExp(pattern, flags);
// We wrap this in a try-catch to ensure that broken regex doesn't break
// the entire chat.
try {
// setting regex overrides matchword
if (regexStr) {
highlightRegex = new RegExp('(' + regexStr + ')', flags);
} else {
const pattern = `${matchWord ? '\\b' : ''}(${highlightWords.join(
'|'
)})${matchWord ? '\\b' : ''}`;
highlightRegex = new RegExp(pattern, flags);
}
} catch {
// We just reset it if it's invalid.
highlightRegex = null;
}
// Lazy init
if (!this.highlightParsers) {
Expand Down
Loading