diff --git a/src/gui/compat/removed_api.cpp b/src/gui/compat/removed_api.cpp index 8d1c8a1920..a64580e9e1 100644 --- a/src/gui/compat/removed_api.cpp +++ b/src/gui/compat/removed_api.cpp @@ -19,6 +19,13 @@ QT_USE_NAMESPACE #if QT_GUI_REMOVED_SINCE(6, 7) +#include "qtextdocument.h" + +bool Qt::mightBeRichText(const QString& text) +{ + return Qt::mightBeRichText(qToStringViewIgnoringNull(text)); +} + // #include "qotherheader.h" // // implement removed functions from qotherheader.h // order sections alphabetically diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 06099ee3f7..e5b271ee3c 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -51,6 +51,8 @@ namespace { }; /*! + \fn bool Qt::mightBeRichText(QAnyStringView text) + Returns \c true if the string \a text is likely to be rich text; otherwise returns \c false. @@ -60,18 +62,21 @@ namespace { for common cases, there is no guarantee. This function is defined in the \c header file. -*/ -bool Qt::mightBeRichText(const QString& text) + + \note In Qt versions prior to 6.7, this function took QString only. + */ +template +static bool mightBeRichTextImpl(T text) { if (text.isEmpty()) return false; qsizetype start = 0; - while (start < text.size() && text.at(start).isSpace()) + while (start < text.size() && QChar(text.at(start)).isSpace()) ++start; // skip a leading as for example with xhtml - if (QStringView{text}.mid(start, 5).compare(" ++open; } @@ -100,13 +105,14 @@ bool Qt::mightBeRichText(const QString& text) if (close > -1) { QVarLengthArray tag; for (qsizetype i = open + 1; i < close; ++i) { - if (text[i].isDigit() || text[i].isLetter()) - tag.append(text[i].toLower().unicode()); - else if (!tag.isEmpty() && text[i].isSpace()) + const auto current = QChar(text[i]); + if (current.isDigit() || current.isLetter()) + tag.append(current.toLower().unicode()); + else if (!tag.isEmpty() && current.isSpace()) break; - else if (!tag.isEmpty() && text[i] == u'/' && i + 1 == close) + else if (!tag.isEmpty() && current == u'/' && i + 1 == close) break; - else if (!text[i].isSpace() && (!tag.isEmpty() || text[i] != u'!')) + else if (!current.isSpace() && (!tag.isEmpty() || current != u'!')) return false; // that's not a tag } #ifndef QT_NO_TEXTHTMLPARSER @@ -119,6 +125,16 @@ bool Qt::mightBeRichText(const QString& text) return false; } +static bool mightBeRichTextImpl(QUtf8StringView text) +{ + return mightBeRichTextImpl(QLatin1StringView(QByteArrayView(text))); +} + +bool Qt::mightBeRichText(QAnyStringView text) +{ + return text.visit([](auto text) { return mightBeRichTextImpl(text); }); +} + /*! Converts the plain text string \a plain to an HTML-formatted paragraph while preserving most of its look. diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index 01ed9789ed..9c7b57a156 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -35,7 +35,10 @@ class QTextCursor; namespace Qt { +#if QT_GUI_REMOVED_SINCE(6, 7) Q_GUI_EXPORT bool mightBeRichText(const QString&); +#endif + Q_GUI_EXPORT bool mightBeRichText(QAnyStringView); Q_GUI_EXPORT QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode = WhiteSpacePre); } diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index f6527fbff6..47efbdb476 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -743,6 +743,9 @@ void tst_QTextDocument::mightBeRichText_data() " PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n" ""; QVERIFY(Qt::mightBeRichText(QString::fromLatin1(qtDocuHeader))); + QVERIFY(Qt::mightBeRichText(QLatin1StringView(qtDocuHeader))); + QVERIFY(QUtf8StringView(qtDocuHeader).isValidUtf8()); + QVERIFY(Qt::mightBeRichText(QUtf8StringView(qtDocuHeader))); QTest::addColumn("input"); QTest::addColumn("result"); @@ -762,6 +765,10 @@ void tst_QTextDocument::mightBeRichText() QFETCH(QString, input); QFETCH(bool, result); QCOMPARE(result, Qt::mightBeRichText(input)); + QCOMPARE(result, Qt::mightBeRichText(QStringView(input))); + QCOMPARE(result, Qt::mightBeRichText(QUtf8StringView(input.toUtf8()))); + QVERIFY(QtPrivate::isLatin1(input)); + QCOMPARE(result, Qt::mightBeRichText(QLatin1StringView(input.toLatin1()))); } Q_DECLARE_METATYPE(QTextDocumentFragment)