Skip to content

Commit 425c668

Browse files
committed
Fix buggy URL rendering with HTML strings
1 parent 8bbac1a commit 425c668

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

modules/Lith/Core/formattedstring.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ QString FormattedString::Part::toHtml(QStringView fullText, const ColorTheme& th
107107
}
108108

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

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

@@ -212,6 +211,7 @@ FormattedString::Part& FormattedString::addPart(QStringView s) {
212211
auto& newPart = addPart();
213212
newPart.pos = m_fullText.size();
214213
newPart.n = s.size();
214+
m_fullText += s;
215215
return newPart;
216216
}
217217

@@ -321,15 +321,17 @@ void FormattedString::prune() {
321321
int previousEnd = 0;
322322
while (reIt.hasNext()) {
323323
auto reMatch = reIt.next();
324-
Part prefix {previousEnd, reMatch.capturedStart() - previousEnd};
325-
Part url = Part {reMatch.capturedStart(), reMatch.capturedLength()};
324+
Part prefix {it->pos + previousEnd, reMatch.capturedStart() - previousEnd};
325+
Part url = Part {it->pos + reMatch.capturedStart(), reMatch.capturedLength()};
326326
url.hyperlink = true;
327-
segments.emplace_back(std::move(prefix));
327+
if (prefix.n > 0) {
328+
segments.emplace_back(std::move(prefix));
329+
}
328330
segments.emplace_back(std::move(url));
329331
previousEnd = static_cast<int>(reMatch.capturedEnd());
330332
}
331333
if (previousEnd < it->n) {
332-
Part suffix = Part {previousEnd, it->n - previousEnd};
334+
Part suffix = Part {it->pos + previousEnd, it->n - previousEnd};
333335
segments.emplace_back(std::move(suffix));
334336
}
335337
it = m_parts.erase(it);

modules/Lith/Core/test/ProtocolTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "protocol.h"
2020

21+
#include <QTextDocument>
22+
2123
class ProtocolTest : public QObject {
2224
Q_OBJECT
2325
private slots:
@@ -99,6 +101,21 @@ private slots:
99101
QVERIFY(ok);
100102
QCOMPARE(result, output);
101103
}
104+
void string_broken_url_result() {
105+
// Taken from a bug introduced in this version, link rendering was broken in HTML versions of messages while plaintext remained ok
106+
FormattedString str(QStringLiteral("https://github.com/LithApp/Lith/releases/tag/v1.7.15"));
107+
str.prune();
108+
109+
auto htmlNotClipped = str.lastPart().toHtml(str.toPlain(), *lightTheme, 100);
110+
QTextDocument docNotClipped;
111+
docNotClipped.setHtml(htmlNotClipped);
112+
QCOMPARE(docNotClipped.toPlainText(), QStringLiteral("https://github.com/LithApp/Lith/releases/tag/v1.7.15"));
113+
114+
auto htmlClipped = str.lastPart().toHtml(str.toPlain(), *lightTheme, 40);
115+
QTextDocument docClipped;
116+
docClipped.setHtml(htmlClipped);
117+
QCOMPARE(docClipped.toPlainText(), QStringLiteral("https://github.com/…releases/tag/v1.7.15"));
118+
}
102119
private slots:
103120
void buffer1_data() {
104121
QTest::addColumn<QByteArray>("input");

0 commit comments

Comments
 (0)