From 103ef2bf584480979da34d150f5467fdfb85a68b Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 29 Apr 2020 17:58:14 +0200 Subject: [PATCH] Remove QTextCodec dependency from QTextBrowser Use QStringConverter instead to convert HTML to a QString. This limits the amount of supported encodings to UTF based encodings and Latin1. This is ok, as anything but utf8 is strongly discouraged by the HTML spec anyway, and the support we have with this change does cover ~98% of all real world HTML. Change-Id: Ia610d327624b083c23d3c604aee70517a4a5eb6a Reviewed-by: Thiago Macieira --- src/widgets/widgets/qtextbrowser.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index af61dda1de..c9c5c3f190 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -48,9 +48,6 @@ #include #include #include "private/qtextdocumentlayout_p.h" -#if QT_CONFIG(textcodec) -#include -#endif #include #include #if QT_CONFIG(whatsthis) @@ -314,16 +311,16 @@ void QTextBrowserPrivate::setSource(const QUrl &url, QTextDocument::ResourceType if (data.userType() == QMetaType::QString) { txt = data.toString(); } else if (data.userType() == QMetaType::QByteArray) { + QByteArray ba = data.toByteArray(); if (type == QTextDocument::HtmlResource) { -#if QT_CONFIG(textcodec) - QByteArray ba = data.toByteArray(); - QTextCodec *codec = Qt::codecForHtml(ba); - txt = codec->toUnicode(ba); -#else - txt = data.toString(); -#endif + auto encoding = QStringConverter::encodingForHtml(ba.constData(), ba.size()); + if (!encoding) + // fall back to utf8 + encoding = QStringDecoder::Utf8; + QStringDecoder toUtf16(*encoding); + txt = toUtf16(ba); } else { - txt = QString::fromUtf8(data.toByteArray()); + txt = QString::fromUtf8(ba); } } if (Q_UNLIKELY(txt.isEmpty()))