Fix/be respectful of special characters like exclamation marks in highlight phrases since they are also word boundaries (#1890)

* Add missing includes

We would normally have these included in another file already, or even
the precompiled headers, but having the files included here too makes
testing single parts easier.

* Modify the regex building of highlight phrases for non-regex phrases

For phrases like !test, the word boundary checking we did before was not
enough, so we now check for either a word boundary, a whitespace
character, or the line start/end.

* Add tests for ensuring I haven't fully broken the highlight system

* Add changelog entry
This commit is contained in:
pajlada
2020-08-22 09:37:03 -04:00
committed by GitHub
parent 17b26ef59c
commit 0cbddf7e9b
7 changed files with 222 additions and 7 deletions
+15 -4
View File
@@ -2,6 +2,13 @@
namespace chatterino {
namespace {
const QString REGEX_START_BOUNDARY("(\\b|\\s|^)");
const QString REGEX_END_BOUNDARY("(\\b|\\s|$)");
} // namespace
QColor HighlightPhrase::FALLBACK_HIGHLIGHT_COLOR = QColor(127, 63, 73, 127);
QColor HighlightPhrase::FALLBACK_REDEEMED_HIGHLIGHT_COLOR =
QColor(28, 126, 141, 90);
@@ -27,8 +34,10 @@ HighlightPhrase::HighlightPhrase(const QString &pattern, bool hasAlert,
, isRegex_(isRegex)
, isCaseSensitive_(isCaseSensitive)
, soundUrl_(soundUrl)
, regex_(isRegex_ ? pattern
: "\\b" + QRegularExpression::escape(pattern) + "\\b",
, regex_(isRegex_
? pattern
: REGEX_START_BOUNDARY + QRegularExpression::escape(pattern) +
REGEX_END_BOUNDARY,
QRegularExpression::UseUnicodePropertiesOption |
(isCaseSensitive_ ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption))
@@ -47,8 +56,10 @@ HighlightPhrase::HighlightPhrase(const QString &pattern, bool hasAlert,
, isCaseSensitive_(isCaseSensitive)
, soundUrl_(soundUrl)
, color_(color)
, regex_(isRegex_ ? pattern
: "\\b" + QRegularExpression::escape(pattern) + "\\b",
, regex_(isRegex_
? pattern
: REGEX_START_BOUNDARY + QRegularExpression::escape(pattern) +
REGEX_END_BOUNDARY,
QRegularExpression::UseUnicodePropertiesOption |
(isCaseSensitive_ ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption))
@@ -4,10 +4,14 @@
#include "util/RapidJsonSerializeQString.hpp"
#include "util/RapidjsonHelpers.hpp"
#include <QColor>
#include <QRegularExpression>
#include <QString>
#include <QUrl>
#include <pajlada/serialize.hpp>
#include <memory>
namespace chatterino {
class HighlightPhrase