diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-08-03 10:26:58 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-08-04 22:17:05 +0200 |
commit | 26a73e1b3127f5e41a73b90121110d3533f4ad60 (patch) | |
tree | efb60a8ad56550d0ac0b94c5d10d22855fddefbd /src/xml/dom | |
parent | 7ceba9c4723846bc77d94c7bfc62c1a8ff1d9138 (diff) |
QDomDocument: Add a way to enable spacing-only text nodes
Added a parse option that can be passed to setContent(), to specify that
spacing-only text nodes must be preserved.
[ChangeLog][QtXml][QDomDocument] Spacing-only text nodes can now
be preserved by passing the ParseOption::PreserveSpacingOnlyNodes option
to setContent().
Fixes: QTBUG-104130
Fixes: QTBUG-89690
Task-number: QTBUG-90003
Change-Id: Id43730ce5b79a856c4b434d1f1d4dd7c49c25f31
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/xml/dom')
-rw-r--r-- | src/xml/dom/qdom.cpp | 6 | ||||
-rw-r--r-- | src/xml/dom/qdom.h | 1 | ||||
-rw-r--r-- | src/xml/dom/qdomhelpers.cpp | 15 | ||||
-rw-r--r-- | src/xml/dom/qdomhelpers_p.h | 3 |
4 files changed, 17 insertions, 8 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 74ea7d1e4e..b2fcac7f1b 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -6221,6 +6221,8 @@ bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing \value Default No parse options are set. \value UseNamespaceProcessing Namespace processing is enabled. + \value PreserveSpacingOnlyNodes Text nodes containing only spacing + characters are preserved. \sa setContent() */ @@ -6307,7 +6309,9 @@ bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing string if the element or attribute has no prefix. Text nodes consisting only of whitespace are stripped and won't - appear in the QDomDocument. + appear in the QDomDocument. Since Qt 6.5, one can pass + QDomDocument::ParseOption::PreserveSpacingOnlyNodes as a parse + option, to specify that spacing-only text nodes must be preserved. \include qdom.cpp entity-refs diff --git a/src/xml/dom/qdom.h b/src/xml/dom/qdom.h index f4c0da1b16..390a319553 100644 --- a/src/xml/dom/qdom.h +++ b/src/xml/dom/qdom.h @@ -268,6 +268,7 @@ public: enum class ParseOption { Default = 0x00, UseNamespaceProcessing = 0x01, + PreserveSpacingOnlyNodes = 0x02, }; Q_DECLARE_FLAGS(ParseOptions, ParseOption) diff --git a/src/xml/dom/qdomhelpers.cpp b/src/xml/dom/qdomhelpers.cpp index 2a4d4c5b3a..62258fbdfd 100644 --- a/src/xml/dom/qdomhelpers.cpp +++ b/src/xml/dom/qdomhelpers.cpp @@ -349,13 +349,14 @@ bool QDomParser::parseBody() } break; case QXmlStreamReader::Characters: - if (!reader->isWhitespace()) { // Skip the content consisting of only whitespaces - if (reader->isCDATA() || !reader->text().trimmed().isEmpty()) { - if (!domBuilder.characters(reader->text().toString(), reader->isCDATA())) { - domBuilder.fatalError(QDomParser::tr( - "Error occurred while processing the element content")); - return false; - } + // Skip the content if it contains only spacing characters, + // unless it's CDATA or PreserveSpacingOnlyNodes was specified. + if (reader->isCDATA() || domBuilder.preserveSpacingOnlyNodes() + || !(reader->isWhitespace() || reader->text().trimmed().isEmpty())) { + if (!domBuilder.characters(reader->text().toString(), reader->isCDATA())) { + domBuilder.fatalError( + QDomParser::tr("Error occurred while processing the element content")); + return false; } } break; diff --git a/src/xml/dom/qdomhelpers_p.h b/src/xml/dom/qdomhelpers_p.h index 73177981b5..5a4b2207f9 100644 --- a/src/xml/dom/qdomhelpers_p.h +++ b/src/xml/dom/qdomhelpers_p.h @@ -56,6 +56,9 @@ public: void fatalError(const QString &message); QDomDocument::ParseResult result() const { return parseResult; } + bool preserveSpacingOnlyNodes() const + { return parseOptions & QDomDocument::ParseOption::PreserveSpacingOnlyNodes; } + private: QString dtdInternalSubset(const QString &dtd); |