Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Mar 14, 2024
1 parent 4feb01d commit c0bf2f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/widgets/emojiregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const EmojiRegistry &EmojiRegistry::instance()

bool EmojiRegistry::isEmoji(const QString &code) const
{
auto cat = startCategory(&code);
auto cat = startCategory(code);
return cat == Category::Emoji || cat == Category::SkinTone;
// TODO check the whole code is emoji. not just start
}
Expand Down
18 changes: 10 additions & 8 deletions src/widgets/psirichtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,17 @@ static void appendTextHelper(QTextDocument *doc, QString text, QTextCursor &curs
// prepare images and remove insecure images
static QRegularExpression imgRe("<img[^>]+src\\s*=\\s*(\"[^\"]*\"|'[^']*')[^>]*>");
QString replace;
for (int pos = 0; (pos = imgRe.indexIn(text, pos)) != -1;) {
QRegularExpressionMatch match;
for (int pos = 0; (match = imgRe.match(text, pos)).hasMatch();) {
replace.clear();
QString imgSrc = imgRe.cap(1).mid(1, imgRe.cap(1).size() - 2);
QString imgSrc = match.captured(1).mid(1, match.captured(1).size() - 2);
QUrl imgSrcUrl = QUrl::fromEncoded(imgSrc.toLatin1());
if (imgSrcUrl.isValid()) {
if (imgSrcUrl.scheme() == "data") {
static QRegularExpression dataRe("^[a-zA-Z]+/[a-zA-Z]+;base64,([a-zA-Z0-9/=+%]+)$");
if (dataRe.indexIn(imgSrcUrl.path()) != -1) {
const QByteArray ba = QByteArray::fromBase64(dataRe.cap(1).toLatin1());
auto dataMatch = dataRe.match(imgSrcUrl.path());
if (dataMatch.hasMatch()) {
const QByteArray ba = QByteArray::fromBase64(dataMatch.captured(1).toLatin1());
if (!ba.isNull()) {
QImage image;
if (image.loadFromData(ba)) {
Expand All @@ -432,7 +434,7 @@ static void appendTextHelper(QTextDocument *doc, QString text, QTextCursor &curs
}
}
} else if (imgSrc.startsWith(":/") || (!imgSrcUrl.scheme().isEmpty() && imgSrcUrl.scheme() != "file")) {
pos += imgRe.matchedLength();
pos = match.capturedEnd();
continue;
} else {
// go here when scheme in ["", "file"] and its not resource
Expand All @@ -449,16 +451,16 @@ static void appendTextHelper(QTextDocument *doc, QString text, QTextCursor &curs
if (imgSrcUrl.scheme() == "file") {
replace = path;
} else {
pos += imgRe.matchedLength();
pos = match.capturedEnd();
continue;
}
}
}
}
if (replace.isEmpty()) {
text.remove(pos, imgRe.matchedLength());
text.remove(match.capturedStart(), match.capturedLength());
} else {
text.replace(imgRe.pos(1) + 1, imgSrc.size(), replace);
text.replace(match.capturedStart(1) + 1, imgSrc.size(), replace);
pos += replace.size() + 1;
}
}
Expand Down

0 comments on commit c0bf2f9

Please sign in to comment.