Commit 92aa5f28 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Extract code for creating autotest

CCBUG: 418482
parent c865c3bc
......@@ -55,8 +55,6 @@
#include <grantlee/templateloader.h>
#include <grantlee/template.h>
#include <qtwebenginewidgetsversion.h>
using namespace MimeTreeParser;
using namespace MessageViewer;
......@@ -241,44 +239,6 @@ QString sigStatusToString(const QGpgME::Protocol *cryptProto, int status_code, G
return result;
}
// FIXME this used to go through the full webkit parser to extract the body and head blocks
// until we have that back, at least attempt to fix some of the damage
// yes, "parsing" HTML with regexps is very very wrong, but it's still better than not filtering
// this at all...
QString processHtml(const QString &htmlSource, QString &extraHead)
{
QString s = htmlSource.trimmed();
s = s.remove(QRegExp(QStringLiteral("^<!DOCTYPE[^>]*>"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("^<html[^>]*>"), Qt::CaseInsensitive)).trimmed();
// head
s = s.remove(QRegExp(QStringLiteral("^<head/>"), Qt::CaseInsensitive)).trimmed();
const int startIndex = s.indexOf(QLatin1String("<head>"), Qt::CaseInsensitive);
if (startIndex >= 0) {
const auto endIndex = s.indexOf(QLatin1String("</head>"), Qt::CaseInsensitive);
if (endIndex < 0) {
return htmlSource;
}
extraHead = s.mid(startIndex + 6, endIndex - startIndex - 6);
#if QTWEBENGINEWIDGETS_VERSION < QT_VERSION_CHECK(5, 13, 0)
//Remove this hack with https://codereview.qt-project.org/#/c/256100/2 is merged
//Don't authorize to refresh content.
if (MessageViewer::Util::excludeExtraHeader(s)) {
extraHead.clear();
}
#endif
s = s.mid(endIndex + 7).trimmed();
}
// body
s = s.remove(QRegExp(QStringLiteral("<body[^>]*>"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("</html>$"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("</body>$"), Qt::CaseInsensitive)).trimmed();
return s;
}
DefaultRendererPrivate::DefaultRendererPrivate(CSSHelperBase *cssHelper, const MessagePartRendererFactory *rendererFactory)
: mCSSHelper(cssHelper)
, mRendererFactory(rendererFactory)
......@@ -379,7 +339,7 @@ void DefaultRendererPrivate::render(const HtmlMessagePart::Ptr &mp, HtmlWriter *
{
QString extraHead;
//laurent: FIXME port to async method webengine
QString bodyText = processHtml(mp->bodyHtml(), extraHead);
QString bodyText = Util::processHtml(mp->bodyHtml(), extraHead);
if (isHtmlPreferred) {
mp->nodeHelper()->setNodeDisplayedEmbedded(mp->content(), true);
......
......@@ -74,6 +74,8 @@
#include <QDesktopServices>
#include <QRegularExpression>
#include <qtwebenginewidgetsversion.h>
using namespace MessageViewer;
/** Checks whether @p str contains external references. To be precise,
we only check whether @p str contains 'xxx="http[s]:' where xxx is
......@@ -678,3 +680,41 @@ void Util::readGravatarConfig()
Gravatar::GravatarCache::self()->clear();
}
}
// FIXME this used to go through the full webkit parser to extract the body and head blocks
// until we have that back, at least attempt to fix some of the damage
// yes, "parsing" HTML with regexps is very very wrong, but it's still better than not filtering
// this at all...
QString Util::processHtml(const QString &htmlSource, QString &extraHead)
{
QString s = htmlSource.trimmed();
s = s.remove(QRegExp(QStringLiteral("^<!DOCTYPE[^>]*>"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("^<html[^>]*>"), Qt::CaseInsensitive)).trimmed();
// head
s = s.remove(QRegExp(QStringLiteral("^<head/>"), Qt::CaseInsensitive)).trimmed();
const int startIndex = s.indexOf(QLatin1String("<head>"), Qt::CaseInsensitive);
if (startIndex >= 0) {
const auto endIndex = s.indexOf(QLatin1String("</head>"), Qt::CaseInsensitive);
if (endIndex < 0) {
return htmlSource;
}
extraHead = s.mid(startIndex + 6, endIndex - startIndex - 6);
#if QTWEBENGINEWIDGETS_VERSION < QT_VERSION_CHECK(5, 13, 0)
//Remove this hack with https://codereview.qt-project.org/#/c/256100/2 is merged
//Don't authorize to refresh content.
if (MessageViewer::Util::excludeExtraHeader(s)) {
extraHead.clear();
}
#endif
s = s.mid(endIndex + 7).trimmed();
}
// body
s = s.remove(QRegExp(QStringLiteral("<body[^>]*>"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("</html>$"), Qt::CaseInsensitive)).trimmed();
s = s.remove(QRegExp(QStringLiteral("</body>$"), Qt::CaseInsensitive)).trimmed();
return s;
}
......@@ -42,6 +42,7 @@
#include <KMime/Content>
#include <KService>
#include <AkonadiCore/Item>
class QUrl;
class QWidget;
class QActionGroup;
......@@ -70,6 +71,8 @@ Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT bool excludeExtraHeader(const QString &s)
/** Return a QTextCodec for the specified charset.
* This function is a bit more tolerant, than QTextCodec::codecForName */
MESSAGEVIEWER_EXPORT const QTextCodec *codecForName(const QByteArray &_str);
Q_REQUIRED_RESULT MESSAGEVIEWER_EXPORT QString processHtml(const QString &htmlSource, QString &extraHead);
}
}
Q_DECLARE_METATYPE(KService::Ptr)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment