diff options
-rw-r--r-- | dist/changes-4.6.0 | 3 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 27 | ||||
-rw-r--r-- | tests/auto/qtextdocument/tst_qtextdocument.cpp | 26 |
3 files changed, 32 insertions, 24 deletions
diff --git a/dist/changes-4.6.0 b/dist/changes-4.6.0 index 7f723da..ba2b051 100644 --- a/dist/changes-4.6.0 +++ b/dist/changes-4.6.0 @@ -35,6 +35,9 @@ information about a particular change. * Added QVariant::toFloat() and QVariant::toReal() * Added QVariant(float) constructor + - Qt::escape + * now escape the double quote (") + **************************************************************************** * Platform Specific Changes * **************************************************************************** diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 17981e3..6978b6c 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -140,7 +140,7 @@ bool Qt::mightBeRichText(const QString& text) /*! Converts the plain text string \a plain to a HTML string with - HTML metacharacters \c{<}, \c{>}, and \c{&} replaced by HTML + HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML entities. Example: @@ -162,23 +162,6 @@ QString Qt::escape(const QString& plain) rich += QLatin1String(">"); else if (plain.at(i) == QLatin1Char('&')) rich += QLatin1String("&"); - else - rich += plain.at(i); - } - return rich; -} - -static QString escapeXml(const QString &plain) -{ - QString rich; - rich.reserve(int(plain.length() * 1.1)); - for (int i = 0; i < plain.length(); ++i) { - if (plain.at(i) == QLatin1Char('<')) - rich += QLatin1String("<"); - else if (plain.at(i) == QLatin1Char('>')) - rich += QLatin1String(">"); - else if (plain.at(i) == QLatin1Char('&')) - rich += QLatin1String("&"); else if (plain.at(i) == QLatin1Char('"')) rich += QLatin1String("""); else @@ -2057,7 +2040,7 @@ void QTextHtmlExporter::emitAttribute(const char *attribute, const QString &valu html += QLatin1Char(' '); html += QLatin1String(attribute); html += QLatin1String("=\""); - html += escapeXml(value); + html += Qt::escape(value); html += QLatin1Char('"'); } @@ -2326,7 +2309,7 @@ void QTextHtmlExporter::emitFontFamily(const QString &family) quote = QLatin1String("""); html += quote; - html += escapeXml(family); + html += Qt::escape(family); html += quote; html += QLatin1Char(';'); } @@ -2360,13 +2343,13 @@ void QTextHtmlExporter::emitFragment(const QTextFragment &fragment) const QString name = format.anchorName(); if (!name.isEmpty()) { html += QLatin1String("<a name=\""); - html += escapeXml(name); + html += Qt::escape(name); html += QLatin1String("\"></a>"); } const QString href = format.anchorHref(); if (!href.isEmpty()) { html += QLatin1String("<a href=\""); - html += escapeXml(href); + html += Qt::escape(href); html += QLatin1String("\">"); closeAnchor = true; } diff --git a/tests/auto/qtextdocument/tst_qtextdocument.cpp b/tests/auto/qtextdocument/tst_qtextdocument.cpp index 452b683..5237438 100644 --- a/tests/auto/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/qtextdocument/tst_qtextdocument.cpp @@ -176,6 +176,8 @@ private slots: void testUndoBlocks(); void receiveCursorPositionChangedAfterContentsChange(); + void escape_data(); + void escape(); private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); @@ -577,7 +579,7 @@ void tst_QTextDocument::task240325() } void tst_QTextDocument::stylesheetFont_data() -{ +{ QTest::addColumn<QString>("stylesheet"); QTest::addColumn<QFont>("font"); @@ -1566,7 +1568,7 @@ void tst_QTextDocument::toHtml() QString output = doc->toHtml(); QCOMPARE(output, expectedOutput); - + QDomDocument document; QVERIFY2(document.setContent(output), "Output was not valid XML"); } @@ -2680,5 +2682,25 @@ void tst_QTextDocument::receiveCursorPositionChangedAfterContentsChange() 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); +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" |