From 11f14c3b0dee239644097b3eddbbf464e4d69600 Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Tue, 18 Apr 2023 13:44:00 +0200 Subject: [PATCH] QDomDocument: no longer drop a provided 'standalone' attribute if 'no' - Definition of 'standalone' attribute: An XML declaration containing the attribute 'standalone' with its value set to 'yes', tells the parser to ignore markup declarations in the DTD and to use them only for validation. The declaration attribute is optional and defaults to 'no'. - Behavior Qt5 In qt5, DOM documents contained the standalone attribute, regardless of whether or not it was explicitly specified. - Behavior Qt6 In Qt6, the standalone attribute was only contained in a DOM document, if its value was 'yes'. If it was explicitly declared with the value being 'no', it was dropped in the DOM document. - Expected behavior If the source specified it overtly, then the generated XML should contain the attribute, even when it takes its default value. - Code base QXmlStreamReader provides a public bool getter isStandaloneDocument(). This says whether the document is standalone or not. The information whether the attribute was actually specified gets lost. In consequence, the attribute was always dropped on non-standalone documents. - Fix This patch makes hasStandalone a member of QXmlStreamReaderPrivate, to record whether the attribute has been explicitly specified. QDomParser is modified to retain the standalone attribute, if QXmlStreamReaderPrivate::hasStandalone is true. - Test The patch adds a test function in tst_QDom. Fixes: QTBUG-111200 Pick-to: 6.5 6.2 Change-Id: I06a0f230a2d69597dd6453f8fd3b036943d08735 Reviewed-by: Volker Hilsheimer Reviewed-by: Giuseppe D'Angelo --- src/corelib/serialization/qxmlstream.cpp | 2 +- src/corelib/serialization/qxmlstream_p.h | 3 +++ src/xml/dom/qdomhelpers.cpp | 8 +++++--- tests/auto/xml/dom/qdom/tst_qdom.cpp | 26 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp index c6d085714fb..eebccc6cc5d 100644 --- a/src/corelib/serialization/qxmlstream.cpp +++ b/src/corelib/serialization/qxmlstream.cpp @@ -827,6 +827,7 @@ void QXmlStreamReaderPrivate::init() isWhitespace = true; isCDATA = false; standalone = false; + hasStandalone = false; tos = 0; resumeReduction = 0; state_stack[tos++] = 0; @@ -1777,7 +1778,6 @@ void QXmlStreamReaderPrivate::startDocument() * proper order: * * [23] XMLDecl ::= '' */ - bool hasStandalone = false; for (qsizetype i = 0; err.isNull() && i < n; ++i) { Attribute &attrib = attributeStack[i]; diff --git a/src/corelib/serialization/qxmlstream_p.h b/src/corelib/serialization/qxmlstream_p.h index b1fee62e04d..ffa49f8c642 100644 --- a/src/corelib/serialization/qxmlstream_p.h +++ b/src/corelib/serialization/qxmlstream_p.h @@ -383,6 +383,7 @@ public: uint hasExternalDtdSubset : 1; uint lockEncoding : 1; uint namespaceProcessing : 1; + uint hasStandalone : 1; // TODO: expose in public API int resumeReduction; void resume(int rule); @@ -509,6 +510,8 @@ public: QXmlStreamEntityResolver *entityResolver; + static QXmlStreamReaderPrivate *get(QXmlStreamReader *q) { return q->d_func(); } + private: /*! \internal Never assign to variable type directly. Instead use this function. diff --git a/src/xml/dom/qdomhelpers.cpp b/src/xml/dom/qdomhelpers.cpp index 62258fbdfd3..48869907f8f 100644 --- a/src/xml/dom/qdomhelpers.cpp +++ b/src/xml/dom/qdomhelpers.cpp @@ -8,6 +8,7 @@ #include "qdomhelpers_p.h" #include "qdom_p.h" #include "qxmlstream.h" +#include "private/qxmlstream_p.h" #include #include @@ -264,9 +265,10 @@ bool QDomParser::parseProlog() if (reader->isStandaloneDocument()) { value += u" standalone='yes'"_s; } else { - // TODO: Add standalone='no', if 'standalone' is specified. With the current - // QXmlStreamReader there is no way to figure out if it was specified or not. - // QXmlStreamReader needs to be modified for handling that case correctly. + // Add the standalone attribute only if it was specified + QXmlStreamReaderPrivate *priv = QXmlStreamReaderPrivate::get(reader); + if (priv->hasStandalone) + value += u" standalone='no'"_s; } if (!domBuilder.processingInstruction(u"xml"_s, value)) { diff --git a/tests/auto/xml/dom/qdom/tst_qdom.cpp b/tests/auto/xml/dom/qdom/tst_qdom.cpp index cfc869949f4..4cf48a9f4b0 100644 --- a/tests/auto/xml/dom/qdom/tst_qdom.cpp +++ b/tests/auto/xml/dom/qdom/tst_qdom.cpp @@ -105,6 +105,7 @@ private slots: void DTDInternalSubset() const; void DTDInternalSubset_data() const; void QTBUG49113_dontCrashWithNegativeIndex() const; + void standalone(); void cleanupTestCase() const; @@ -2225,6 +2226,31 @@ void tst_QDom::QTBUG49113_dontCrashWithNegativeIndex() const QVERIFY(node.isNull()); } +void tst_QDom::standalone() +{ + { + QDomDocument doc; + const QString dtd("\n" + "\n"); + doc.setContent(dtd); + QVERIFY(doc.toString().contains("standalone=\'no\'")); + } + { + QDomDocument doc; + const QString dtd("\n" + "\n"); + doc.setContent(dtd); + QVERIFY(!doc.toString().contains("standalone")); + } + { + QDomDocument doc; + const QString dtd("\n" + "\n"); + doc.setContent(dtd); + QVERIFY(doc.toString().contains("standalone=\'yes\'")); + } +} + void tst_QDom::DTDInternalSubset() const { QFETCH( QString, doc );