summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@trolltech.com>2009-10-27 20:37:27 +0100
committerOlivier Goffart <ogoffart@trolltech.com>2009-10-28 10:24:50 +0100
commit1f3218e086778a0e324542a00937cd44e8e10c79 (patch)
treed1d3813e330f8e0144ee003a7e6b68079ff6ea29
parentde7de35082ec21d506a90e68cb30888540ec64cc (diff)
Qt::escape(): also escape the quote (&quot;)
This amend previous commit by removing the duplicate function. The quote need to be escaped in attributes. Reviewed-by: Thomas Zander
-rw-r--r--dist/changes-4.6.03
-rw-r--r--src/gui/text/qtextdocument.cpp27
-rw-r--r--tests/auto/qtextdocument/tst_qtextdocument.cpp26
3 files changed, 32 insertions, 24 deletions
diff --git a/dist/changes-4.6.0 b/dist/changes-4.6.0
index 7f723dae67..ba2b0512a0 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 (&quot;)
+
****************************************************************************
* Platform Specific Changes *
****************************************************************************
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index 17981e35d7..6978b6c064 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("&gt;");
else if (plain.at(i) == QLatin1Char('&'))
rich += QLatin1String("&amp;");
- 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("&lt;");
- else if (plain.at(i) == QLatin1Char('>'))
- rich += QLatin1String("&gt;");
- else if (plain.at(i) == QLatin1Char('&'))
- rich += QLatin1String("&amp;");
else if (plain.at(i) == QLatin1Char('"'))
rich += QLatin1String("&quot;");
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("&quot;");
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 452b6835ff..5237438686 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 &lt;QtCore&gt;";
+ QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&amp;bar=foo\">plop --&gt; </a></p>"
+ << "&lt;p class=&quot;cool&quot;&gt;&lt;a href=&quot;http://example.com/?foo=bar&amp;amp;bar=foo&quot;&gt;plop --&amp;gt; &lt;/a&gt;&lt;/p&gt;";
+ QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("&lt;\320\222\321\201&gt;");
+}
+
+void tst_QTextDocument::escape()
+{
+ QFETCH(QString, original);
+ QFETCH(QString, expected);
+
+ QCOMPARE(Qt::escape(original), expected);
+}
+
QTEST_MAIN(tst_QTextDocument)
#include "tst_qtextdocument.moc"