From 23eef9e1bcbb7a9ada18d55a9198aa13903e571a Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 1 May 2019 12:11:19 +0200 Subject: Simplify linkifyMessage Parsing html as xml has inherent problems, most notable there are many matrix clients that don't escape ampersands in urls of mx-replies, etc. (See issue #18) This also removes the replacement of as it isn't strictly needed. Also the QRegExp is replaced with the Qt5 QRegularExpression for perfomance and because it supports lookahead and lookbehind. I'm pretty sure that the original code also replaced href="" with href=\"\", which was probably wrong, but I'm not to sure about that. Fixes #18 --- src/Utils.cpp | 60 ++--------------------------------------------------------- 1 file changed, 2 insertions(+), 58 deletions(-) (limited to 'src/Utils.cpp') diff --git a/src/Utils.cpp b/src/Utils.cpp index 0d3e9384..f8fdfaf9 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -291,65 +291,9 @@ utils::linkifyMessage(const QString &body) { // Convert to valid XML. auto doc = QString("%1").arg(body); + doc.replace(conf::strings::url_regex, conf::strings::url_html); - doc.replace("", ""); - doc.replace("", ""); - doc.replace("
", "

"); - - QXmlStreamReader xml{doc}; - - QString textString; - while (!xml.atEnd() && !xml.hasError()) { - auto t = xml.readNext(); - - switch (t) { - case QXmlStreamReader::Characters: { - auto text = xml.text().toString(); - text.replace(conf::strings::url_regex, conf::strings::url_html); - - textString += text; - break; - } - case QXmlStreamReader::StartDocument: - case QXmlStreamReader::EndDocument: - break; - case QXmlStreamReader::StartElement: { - if (xml.name() == "html") - break; - - textString += QString("<%1").arg(xml.name().toString()); - - const auto attrs = xml.attributes(); - for (const auto &e : attrs) - textString += QString(" %1=\"%2\"") - .arg(e.name().toString()) - .arg(e.value().toString()); - - textString += ">"; - - break; - } - case QXmlStreamReader::EndElement: { - if (xml.name() == "html") - break; - - textString += QString("").arg(xml.name().toString()); - break; - } - default: { - break; - } - } - } - - if (xml.hasError()) { - qWarning() << "error while parsing xml" << xml.errorString() << doc; - doc.replace("", ""); - doc.replace("", ""); - return doc; - } - - return textString; + return doc; } QString -- cgit 1.5.1