Add better support for colored text (and shrugs) from other clients

Some clients - such as Element - can send colored text through <span>,
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.
This commit is contained in:
Joshua Goins
2024-12-13 14:36:56 -05:00
parent 23eaa6a4c7
commit 843da2664f
2 changed files with 31 additions and 0 deletions

View File

@@ -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;
}