Skip to content

Commit

Permalink
Fix buggy URL rendering with HTML strings
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBriza committed Jun 30, 2024
1 parent 8bbac1a commit 425c668
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 11 additions & 9 deletions modules/Lith/Core/formattedstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ QString FormattedString::Part::toHtml(QStringView fullText, const ColorTheme& th
}

QString finalText;
const auto urlThreshold = Lith::settingsGet()->shortenLongUrlsThresholdGet();
const auto urlShortenEnabled = Lith::settingsGet()->shortenLongUrlsGet();
if (urlThreshold > 0 && hyperlink && n > urlThreshold && urlShortenEnabled) {
const auto urlThreshold = trim;
if (urlThreshold > 0 && hyperlink && n > urlThreshold) {
auto url = QUrl(text(fullText, -1).toString());
auto scheme = url.scheme();
auto host = url.host();
Expand All @@ -123,7 +122,7 @@ QString FormattedString::Part::toHtml(QStringView fullText, const ColorTheme& th
} else {
// We'll show always show the host and the scheme.
const auto hostPrefix = scheme + QStringLiteral("://") + host + QStringLiteral("/");
constexpr auto ellipsis = QLatin1String("\u2026");
const auto ellipsis = QStringLiteral("\u2026");

// The threshold is so small that it doesn't even accomodate the hostPrefix. We'll just put the hostPrefix and
// ellipsis...
Expand All @@ -143,7 +142,7 @@ QString FormattedString::Part::toHtml(QStringView fullText, const ColorTheme& th
}
}
} else {
finalText = text(fullText, trim).toString();
finalText = text(fullText, -1).toString();
}
ret.append(finalText.toHtmlEscaped());

Expand Down Expand Up @@ -212,6 +211,7 @@ FormattedString::Part& FormattedString::addPart(QStringView s) {
auto& newPart = addPart();
newPart.pos = m_fullText.size();
newPart.n = s.size();
m_fullText += s;
return newPart;
}

Expand Down Expand Up @@ -321,15 +321,17 @@ void FormattedString::prune() {
int previousEnd = 0;
while (reIt.hasNext()) {
auto reMatch = reIt.next();
Part prefix {previousEnd, reMatch.capturedStart() - previousEnd};
Part url = Part {reMatch.capturedStart(), reMatch.capturedLength()};
Part prefix {it->pos + previousEnd, reMatch.capturedStart() - previousEnd};
Part url = Part {it->pos + reMatch.capturedStart(), reMatch.capturedLength()};
url.hyperlink = true;
segments.emplace_back(std::move(prefix));
if (prefix.n > 0) {
segments.emplace_back(std::move(prefix));
}
segments.emplace_back(std::move(url));
previousEnd = static_cast<int>(reMatch.capturedEnd());
}
if (previousEnd < it->n) {
Part suffix = Part {previousEnd, it->n - previousEnd};
Part suffix = Part {it->pos + previousEnd, it->n - previousEnd};
segments.emplace_back(std::move(suffix));
}
it = m_parts.erase(it);
Expand Down
17 changes: 17 additions & 0 deletions modules/Lith/Core/test/ProtocolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "protocol.h"

#include <QTextDocument>

class ProtocolTest : public QObject {
Q_OBJECT
private slots:
Expand Down Expand Up @@ -99,6 +101,21 @@ private slots:
QVERIFY(ok);
QCOMPARE(result, output);
}
void string_broken_url_result() {
// Taken from a bug introduced in this version, link rendering was broken in HTML versions of messages while plaintext remained ok
FormattedString str(QStringLiteral("https://github.com/LithApp/Lith/releases/tag/v1.7.15"));
str.prune();

auto htmlNotClipped = str.lastPart().toHtml(str.toPlain(), *lightTheme, 100);
QTextDocument docNotClipped;
docNotClipped.setHtml(htmlNotClipped);
QCOMPARE(docNotClipped.toPlainText(), QStringLiteral("https://github.com/LithApp/Lith/releases/tag/v1.7.15"));

auto htmlClipped = str.lastPart().toHtml(str.toPlain(), *lightTheme, 40);
QTextDocument docClipped;
docClipped.setHtml(htmlClipped);
QCOMPARE(docClipped.toPlainText(), QStringLiteral("https://github.com/…releases/tag/v1.7.15"));
}
private slots:
void buffer1_data() {
QTest::addColumn<QByteArray>("input");
Expand Down

0 comments on commit 425c668

Please sign in to comment.