diff options
author | Lars Knoll <lars.knoll@qt.io> | 2022-05-31 11:58:50 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-06-21 01:04:12 +0200 |
commit | 9c1f3b6d4d5a5fe59396062c6f68cc1201665c62 (patch) | |
tree | 0a59a4dd0293f5c323433ffd314b34057e3d5152 /src/corelib/text/qstringconverter.cpp | |
parent | d531c4b65dc47312256f9de2786a852f6ac4c1f5 (diff) |
Add QStringDecoder::decoderForHtml()
Now that QStringConverter can handle non UTF encodings through ICU,
add a way to get a decoder for arbitrary HTML code.
Opposed to QStringConverter::encodingForHtml(), this method will
try to create a valid string decoder also for non unicode codecs.
Pick-to: 6.4
Change-Id: I343584da1b114396c744f482d9b433c9cedcc511
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstringconverter.cpp')
-rw-r--r-- | src/corelib/text/qstringconverter.cpp | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index ba13194310..38a434f353 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -2049,20 +2049,8 @@ QStringConverter::encodingForData(QByteArrayView data, char16_t expectedFirstCha return std::nullopt; } -/*! - Tries to determine the encoding of the HTML in \a data by looking at leading byte - order marks or a charset specifier in the HTML meta tag. If the optional is empty, - the encoding specified is not supported by QStringConverter. If no encoding is - detected, the method returns Utf8. -*/ -std::optional<QStringConverter::Encoding> QStringConverter::encodingForHtml(QByteArrayView data) +static QByteArray parseHtmlMetaForEncoding(QByteArrayView data) { - // determine charset - auto encoding = encodingForData(data); - if (encoding) - // trust the initial BOM - return encoding; - static constexpr auto metaSearcher = qMakeStaticByteArrayMatcher("meta "); static constexpr auto charsetSearcher = qMakeStaticByteArrayMatcher("charset="); @@ -2089,15 +2077,63 @@ std::optional<QStringConverter::Encoding> QStringConverter::encodingForHtml(QByt if (name == "unicode") // QTBUG-41998, ICU will return UTF-16. name = QByteArrayLiteral("UTF-8"); if (!name.isEmpty()) - return encodingForName(name); + return name; } } } } + return QByteArray(); +} + +/*! + Tries to determine the encoding of the HTML in \a data by looking at leading byte + order marks or a charset specifier in the HTML meta tag. If the optional is empty, + the encoding specified is not supported by QStringConverter. If no encoding is + detected, the method returns Utf8. + + \sa QStringDecoder::decoderForHtml() +*/ +std::optional<QStringConverter::Encoding> QStringConverter::encodingForHtml(QByteArrayView data) +{ + // determine charset + std::optional<QStringConverter::Encoding> encoding = encodingForData(data); + if (encoding) + // trust the initial BOM + return encoding; + + QByteArray encodingTag = parseHtmlMetaForEncoding(data); + if (!encodingTag.isEmpty()) + return encodingForName(encodingTag); + return Utf8; } /*! + Tries to determine the encoding of the HTML in \a data by looking at leading byte + order marks or a charset specifier in the HTML meta tag and returns a QStringDecoder + matching the encoding. If the returned decoder is not valid, + the encoding specified is not supported by QStringConverter. If no encoding is + detected, the method returns a decoder for Utf8. + + \sa isValid() +*/ +QStringDecoder QStringDecoder::decoderForHtml(QByteArrayView data) +{ + // determine charset + std::optional<QStringConverter::Encoding> encoding = encodingForData(data); + if (encoding) + // trust the initial BOM + return QStringDecoder(encoding.value()); + + QByteArray encodingTag = parseHtmlMetaForEncoding(data); + if (!encodingTag.isEmpty()) + return QStringDecoder(encodingTag); + + return QStringDecoder(Utf8); +} + + +/*! Returns the canonical name for encoding \a e. */ const char *QStringConverter::nameForEncoding(QStringConverter::Encoding e) |