This commit is contained in:
Colton Clemmer 2023-09-03 10:37:03 -05:00
parent d6873ad05d
commit 3c72fc867c
2 changed files with 66 additions and 58 deletions

View file

@ -129,10 +129,13 @@ std::vector<UnicodeRange> alphaNumeric = {
{U'\u0030', U'\u0039'}, // Numbers {U'\u0030', U'\u0039'}, // Numbers
}; };
bool isInUnicodeRange(const QChar& ch, std::vector<UnicodeRange> ranges) { bool isInUnicodeRange(const QChar &ch, std::vector<UnicodeRange> ranges)
{
ushort unicodeValue = ch.unicode(); ushort unicodeValue = ch.unicode();
for (const auto& range : ranges) { for (const auto &range : ranges)
if (unicodeValue >= range.first && unicodeValue <= range.second) { {
if (unicodeValue >= range.first && unicodeValue <= range.second)
{
return true; return true;
} }
} }
@ -147,7 +150,8 @@ QString sanitizeUrl(const QString &unparsedString)
QString sanitizedUrl; QString sanitizedUrl;
for (const QChar &c : unparsedString) for (const QChar &c : unparsedString)
{ {
if (isInUnicodeRange(c, alphaNumeric) || isInUnicodeRange(c, emojiRanges)) if (isInUnicodeRange(c, alphaNumeric) ||
isInUnicodeRange(c, emojiRanges))
{ {
sanitizedUrl.append(c); sanitizedUrl.append(c);
continue; continue;

View file

@ -34,9 +34,11 @@ struct SanitizeCheck {
TEST(LinkParser, parseDomainLinks) TEST(LinkParser, parseDomainLinks)
{ {
const QList<SanitizeCheck> sanitizeCases = { const QList<SanitizeCheck> sanitizeCases = {
{ "TW❘TCH.tv", "TW❘TCH.tv" "" }, // contains dingbat {"TW❘TCH.tv", "TW❘TCH.tv"
""}, // contains dingbat
{"(twitch.tv/foo)", "twitch.tv", "/foo"}, {"(twitch.tv/foo)", "twitch.tv", "/foo"},
{"t🤪w🤪i🤪t🤪c🤪h🤪.tv/foo", "t🤪w🤪i🤪t🤪c🤪h🤪.tv", "/foo" }, {"t🤪w🤪i🤪t🤪c🤪h🤪.tv/foo",
"t🤪w🤪i🤪t🤪c🤪h🤪.tv", "/foo"},
{"https://🏹.to/bar", "🏹.to", "/bar"}, {"https://🏹.to/bar", "🏹.to", "/bar"},
{"😀.com/baz", "😀.com", "/baz"}, // Emoticon {"😀.com/baz", "😀.com", "/baz"}, // Emoticon
{"❀.com/baz", "❀.com", "/baz"}, // Dingbat {"❀.com/baz", "❀.com", "/baz"}, // Dingbat
@ -50,7 +52,8 @@ TEST(LinkParser, parseDomainLinks)
ASSERT_TRUE(p.result().has_value()) << c.testValue.toStdString(); ASSERT_TRUE(p.result().has_value()) << c.testValue.toStdString();
const auto &r = *p.result(); const auto &r = *p.result();
std::ostringstream ss; std::ostringstream ss;
ss << "Expected: " << c.expectedHost.toStdString() << "\nResult: " << r.host.toString().toStdString(); ss << "Expected: " << c.expectedHost.toStdString()
<< "\nResult: " << r.host.toString().toStdString();
ASSERT_EQ(c.expectedHost, r.host) << ss.str(); ASSERT_EQ(c.expectedHost, r.host) << ss.str();
ASSERT_EQ(c.expectedRest, r.rest) << c.expectedRest.toStdString(); ASSERT_EQ(c.expectedRest, r.rest) << c.expectedRest.toStdString();
} }
@ -160,7 +163,8 @@ TEST(LinkParser, doesntParseInvalidIpv4Links)
TEST(LinkParser, doesntParseInvalidLinks) TEST(LinkParser, doesntParseInvalidLinks)
{ {
const QStringList inputs = {"h://foo.com", const QStringList inputs = {
"h://foo.com",
"spotify:1234", "spotify:1234",
"ftp://chatterino.com", "ftp://chatterino.com",
"ftps://chatterino.com", "ftps://chatterino.com",