diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index aa3c46a0d..44c43e37b 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -161,14 +161,21 @@ void TextHandlerTest::initTestCase() void TextHandlerTest::allowedAttributes() { - const QString testInputString = QStringLiteral("
Test
"); - const QString testOutputString = QStringLiteral("Test
"); + const QString testInputString1 = QStringLiteral("Test
"); + const QString testOutputString1 = QStringLiteral("Test
"); + // Handle urls where the href has either single (') or double (") quotes. + const QString testInputString2 = QStringLiteral(""); + const QString testOutputString2 = QStringLiteral(""); TextHandler testTextHandler; - testTextHandler.setData(testInputString); + testTextHandler.setData(testInputString1); - QCOMPARE(testTextHandler.handleSendText(), testOutputString); - QCOMPARE(testTextHandler.handleRecieveRichText(), testOutputString); + QCOMPARE(testTextHandler.handleSendText(), testOutputString1); + QCOMPARE(testTextHandler.handleRecieveRichText(), testOutputString1); + + testTextHandler.setData(testInputString2); + QCOMPARE(testTextHandler.handleSendText(), testOutputString2); + QCOMPARE(testTextHandler.handleRecieveRichText(), testOutputString2); } void TextHandlerTest::stripDisallowedTags() diff --git a/src/texthandler.cpp b/src/texthandler.cpp index 3083c19ff..f6e6ff8a2 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -307,12 +307,13 @@ QString TextHandler::cleanAttributes(const QString &tag, const QString &tagStrin if (isAllowedAttribute(tag, getAttributeType(nextAttribute))) { if (tag == QStringLiteral("img") && getAttributeType(nextAttribute) == QStringLiteral("src")) { - QString attributeData = getAttributeData(nextAttribute).remove(u'"'); + QString attributeData = TextRegex::attributeData.match(getAttributeData(nextAttribute)).captured(1); if (isAllowedLink(attributeData, true)) { outputString.append(u' ' + nextAttribute); } } else if (tag == u'a' && getAttributeType(nextAttribute) == QStringLiteral("href")) { - if (isAllowedLink(getAttributeData(nextAttribute).remove(u'"'))) { + QString attributeData = TextRegex::attributeData.match(getAttributeData(nextAttribute)).captured(1); + if (isAllowedLink(attributeData)) { outputString.append(u' ' + nextAttribute); } } else if (tag == QStringLiteral("code") && getAttributeType(nextAttribute) == QStringLiteral("class")) { diff --git a/src/texthandler.h b/src/texthandler.h index 7f260fbde..dc0440fa0 100644 --- a/src/texthandler.h +++ b/src/texthandler.h @@ -13,6 +13,7 @@ namespace TextRegex { static const QRegularExpression endTagType{QStringLiteral("(>| )")}; +static const QRegularExpression attributeData{QStringLiteral("['\"](.*?)['\"]")}; static const QRegularExpression removeReply{QStringLiteral("> <.*?>.*?\\n\\n"), QRegularExpression::DotMatchesEverythingOption}; static const QRegularExpression removeRichReply{QStringLiteral("]*>(.*?)"), QRegularExpression::DotMatchesEverythingOption};