From 5529a356368b31998e859e3fb5f94e85c16447aa Mon Sep 17 00:00:00 2001 From: Denis Dzyubenko Date: Fri, 18 Dec 2009 11:49:51 +0100 Subject: Temporary hackiesh solution to prevent BOM in the xml data. We don't want to have byte-order-mark in the middle of the written string. Task-number: QTBUG-6893 Reviewed-by: Thiago --- src/corelib/codecs/qtextcodec.h | 3 +++ src/corelib/xml/qxmlstream.cpp | 4 ++-- tests/auto/qxmlstream/tst_qxmlstream.cpp | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/corelib/codecs/qtextcodec.h b/src/corelib/codecs/qtextcodec.h index 75aafdd..d61cd6b 100644 --- a/src/corelib/codecs/qtextcodec.h +++ b/src/corelib/codecs/qtextcodec.h @@ -167,6 +167,9 @@ public: private: const QTextCodec *c; QTextCodec::ConverterState state; + + friend class QXmlStreamWriter; + friend class QXmlStreamWriterPrivate; }; class Q_CORE_EXPORT QTextDecoder { diff --git a/src/corelib/xml/qxmlstream.cpp b/src/corelib/xml/qxmlstream.cpp index 9f8867f..f987a31 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -3006,7 +3006,7 @@ QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) #ifndef QT_NO_TEXTCODEC codec = QTextCodec::codecForMib(106); // utf8 encoder = codec->makeEncoder(); - encoder->fromUnicode(QLatin1String("")); // no byte order mark for utf8 + encoder->state.flags |= QTextCodec::IgnoreHeader; // no byte order mark for utf8 #endif inStartElement = inEmptyElement = false; wroteSomething = false; @@ -3282,7 +3282,7 @@ void QXmlStreamWriter::setCodec(QTextCodec *codec) delete d->encoder; d->encoder = codec->makeEncoder(); if (codec->mibEnum() == 106) - d->encoder->fromUnicode(QLatin1String("")); // no byte order mark for utf8 + d->encoder->state.flags |= QTextCodec::IgnoreHeader; // no byte order mark for utf8 } } diff --git a/tests/auto/qxmlstream/tst_qxmlstream.cpp b/tests/auto/qxmlstream/tst_qxmlstream.cpp index 62cb54d..6aa9955 100644 --- a/tests/auto/qxmlstream/tst_qxmlstream.cpp +++ b/tests/auto/qxmlstream/tst_qxmlstream.cpp @@ -559,6 +559,7 @@ private slots: void hasAttributeSignature() const; void hasAttribute() const; void writeWithCodec() const; + void writeWithUtf8Codec() const; void writeWithStandalone() const; void entitiesAndWhitespace_1() const; void entitiesAndWhitespace_2() const; @@ -1303,7 +1304,6 @@ void tst_QXmlStream::hasAttribute() const void tst_QXmlStream::writeWithCodec() const { - QByteArray outarray; QXmlStreamWriter writer(&outarray); writer.setAutoFormatting(true); @@ -1326,6 +1326,20 @@ void tst_QXmlStream::writeWithCodec() const QVERIFY(outarray.contains(codec->name())); } +void tst_QXmlStream::writeWithUtf8Codec() const +{ + QByteArray outarray; + QXmlStreamWriter writer(&outarray); + + QTextCodec *codec = QTextCodec::codecForMib(106); // utf-8 + QVERIFY(codec); + writer.setCodec(codec); + + writer.writeStartDocument("1.0"); + static const char begin[] = ""; + QVERIFY(outarray.startsWith(begin)); +} + void tst_QXmlStream::writeWithStandalone() const { { -- cgit v0.12