From ab4e1a86dcc4dc9046b0871394c8cac6a958a60b Mon Sep 17 00:00:00 2001 From: Claire Elford Date: Fri, 6 Sep 2024 16:28:02 +1000 Subject: [PATCH] Fix parsing self-closing tags with no space (such as
) If there was no space between the tag name and the slash of a self-closing tag, the code assumes that the tag name is "br/". This commit adds the slash as a character to close a tag on, so that "
" is treated as a self-closing "br". BUG: 487377 --- autotests/texthandlertest.cpp | 16 +++++++++++++++- src/texthandler.cpp | 4 ++-- src/utils.h | 3 ++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index f2b5b6c29..da06d3415 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -46,6 +46,7 @@ private Q_SLOTS: void sendCustomEmojiCode_data(); void sendCustomEmojiCode(); + void receiveSpacelessSelfClosingTag(); void receiveStripReply(); void receivePlainTextIn(); @@ -252,6 +253,19 @@ void TextHandlerTest::sendCustomEmojiCode() QCOMPARE(testTextHandler.handleSendText(), testOutputString); } +void TextHandlerTest::receiveSpacelessSelfClosingTag() +{ + const QString testInputString = QStringLiteral("Test...
...ing"); + const QString testRichOutputString = QStringLiteral("Test...
...ing"); + const QString testPlainOutputString = QStringLiteral("Test...\n...ing"); + + TextHandler testTextHandler; + testTextHandler.setData(testInputString); + + QCOMPARE(testTextHandler.handleRecieveRichText(), testRichOutputString); + QCOMPARE(testTextHandler.handleRecievePlainText(Qt::RichText), testPlainOutputString); +} + void TextHandlerTest::receiveStripReply() { const QString testInputString = QStringLiteral( @@ -536,7 +550,7 @@ void TextHandlerTest::componentOutput_data() "someField }\nCustomQml {\n someTextProperty: someField.text\n}\nSure you can, it's still local to the same file where you " "defined the id") << QList{ - MessageComponent{MessageComponentType::Text, QStringLiteral("Ah, you mean something like"), {}}, + MessageComponent{MessageComponentType::Text, QStringLiteral("Ah, you mean something like
"), {}}, MessageComponent{ MessageComponentType::Code, QStringLiteral( diff --git a/src/texthandler.cpp b/src/texthandler.cpp index c3e02b4b4..5b45966b8 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -459,7 +459,7 @@ QString TextHandler::cleanAttributes(const QString &tag, const QString &tagStrin nextAttributeIndex += 1; while (nextAttributeIndex < tagString.length()) { - nextSpaceIndex = tagString.indexOf(TextRegex::endTagType, nextAttributeIndex); + nextSpaceIndex = tagString.indexOf(TextRegex::endAttributeType, nextAttributeIndex); if (nextSpaceIndex == -1) { nextSpaceIndex = tagString.length(); } @@ -505,7 +505,7 @@ QVariantMap TextHandler::getAttributes(const QString &tag, const QString &tagStr nextAttributeIndex += 1; while (nextAttributeIndex < tagString.length()) { - nextSpaceIndex = tagString.indexOf(TextRegex::endTagType, nextAttributeIndex); + nextSpaceIndex = tagString.indexOf(TextRegex::endAttributeType, nextAttributeIndex); if (nextSpaceIndex == -1) { nextSpaceIndex = tagString.length(); } diff --git a/src/utils.h b/src/utils.h index a0d688c26..7e284e2a9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -57,7 +57,8 @@ inline QColor getUserColor(qreal hueF) namespace TextRegex { -static const QRegularExpression endTagType{QStringLiteral("(>| )")}; +static const QRegularExpression endTagType{QStringLiteral("[> /]")}; +static const QRegularExpression endAttributeType{QStringLiteral("[> ]")}; static const QRegularExpression attributeData{QStringLiteral("['\"](.*?)['\"]")}; static const QRegularExpression removeReply{QStringLiteral("> <.*?>.*?\\n\\n"), QRegularExpression::DotMatchesEverythingOption}; static const QRegularExpression removeRichReply{QStringLiteral(".*?"), QRegularExpression::DotMatchesEverythingOption};