From 4341cc437dc9ca98986bd43d15ae6575369a63b6 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 9 Apr 2023 14:20:22 +0000 Subject: [PATCH] Handle escaped html for plain text output Always unescape html for plain text output no matter what the input is. --- autotests/texthandlertest.cpp | 22 ++++++++++++++++------ src/texthandler.cpp | 4 +--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index 9e48b0358..469a7bcb8 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -6,7 +6,7 @@ #include "texthandler.h" -#include +#include #include #include @@ -49,7 +49,8 @@ private Q_SLOTS: void receiveStripReply(); void receivePlainTextIn(); - void recieveRichInPlainOut(); + void receiveRichInPlainOut_data(); + void receiveRichInPlainOut(); void receivePlainStripHtml(); void receivePlainStripMarkup(); void receiveStripNewlines(); @@ -349,15 +350,24 @@ void TextHandlerTest::receiveStripReply() QCOMPARE(testTextHandler.handleRecievePlainText(), testOutputString); } -void TextHandlerTest::recieveRichInPlainOut() +void TextHandlerTest::receiveRichInPlainOut_data() { - const QString testInputString = QStringLiteral("a & b"); - const QString testOutputString = QStringLiteral("a & b"); + QTest::addColumn("testInputString"); + QTest::addColumn("testOutputString"); + + QTest::newRow("ampersand") << QStringLiteral("a & b") << QStringLiteral("a & b"); + QTest::newRow("quote") << QStringLiteral(""a and b"") << QStringLiteral("\"a and b\""); +} + +void TextHandlerTest::receiveRichInPlainOut() +{ + QFETCH(QString, testInputString); + QFETCH(QString, testOutputString); TextHandler testTextHandler; testTextHandler.setData(testInputString); - QCOMPARE(testTextHandler.handleRecievePlainText(), testOutputString); + QCOMPARE(testTextHandler.handleRecievePlainText(Qt::RichText), testOutputString); } void TextHandlerTest::receivePlainTextIn() diff --git a/src/texthandler.cpp b/src/texthandler.cpp index fdcef9810..8417bddfc 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -235,9 +235,7 @@ QString TextHandler::handleRecievePlainText(Qt::TextFormat inputFormat, const bo // 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) { - outputString = unescapeHtml(outputString); - } + outputString = unescapeHtml(outputString); outputString = outputString.trimmed(); return outputString;