- QCoreApplication::translate() will no longer return the source text when
the translation is empty. Use lrelease -removeidentical for optimization.
+
+- Qt::escape() is deprecated (but can be enabled via
+ QT_DISABLE_DEPRECATED_BEFORE), use QString::toHtmlEscaped() instead.
****************************************************************************
* General *
//! [7]
QString plain = "#include <QtCore>"
-QString html = Qt::escape(plain);
+QString html = plain.toHtmlEscaped();
// html == "#include <QtCore>"
//! [7]
return v;
}
+
+/*!
+ \obsolete
+ \fn QString Qt::escape(const QString &plain)
+
+ \sa QString::toHtmlEscaped()
+*/
+
/*!
Converts the plain text string \a plain to a HTML string with
HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML
Example:
- \snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 0
-
- This function is defined in the \c <QString> header file.
-
- \sa convertFromPlainText(), mightBeRichText()
+ \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 7
*/
-QString Qt::escape(const QString &plain)
+QString QString::toHtmlEscaped() const
{
QString rich;
- rich.reserve(int(plain.length() * 1.1));
- for (int i = 0; i < plain.length(); ++i) {
- if (plain.at(i) == QLatin1Char('<'))
+ const int len = length();
+ rich.reserve(int(len * 1.1));
+ for (int i = 0; i < len; ++i) {
+ if (at(i) == QLatin1Char('<'))
rich += QLatin1String("<");
- else if (plain.at(i) == QLatin1Char('>'))
+ else if (at(i) == QLatin1Char('>'))
rich += QLatin1String(">");
- else if (plain.at(i) == QLatin1Char('&'))
+ else if (at(i) == QLatin1Char('&'))
rich += QLatin1String("&");
- else if (plain.at(i) == QLatin1Char('"'))
+ else if (at(i) == QLatin1Char('"'))
rich += QLatin1String(""");
else
- rich += plain.at(i);
+ rich += at(i);
}
rich.squeeze();
return rich;
QString trimmed() const Q_REQUIRED_RESULT;
QString simplified() const Q_REQUIRED_RESULT;
+ QString toHtmlEscaped() const Q_REQUIRED_RESULT;
QString &insert(int i, QChar c);
QString &insert(int i, const QChar *uc, int len);
{ return QBool(indexOf(s, 0, cs) != -1); }
namespace Qt {
- Q_CORE_EXPORT QString escape(const QString &plain);
+#if QT_DEPRECATED_SINCE(5, 0)
+QT_DEPRECATED inline QString escape(const QString &plain) {
+ return plain.toHtmlEscaped();
+}
+#endif
}
QT_END_NAMESPACE
html += QLatin1Char(' ');
html += QLatin1String(attribute);
html += QLatin1String("=\"");
- html += Qt::escape(value);
+ html += value.toHtmlEscaped();
html += QLatin1Char('"');
}
quote = QLatin1String(""");
html += quote;
- html += Qt::escape(family);
+ html += family.toHtmlEscaped();
html += quote;
html += QLatin1Char(';');
}
const QString name = format.anchorName();
if (!name.isEmpty()) {
html += QLatin1String("<a name=\"");
- html += Qt::escape(name);
+ html += name.toHtmlEscaped();
html += QLatin1String("\"></a>");
}
const QString href = format.anchorHref();
if (!href.isEmpty()) {
html += QLatin1String("<a href=\"");
- html += Qt::escape(href);
+ html += href.toHtmlEscaped();
html += QLatin1String("\">");
closeAnchor = true;
}
} else {
Q_ASSERT(!txt.contains(QChar::ObjectReplacementCharacter));
- txt = Qt::escape(txt);
+ txt = txt.toHtmlEscaped();
// split for [\n{LineSeparator}]
QString forcedLineBreakRegExp = QString::fromLatin1("[\\na]");
void literals();
void reserve();
+ void toHtmlEscaped_data();
+ void toHtmlEscaped();
};
typedef QList<int> IntList;
nil2.reserve(0);
}
+void tst_QString::toHtmlEscaped_data()
+{
+ QTest::addColumn<QString>("original");
+ QTest::addColumn<QString>("expected");
+
+ QTest::newRow("1") << "Hello World\n" << "Hello World\n";
+ QTest::newRow("2") << "#include <QtCore>" << "#include <QtCore>";
+ QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&bar=foo\">plop --> </a></p>"
+ << "<p class="cool"><a href="http://example.com/?foo=bar&amp;bar=foo">plop --&gt; </a></p>";
+ QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("<\320\222\321\201>");
+}
+
+void tst_QString::toHtmlEscaped()
+{
+ QFETCH(QString, original);
+ QFETCH(QString, expected);
+
+ QCOMPARE(original.toHtmlEscaped(), expected);
+}
+
QTEST_APPLESS_MAIN(tst_QString)
#include "tst_qstring.moc"
void testUndoBlocks();
void receiveCursorPositionChangedAfterContentsChange();
- void escape_data();
- void escape();
void copiedFontSize();
QCOMPARE(rec.first, QString("contentsChanged"));
}
-void tst_QTextDocument::escape_data()
-{
- QTest::addColumn<QString>("original");
- QTest::addColumn<QString>("expected");
-
- QTest::newRow("1") << "Hello World\n" << "Hello World\n";
- QTest::newRow("2") << "#include <QtCore>" << "#include <QtCore>";
- QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&bar=foo\">plop --> </a></p>"
- << "<p class="cool"><a href="http://example.com/?foo=bar&amp;bar=foo">plop --&gt; </a></p>";
- QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("<\320\222\321\201>");
-}
-
-void tst_QTextDocument::escape()
-{
- QFETCH(QString, original);
- QFETCH(QString, expected);
-
- QCOMPARE(Qt::escape(original), expected);
-}
-
void tst_QTextDocument::copiedFontSize()
{
QTextDocument documentInput;