From a0ae8b28b2ee31014cff694ecbfd8e58a746b2e5 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 26 Mar 2023 20:27:23 +0000 Subject: [PATCH] Handle stripnewlines for plain text list Handle stripping new lines when the plain text input is a markdown list. --- autotests/texthandlertest.cpp | 7 ++++++- src/neochatroom.cpp | 2 +- src/texthandler.cpp | 15 +++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index d56e4c379..2d2eed630 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -347,6 +347,9 @@ void TextHandlerTest::receiveStripNewlines() const QString testInputStringRich = QStringLiteral("Test
many
new
lines."); const QString testOutputString = QStringLiteral("Test many new lines."); + const QString testInputStringPlain2 = QStringLiteral("* List\n* Items"); + const QString testOutputString2 = QStringLiteral("List Items"); + TextHandler testTextHandler; testTextHandler.setData(testInputStringPlain); @@ -354,9 +357,11 @@ void TextHandlerTest::receiveStripNewlines() QCOMPARE(testTextHandler.handleRecieveRichText(Qt::PlainText, nullptr, nullptr, true), testOutputString); testTextHandler.setData(testInputStringRich); - QCOMPARE(testTextHandler.handleRecievePlainText(Qt::RichText, true), testOutputString); QCOMPARE(testTextHandler.handleRecieveRichText(Qt::RichText, nullptr, nullptr, true), testOutputString); + + testTextHandler.setData(testInputStringPlain2); + QCOMPARE(testTextHandler.handleRecievePlainText(Qt::RichText, true), testOutputString2); } /** diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index c9a655b43..bda530045 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -468,7 +468,7 @@ QString NeoChatRoom::eventToString(const RoomEvent &evt, Qt::TextFormat format, fileCaption = e.plainBody() + " | " + fileCaption; } textHandler.setData(fileCaption); - return !fileCaption.isEmpty() ? textHandler.handleRecievePlainText() : i18n("a file"); + return !fileCaption.isEmpty() ? textHandler.handleRecievePlainText(Qt::PlainText, stripNewlines) : i18n("a file"); } QString body; diff --git a/src/texthandler.cpp b/src/texthandler.cpp index 102116f72..25a9f4eed 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -151,12 +151,6 @@ QString TextHandler::handleRecievePlainText(Qt::TextFormat inputFormat, const bo // Strip mx-reply if present. m_dataBuffer.remove(TextRegex::removeRichReply); - if (stripNewlines) { - m_dataBuffer.replace(QStringLiteral("
"), QStringLiteral(" ")); - m_dataBuffer.replace(QStringLiteral("
"), QStringLiteral(" ")); - m_dataBuffer.replace(u'\n', QStringLiteral(" ")); - } - // Escaping then unescaping allows < and > to be maintained in a plain text string // otherwise markdownToHTML will strip what it thinks is a bad html tag entirely. if (inputFormat == Qt::PlainText) { @@ -169,6 +163,14 @@ QString TextHandler::handleRecievePlainText(Qt::TextFormat inputFormat, const bo */ m_dataBuffer = markdownToHTML(m_dataBuffer); + if (stripNewlines) { + m_dataBuffer.replace(QStringLiteral("
\n"), QStringLiteral(" ")); + m_dataBuffer.replace(QStringLiteral("
"), QStringLiteral(" ")); + m_dataBuffer.replace(QStringLiteral("
\n"), QStringLiteral(" ")); + m_dataBuffer.replace(QStringLiteral("
"), QStringLiteral(" ")); + m_dataBuffer.replace(u'\n', QStringLiteral(" ")); + } + // Strip all tags/attributes except code blocks which will be escaped. QString outputString; nextTokenType(); @@ -193,6 +195,7 @@ QString TextHandler::handleRecievePlainText(Qt::TextFormat inputFormat, const bo outputString = unescapeHtml(outputString); } + outputString = outputString.trimmed(); return outputString; }