diff options
author | Bea Lam <bea.lam@nokia.com> | 2010-03-05 06:08:26 (GMT) |
---|---|---|
committer | Bea Lam <bea.lam@nokia.com> | 2010-03-05 06:08:26 (GMT) |
commit | 769a11719ca85e9cdac4c0f1d0fcffe6145d6972 (patch) | |
tree | 8cf89cdceff0e832ff2cdf9f7ee96ea230403125 | |
parent | 48fd47f64f3f73e82016161d82cdf67908a9c653 (diff) | |
download | Qt-769a11719ca85e9cdac4c0f1d0fcffe6145d6972.zip Qt-769a11719ca85e9cdac4c0f1d0fcffe6145d6972.tar.gz Qt-769a11719ca85e9cdac4c0f1d0fcffe6145d6972.tar.bz2 |
Don't assume documents are in UTF-8.
Task-number: QTBUG-7719
4 files changed, 69 insertions, 3 deletions
diff --git a/src/declarative/qml/qdeclarativexmlhttprequest.cpp b/src/declarative/qml/qdeclarativexmlhttprequest.cpp index 3ba53f0..87cab85 100644 --- a/src/declarative/qml/qdeclarativexmlhttprequest.cpp +++ b/src/declarative/qml/qdeclarativexmlhttprequest.cpp @@ -52,6 +52,7 @@ #include <QtScript/qscriptcontext.h> #include <QtScript/qscriptengine.h> #include <QtNetwork/qnetworkreply.h> +#include <QtCore/qtextcodec.h> #include <QtCore/qxmlstream.h> #include <QtCore/qstack.h> #include <QtCore/qdebug.h> @@ -312,7 +313,7 @@ public: // C++ API static QScriptValue prototype(QScriptEngine *); - static QScriptValue load(QScriptEngine *engine, const QString &data); + static QScriptValue load(QScriptEngine *engine, const QByteArray &data); }; QT_END_NAMESPACE @@ -619,7 +620,7 @@ QScriptValue Document::prototype(QScriptEngine *engine) return proto; } -QScriptValue Document::load(QScriptEngine *engine, const QString &data) +QScriptValue Document::load(QScriptEngine *engine, const QByteArray &data) { Q_ASSERT(engine); @@ -960,6 +961,7 @@ public: QScriptValue abort(QScriptValue *me); QString responseBody() const; + const QByteArray & rawResponseBody() const; private slots: void downloadProgress(qint64); void error(QNetworkReply::NetworkError); @@ -1279,9 +1281,20 @@ void QDeclarativeXMLHttpRequest::finished() QString QDeclarativeXMLHttpRequest::responseBody() const { + QXmlStreamReader reader(m_responseEntityBody); + reader.readNext(); + QTextCodec *codec = QTextCodec::codecForName(reader.documentEncoding().toString().toUtf8()); + if (codec) + return codec->toUnicode(m_responseEntityBody); + return QString::fromUtf8(m_responseEntityBody); } +const QByteArray &QDeclarativeXMLHttpRequest::rawResponseBody() const +{ + return m_responseEntityBody; +} + QScriptValue QDeclarativeXMLHttpRequest::dispatchCallback(QScriptValue *me) { QScriptValue v = me->property(QLatin1String("callback")); @@ -1538,7 +1551,7 @@ static QScriptValue qmlxmlhttprequest_responseXML(QScriptContext *context, QScri request->readyState() != QDeclarativeXMLHttpRequest::Done) return engine->nullValue(); else - return Document::load(engine, request->responseBody()); + return Document::load(engine, request->rawResponseBody()); } static QScriptValue qmlxmlhttprequest_onreadystatechange(QScriptContext *context, QScriptEngine *engine) diff --git a/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.qml b/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.qml new file mode 100644 index 0000000..63165ab --- /dev/null +++ b/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.qml @@ -0,0 +1,28 @@ +import Qt 4.6 + +QtObject { + property bool dataOK: false + + property string responseText + property string responseXmlRootNodeValue + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "utf16.xml"); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + + responseText = x.responseText + if (x.responseXML) + responseXmlRootNodeValue = x.responseXML.documentElement.childNodes[0].nodeValue + + dataOK = true; + } + } + x.send() + } +} + diff --git a/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.xml b/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.xml Binary files differnew file mode 100644 index 0000000..0fbb126 --- /dev/null +++ b/tests/auto/declarative/qdeclarativexmlhttprequest/data/utf16.xml diff --git a/tests/auto/declarative/qdeclarativexmlhttprequest/tst_qdeclarativexmlhttprequest.cpp b/tests/auto/declarative/qdeclarativexmlhttprequest/tst_qdeclarativexmlhttprequest.cpp index 01f07ab..13ed959 100644 --- a/tests/auto/declarative/qdeclarativexmlhttprequest/tst_qdeclarativexmlhttprequest.cpp +++ b/tests/auto/declarative/qdeclarativexmlhttprequest/tst_qdeclarativexmlhttprequest.cpp @@ -104,6 +104,7 @@ private slots: void responseXML_invalid(); void invalidMethodUsage(); void redirects(); + void nonUtf8(); // Attributes void document(); @@ -917,6 +918,30 @@ void tst_qdeclarativexmlhttprequest::responseText_data() QTest::newRow("Not Found") << TEST_FILE("status.404.reply") << TEST_FILE("testdocument.html") << ""; } +void tst_qdeclarativexmlhttprequest::nonUtf8() +{ + QDeclarativeComponent component(&engine, TEST_FILE("utf16.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QString uc; + uc.resize(3); + uc[0] = QChar(0x10e3); + uc[1] = QChar(' '); + uc[2] = QChar(0x03a3); + QString xml = "<?xml version=\"1.0\" encoding=\"UTF-16\" standalone='yes'?>\n<root>\n" + uc + "\n</root>\n"; + + TRY_WAIT(object->property("dataOK").toBool() == true); + + QString responseText = object->property("responseText").toString(); + QCOMPARE(responseText, xml); + + QString responseXmlText = object->property("responseXmlRootNodeValue").toString(); + QCOMPARE(responseXmlText, '\n' + uc + '\n'); + + delete object; +} + // Test that calling hte XMLHttpRequest methods on a non-XMLHttpRequest object // throws an exception void tst_qdeclarativexmlhttprequest::invalidMethodUsage() |