From a046e3ed27afc8a29c37bf03837080dcb342dedd Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 13 Dec 2024 14:36:56 -0500 Subject: [PATCH] Add better support for colored text (and shrugs) from other clients Some clients - such as Element - can send colored text through , which fails to display in Qt's rich text parser. So we need to transform that into CSS styles which is supported by Qt. Notably this allows you to exchange rainbow shrugs through Matrix, which is really important. And this means colored backgrounds for text is supported too, I guess. (cherry picked from commit 843da2664fcc2082c297756ad0d897f377cd6b11) --- autotests/texthandlertest.cpp | 20 ++++++++++++++++++++ src/texthandler.cpp | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index 523b72f55..6e30d53bc 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -63,6 +63,7 @@ private Q_SLOTS: void receiveRichEdited(); void receiveLineSeparator(); void receiveRichCodeUrl(); + void receiveRichColor(); void componentOutput_data(); void componentOutput(); @@ -520,6 +521,25 @@ void TextHandlerTest::receiveRichCodeUrl() QCOMPARE(testTextHandler.handleRecieveRichText(), input); } +void TextHandlerTest::receiveRichColor() +{ + const QString testInputString = QStringLiteral( + "¯\\_()_/¯"); + const QString testOutputString = QStringLiteral( + "¯\\_()_/¯"); + + TextHandler testTextHandler; + testTextHandler.setData(testInputString); + + qInfo() << testTextHandler.handleRecieveRichText(); + + QCOMPARE(testTextHandler.handleRecieveRichText(), testOutputString); +} + void TextHandlerTest::componentOutput_data() { QTest::addColumn("testInputString"); diff --git a/src/texthandler.cpp b/src/texthandler.cpp index 42ac78344..d79d83415 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -496,6 +496,7 @@ QString TextHandler::cleanAttributes(const QString &tag, const QString &tagStrin nextAttribute = tagString.mid(nextAttributeIndex, nextSpaceIndex - nextAttributeIndex); if (isAllowedAttribute(tag, getAttributeType(nextAttribute))) { + QString style; if (tag == QStringLiteral("img") && getAttributeType(nextAttribute) == QStringLiteral("src")) { QString attributeData = TextRegex::attributeData.match(getAttributeData(nextAttribute)).captured(1); if (isAllowedLink(attributeData, true)) { @@ -516,9 +517,19 @@ QString TextHandler::cleanAttributes(const QString &tag, const QString &tagStrin if (attributeData == customEmojiStyle) { outputString.append(u' ' + nextAttribute); } + } else if (getAttributeType(nextAttribute) == QStringLiteral("data-mx-color")) { + const QString attributeData = TextRegex::attributeData.match(getAttributeData(nextAttribute)).captured(1); + style.append(u"color: " + attributeData + u';'); + } else if (getAttributeType(nextAttribute) == QStringLiteral("data-mx-bg-color")) { + const QString attributeData = TextRegex::attributeData.match(getAttributeData(nextAttribute)).captured(1); + style.append(u"background-color: " + attributeData + u';'); } else { outputString.append(u' ' + nextAttribute); } + + if (!style.isEmpty()) { + outputString.append(u" style=\"" + style + u'"'); + } } nextAttributeIndex = nextSpaceIndex + 1; }