summaryrefslogtreecommitdiffstats
path: root/src/xml/dom/qdom.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/xml/dom/qdom.cpp')
-rw-r--r--src/xml/dom/qdom.cpp920
1 files changed, 550 insertions, 370 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp
index 8974421ed6..b25cdf487f 100644
--- a/src/xml/dom/qdom.cpp
+++ b/src/xml/dom/qdom.cpp
@@ -22,7 +22,8 @@
#include <qdebug.h>
#include <qxmlstream.h>
#include <private/qduplicatetracker_p.h>
-
+#include <private/qstringiterator_p.h>
+#include <qvarlengtharray.h>
#include <stdio.h>
#include <limits>
@@ -46,7 +47,7 @@ using namespace Qt::StringLiterals;
/* ##### new TODOs:
- Remove empty emthods in the *Private classes
+ Remove empty methods in the *Private classes
Make a lot of the (mostly empty) methods in the public classes inline.
Specially constructors assignment operators and comparison operators are candidates.
@@ -156,10 +157,11 @@ static QString fixedCharData(const QString &data, bool *ok)
}
QString result;
- for (int i = 0; i < data.size(); ++i) {
- QChar c = data.at(i);
+ QStringIterator it(data);
+ while (it.hasNext()) {
+ const char32_t c = it.next(QChar::Null);
if (QXmlUtils::isChar(c)) {
- result.append(c);
+ result.append(QChar::fromUcs4(c));
} else if (QDomImplementationPrivate::invalidDataPolicy == QDomImplementation::ReturnNullNode) {
*ok = false;
return QString();
@@ -372,52 +374,52 @@ QDomImplementation::QDomImplementation()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a implementation.
*/
-QDomImplementation::QDomImplementation(const QDomImplementation &x)
+QDomImplementation::QDomImplementation(const QDomImplementation &implementation)
+ : impl(implementation.impl)
{
- impl = x.impl;
if (impl)
impl->ref.ref();
}
-QDomImplementation::QDomImplementation(QDomImplementationPrivate *p)
+QDomImplementation::QDomImplementation(QDomImplementationPrivate *pimpl)
+ : impl(pimpl)
{
// We want to be co-owners, so increase the reference count
- impl = p;
if (impl)
impl->ref.ref();
}
/*!
- Assigns \a x to this DOM implementation.
+ Assigns \a other to this DOM implementation.
*/
-QDomImplementation& QDomImplementation::operator=(const QDomImplementation &x)
+QDomImplementation& QDomImplementation::operator=(const QDomImplementation &other)
{
- if (x.impl)
- x.impl->ref.ref();
+ if (other.impl)
+ other.impl->ref.ref();
if (impl && !impl->ref.deref())
delete impl;
- impl = x.impl;
+ impl = other.impl;
return *this;
}
/*!
- Returns \c true if \a x and this DOM implementation object were
+ Returns \c true if \a other and this DOM implementation object were
created from the same QDomDocument; otherwise returns \c false.
*/
-bool QDomImplementation::operator==(const QDomImplementation &x) const
+bool QDomImplementation::operator==(const QDomImplementation &other) const
{
- return (impl == x.impl);
+ return impl == other.impl;
}
/*!
- Returns \c true if \a x and this DOM implementation object were
+ Returns \c true if \a other and this DOM implementation object were
created from different QDomDocuments; otherwise returns \c false.
*/
-bool QDomImplementation::operator!=(const QDomImplementation &x) const
+bool QDomImplementation::operator!=(const QDomImplementation &other) const
{
- return (impl != x.impl);
+ return !operator==(other);
}
/*!
@@ -644,10 +646,10 @@ bool QDomNodeListPrivate::operator==(const QDomNodeListPrivate &other) const
bool QDomNodeListPrivate::operator!=(const QDomNodeListPrivate &other) const
{
- return (node_impl != other.node_impl) || (tagname != other.tagname);
+ return !operator==(other);
}
-void QDomNodeListPrivate::createList()
+void QDomNodeListPrivate::createList() const
{
if (!node_impl)
return;
@@ -701,16 +703,21 @@ void QDomNodeListPrivate::createList()
}
}
-QDomNodePrivate* QDomNodeListPrivate::item(int index)
+bool QDomNodeListPrivate::maybeCreateList() const
{
if (!node_impl)
- return nullptr;
+ return false;
const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
if (!doc || timestamp != doc->nodeListTime)
createList();
- if (index >= list.size())
+ return true;
+}
+
+QDomNodePrivate *QDomNodeListPrivate::item(int index)
+{
+ if (!maybeCreateList() || index >= list.size() || index < 0)
return nullptr;
return list.at(index);
@@ -718,16 +725,10 @@ QDomNodePrivate* QDomNodeListPrivate::item(int index)
int QDomNodeListPrivate::length() const
{
- if (!node_impl)
+ if (!maybeCreateList())
return 0;
- const QDomDocumentPrivate *const doc = node_impl->ownerDocument();
- if (!doc || timestamp != doc->nodeListTime) {
- QDomNodeListPrivate *that = const_cast<QDomNodeListPrivate *>(this);
- that->createList();
- }
-
- return list.count();
+ return list.size();
}
/**************************************************************
@@ -769,54 +770,54 @@ QDomNodeList::QDomNodeList()
{
}
-QDomNodeList::QDomNodeList(QDomNodeListPrivate* p)
- : impl(p)
+QDomNodeList::QDomNodeList(QDomNodeListPrivate *pimpl)
+ : impl(pimpl)
{
}
/*!
- Constructs a copy of \a n.
+ Constructs a copy of \a nodeList.
*/
-QDomNodeList::QDomNodeList(const QDomNodeList& n)
+QDomNodeList::QDomNodeList(const QDomNodeList &nodeList)
+ : impl(nodeList.impl)
{
- impl = n.impl;
if (impl)
impl->ref.ref();
}
/*!
- Assigns \a n to this node list.
+ Assigns \a other to this node list.
*/
-QDomNodeList& QDomNodeList::operator=(const QDomNodeList &n)
+QDomNodeList& QDomNodeList::operator=(const QDomNodeList &other)
{
- if (n.impl)
- n.impl->ref.ref();
+ if (other.impl)
+ other.impl->ref.ref();
if (impl && !impl->ref.deref())
delete impl;
- impl = n.impl;
+ impl = other.impl;
return *this;
}
/*!
- Returns \c true if the node list \a n and this node list are equal;
+ Returns \c true if the node list \a other and this node list are equal;
otherwise returns \c false.
*/
-bool QDomNodeList::operator==(const QDomNodeList &n) const
+bool QDomNodeList::operator==(const QDomNodeList &other) const
{
- if (impl == n.impl)
+ if (impl == other.impl)
return true;
- if (!impl || !n.impl)
+ if (!impl || !other.impl)
return false;
- return (*impl == *n.impl);
+ return (*impl == *other.impl);
}
/*!
- Returns \c true the node list \a n and this node list are not equal;
+ Returns \c true the node list \a other and this node list are not equal;
otherwise returns \c false.
*/
-bool QDomNodeList::operator!=(const QDomNodeList &n) const
+bool QDomNodeList::operator!=(const QDomNodeList &other) const
{
- return !operator==(n);
+ return !operator==(other);
}
/*!
@@ -1309,7 +1310,7 @@ QDomDocumentPrivate* QDomNodePrivate::ownerDocument()
QDomNodePrivate* p = this;
while (p && !p->isDocument()) {
if (!p->hasParent)
- return (QDomDocumentPrivate*)p->ownerNode;
+ return static_cast<QDomDocumentPrivate *>(p->ownerNode);
p = p->parent();
}
@@ -1337,7 +1338,7 @@ static void qNormalizeNode(QDomNodePrivate* n)
n->removeChild(p);
p = tmp;
} else {
- t = (QDomTextPrivate*)p;
+ t = static_cast<QDomTextPrivate *>(p);
p = p->next;
}
} else {
@@ -1377,7 +1378,7 @@ void QDomNodePrivate::setLocation(int lineNumber, int columnNumber)
*
**************************************************************/
-#define IMPL ((QDomNodePrivate*)impl)
+#define IMPL static_cast<QDomNodePrivate *>(impl)
/*!
\class QDomNode
@@ -1472,48 +1473,48 @@ QDomNode::QDomNode()
}
/*!
- Constructs a copy of \a n.
+ Constructs a copy of \a node.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomNode::QDomNode(const QDomNode &n)
+QDomNode::QDomNode(const QDomNode &node)
+ : impl(node.impl)
{
- impl = n.impl;
if (impl)
impl->ref.ref();
}
/*! \internal
- Constructs a new node for the data \a n.
+ Constructs a new node for the data \a pimpl.
*/
-QDomNode::QDomNode(QDomNodePrivate *n)
+QDomNode::QDomNode(QDomNodePrivate *pimpl)
+ : impl(pimpl)
{
- impl = n;
if (impl)
impl->ref.ref();
}
/*!
- Assigns a copy of \a n to this DOM node.
+ Assigns a copy of \a other to this DOM node.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomNode& QDomNode::operator=(const QDomNode &n)
+QDomNode& QDomNode::operator=(const QDomNode &other)
{
- if (n.impl)
- n.impl->ref.ref();
+ if (other.impl)
+ other.impl->ref.ref();
if (impl && !impl->ref.deref())
delete impl;
- impl = n.impl;
+ impl = other.impl;
return *this;
}
/*!
- Returns \c true if \a n and this DOM node are equal; otherwise
+ Returns \c true if \a other and this DOM node are equal; otherwise
returns \c false.
Any instance of QDomNode acts as a reference to an underlying data
@@ -1532,18 +1533,18 @@ QDomNode& QDomNode::operator=(const QDomNode &n)
\c {element3 == element4} will return false because they refer to
two different nodes in the underlying data structure.
*/
-bool QDomNode::operator== (const QDomNode& n) const
+bool QDomNode::operator==(const QDomNode &other) const
{
- return (impl == n.impl);
+ return impl == other.impl;
}
/*!
- Returns \c true if \a n and this DOM node are not equal; otherwise
+ Returns \c true if \a other and this DOM node are not equal; otherwise
returns \c false.
*/
-bool QDomNode::operator!= (const QDomNode& n) const
+bool QDomNode::operator!=(const QDomNode &other) const
{
- return (impl != n.impl);
+ return !operator==(other);
}
/*!
@@ -1620,15 +1621,14 @@ QString QDomNode::nodeValue() const
}
/*!
- Sets the node's value to \a v.
+ Sets the node's value to \a value.
\sa nodeValue()
*/
-void QDomNode::setNodeValue(const QString& v)
+void QDomNode::setNodeValue(const QString& value)
{
- if (!impl)
- return;
- IMPL->setNodeValue(v);
+ if (impl)
+ IMPL->setNodeValue(value);
}
/*!
@@ -2502,11 +2502,12 @@ int QDomNode::columnNumber() const
*
**************************************************************/
-QDomNamedNodeMapPrivate::QDomNamedNodeMapPrivate(QDomNodePrivate* n) : ref(1)
+QDomNamedNodeMapPrivate::QDomNamedNodeMapPrivate(QDomNodePrivate *pimpl)
+ : ref(1)
+ , parent(pimpl)
+ , readonly(false)
+ , appendToParent(false)
{
- readonly = false;
- parent = n;
- appendToParent = false;
}
QDomNamedNodeMapPrivate::~QDomNamedNodeMapPrivate()
@@ -2514,16 +2515,16 @@ QDomNamedNodeMapPrivate::~QDomNamedNodeMapPrivate()
clearMap();
}
-QDomNamedNodeMapPrivate* QDomNamedNodeMapPrivate::clone(QDomNodePrivate* p)
+QDomNamedNodeMapPrivate* QDomNamedNodeMapPrivate::clone(QDomNodePrivate *pimpl)
{
- std::unique_ptr<QDomNamedNodeMapPrivate> m(new QDomNamedNodeMapPrivate(p));
+ std::unique_ptr<QDomNamedNodeMapPrivate> m(new QDomNamedNodeMapPrivate(pimpl));
m->readonly = readonly;
m->appendToParent = appendToParent;
auto it = map.constBegin();
for (; it != map.constEnd(); ++it) {
- QDomNodePrivate *new_node = (*it)->cloneNode();
- new_node->setParent(p);
+ QDomNodePrivate *new_node = it.value()->cloneNode();
+ new_node->setParent(pimpl);
m->setNamedItem(new_node);
}
@@ -2538,16 +2539,16 @@ void QDomNamedNodeMapPrivate::clearMap()
if (!appendToParent) {
auto it = map.constBegin();
for (; it != map.constEnd(); ++it)
- if (!(*it)->ref.deref())
- delete *it;
+ if (!it.value()->ref.deref())
+ delete it.value();
}
map.clear();
}
QDomNodePrivate* QDomNamedNodeMapPrivate::namedItem(const QString& name) const
{
- auto it = map.constFind(name);
- return it == map.cend() ? nullptr : *it;
+ auto it = map.find(name);
+ return it == map.end() ? nullptr : it.value();
}
QDomNodePrivate* QDomNamedNodeMapPrivate::namedItemNS(const QString& nsURI, const QString& localName) const
@@ -2555,7 +2556,7 @@ QDomNodePrivate* QDomNamedNodeMapPrivate::namedItemNS(const QString& nsURI, cons
auto it = map.constBegin();
QDomNodePrivate *n;
for (; it != map.constEnd(); ++it) {
- n = *it;
+ n = it.value();
if (!n->prefix.isNull()) {
// node has a namespace
if (n->namespaceURI == nsURI && n->name == localName)
@@ -2622,12 +2623,12 @@ QDomNodePrivate* QDomNamedNodeMapPrivate::item(int index) const
{
if (index >= length() || index < 0)
return nullptr;
- return *std::next(map.cbegin(), index);
+ return std::next(map.begin(), index).value();
}
int QDomNamedNodeMapPrivate::length() const
{
- return map.count();
+ return map.size();
}
bool QDomNamedNodeMapPrivate::contains(const QString& name) const
@@ -2646,7 +2647,7 @@ bool QDomNamedNodeMapPrivate::containsNS(const QString& nsURI, const QString & l
*
**************************************************************/
-#define IMPL ((QDomNamedNodeMapPrivate*)impl)
+#define IMPL static_cast<QDomNamedNodeMapPrivate *>(impl)
/*!
\class QDomNamedNodeMap
@@ -2695,51 +2696,51 @@ QDomNamedNodeMap::QDomNamedNodeMap()
}
/*!
- Constructs a copy of \a n.
+ Constructs a copy of \a namedNodeMap.
*/
-QDomNamedNodeMap::QDomNamedNodeMap(const QDomNamedNodeMap &n)
+QDomNamedNodeMap::QDomNamedNodeMap(const QDomNamedNodeMap &namedNodeMap)
+ : impl(namedNodeMap.impl)
{
- impl = n.impl;
if (impl)
impl->ref.ref();
}
-QDomNamedNodeMap::QDomNamedNodeMap(QDomNamedNodeMapPrivate *n)
+QDomNamedNodeMap::QDomNamedNodeMap(QDomNamedNodeMapPrivate *pimpl)
+ : impl(pimpl)
{
- impl = n;
if (impl)
impl->ref.ref();
}
/*!
- Assigns \a n to this named node map.
+ Assigns \a other to this named node map.
*/
-QDomNamedNodeMap& QDomNamedNodeMap::operator=(const QDomNamedNodeMap &n)
+QDomNamedNodeMap& QDomNamedNodeMap::operator=(const QDomNamedNodeMap &other)
{
- if (n.impl)
- n.impl->ref.ref();
+ if (other.impl)
+ other.impl->ref.ref();
if (impl && !impl->ref.deref())
delete impl;
- impl = n.impl;
+ impl = other.impl;
return *this;
}
/*!
- Returns \c true if \a n and this named node map are equal; otherwise
+ Returns \c true if \a other and this named node map are equal; otherwise
returns \c false.
*/
-bool QDomNamedNodeMap::operator== (const QDomNamedNodeMap& n) const
+bool QDomNamedNodeMap::operator==(const QDomNamedNodeMap &other) const
{
- return (impl == n.impl);
+ return impl == other.impl;
}
/*!
- Returns \c true if \a n and this named node map are not equal;
+ Returns \c true if \a other and this named node map are not equal;
otherwise returns \c false.
*/
-bool QDomNamedNodeMap::operator!= (const QDomNamedNodeMap& n) const
+bool QDomNamedNodeMap::operator!=(const QDomNamedNodeMap &other) const
{
- return (impl != n.impl);
+ return !operator==(other);
}
/*!
@@ -2781,7 +2782,7 @@ QDomNode QDomNamedNodeMap::setNamedItem(const QDomNode& newNode)
{
if (!impl)
return QDomNode();
- return QDomNode(IMPL->setNamedItem((QDomNodePrivate*)newNode.impl));
+ return QDomNode(IMPL->setNamedItem(static_cast<QDomNodePrivate *>(newNode.impl)));
}
/*!
@@ -2843,7 +2844,7 @@ QDomNode QDomNamedNodeMap::setNamedItemNS(const QDomNode& newNode)
{
if (!impl)
return QDomNode();
- return QDomNode(IMPL->setNamedItemNS((QDomNodePrivate*)newNode.impl));
+ return QDomNode(IMPL->setNamedItemNS(static_cast<QDomNodePrivate *>(newNode.impl)));
}
/*!
@@ -3068,11 +3069,11 @@ void QDomDocumentTypePrivate::save(QTextStream& s, int, int indent) const
auto it2 = notations->map.constBegin();
for (; it2 != notations->map.constEnd(); ++it2)
- (*it2)->save(s, 0, indent);
+ it2.value()->save(s, 0, indent);
auto it = entities->map.constBegin();
for (; it != entities->map.constEnd(); ++it)
- (*it)->save(s, 0, indent);
+ it.value()->save(s, 0, indent);
s << ']';
}
@@ -3086,7 +3087,7 @@ void QDomDocumentTypePrivate::save(QTextStream& s, int, int indent) const
*
**************************************************************/
-#define IMPL ((QDomDocumentTypePrivate*)impl)
+#define IMPL static_cast<QDomDocumentTypePrivate *>(impl)
/*!
\class QDomDocumentType
@@ -3115,34 +3116,30 @@ QDomDocumentType::QDomDocumentType() : QDomNode()
}
/*!
- Constructs a copy of \a n.
+ Constructs a copy of \a documentType.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocumentType::QDomDocumentType(const QDomDocumentType& n)
- : QDomNode(n)
+QDomDocumentType::QDomDocumentType(const QDomDocumentType &documentType)
+ : QDomNode(documentType)
{
}
-QDomDocumentType::QDomDocumentType(QDomDocumentTypePrivate* n)
- : QDomNode(n)
+QDomDocumentType::QDomDocumentType(QDomDocumentTypePrivate *pimpl)
+ : QDomNode(pimpl)
{
}
/*!
- Assigns \a n to this document type.
+ Assigns \a other to this document type.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocumentType& QDomDocumentType::operator= (const QDomDocumentType& n)
-{
- return (QDomDocumentType&) QDomNode::operator=(n);
-}
-
+QDomDocumentType &QDomDocumentType::operator=(const QDomDocumentType &other) = default;
/*!
Returns the name of the document type as specified in the
&lt;!DOCTYPE name&gt; tag.
@@ -3300,28 +3297,25 @@ QDomDocumentFragment::QDomDocumentFragment(QDomDocumentFragmentPrivate* n)
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a documentFragment.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocumentFragment::QDomDocumentFragment(const QDomDocumentFragment& x)
- : QDomNode(x)
+QDomDocumentFragment::QDomDocumentFragment(const QDomDocumentFragment &documentFragment)
+ : QDomNode(documentFragment)
{
}
/*!
- Assigns \a x to this DOM document fragment.
+ Assigns \a other to this DOM document fragment.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocumentFragment& QDomDocumentFragment::operator= (const QDomDocumentFragment& x)
-{
- return (QDomDocumentFragment&) QDomNode::operator=(x);
-}
+QDomDocumentFragment &QDomDocumentFragment::operator=(const QDomDocumentFragment &other) = default;
/*!
\fn QDomNode::NodeType QDomDocumentFragment::nodeType() const
@@ -3360,7 +3354,7 @@ QDomNodePrivate* QDomCharacterDataPrivate::cloneNode(bool deep)
int QDomCharacterDataPrivate::dataLength() const
{
- return value.length();
+ return value.size();
}
QString QDomCharacterDataPrivate::substringData(unsigned long offset, unsigned long n) const
@@ -3394,7 +3388,7 @@ void QDomCharacterDataPrivate::appendData(const QString& arg)
*
**************************************************************/
-#define IMPL ((QDomCharacterDataPrivate*)impl)
+#define IMPL static_cast<QDomCharacterDataPrivate *>(impl)
/*!
\class QDomCharacterData
@@ -3429,14 +3423,14 @@ QDomCharacterData::QDomCharacterData()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a characterData.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomCharacterData::QDomCharacterData(const QDomCharacterData& x)
- : QDomNode(x)
+QDomCharacterData::QDomCharacterData(const QDomCharacterData &characterData)
+ : QDomNode(characterData)
{
}
@@ -3446,16 +3440,13 @@ QDomCharacterData::QDomCharacterData(QDomCharacterDataPrivate* n)
}
/*!
- Assigns \a x to this character data.
+ Assigns \a other to this character data.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomCharacterData& QDomCharacterData::operator= (const QDomCharacterData& x)
-{
- return (QDomCharacterData&) QDomNode::operator=(x);
-}
+QDomCharacterData &QDomCharacterData::operator=(const QDomCharacterData &other) = default;
/*!
Returns the string stored in this object.
@@ -3471,12 +3462,12 @@ QString QDomCharacterData::data() const
}
/*!
- Sets this object's string to \a v.
+ Sets this object's string to \a data.
*/
-void QDomCharacterData::setData(const QString& v)
+void QDomCharacterData::setData(const QString &data)
{
if (impl)
- impl->setNodeValue(v);
+ impl->setNodeValue(data);
}
/*!
@@ -3621,7 +3612,7 @@ static QString encodeText(const QString &str,
const bool encodeEOLs = false)
{
QString retval(str);
- int len = retval.length();
+ int len = retval.size();
int i = 0;
while (i < len) {
@@ -3649,8 +3640,8 @@ static QString encodeText(const QString &str,
ati == QChar(0x9))) {
const QString replacement(u"&#x"_s + QString::number(ati.unicode(), 16) + u';');
retval.replace(i, 1, replacement);
- i += replacement.length();
- len += replacement.length() - 1;
+ i += replacement.size();
+ len += replacement.size() - 1;
} else if (encodeEOLs && ati == QChar(0xD)) {
retval.replace(i, 1, "&#xd;"_L1); // Replace a single 0xD with a ref for 0xD
len += 4;
@@ -3693,7 +3684,7 @@ void QDomAttrPrivate::save(QTextStream& s, int, int) const
*
**************************************************************/
-#define IMPL ((QDomAttrPrivate*)impl)
+#define IMPL static_cast<QDomAttrPrivate *>(impl)
/*!
\class QDomAttr
@@ -3738,14 +3729,14 @@ QDomAttr::QDomAttr()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a attr.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomAttr::QDomAttr(const QDomAttr& x)
- : QDomNode(x)
+QDomAttr::QDomAttr(const QDomAttr &attr)
+ : QDomNode(attr)
{
}
@@ -3755,16 +3746,13 @@ QDomAttr::QDomAttr(QDomAttrPrivate* n)
}
/*!
- Assigns \a x to this DOM attribute.
+ Assigns \a other to this DOM attribute.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomAttr& QDomAttr::operator= (const QDomAttr& x)
-{
- return (QDomAttr&) QDomNode::operator=(x);
-}
+QDomAttr &QDomAttr::operator=(const QDomAttr &other) = default;
/*!
Returns the attribute's name.
@@ -3799,7 +3787,7 @@ QDomElement QDomAttr::ownerElement() const
Q_ASSERT(impl->parent());
if (!impl->parent()->isElement())
return QDomElement();
- return QDomElement((QDomElementPrivate*)(impl->parent()));
+ return QDomElement(static_cast<QDomElementPrivate *>(impl->parent()));
}
/*!
@@ -3816,15 +3804,15 @@ QString QDomAttr::value() const
}
/*!
- Sets the attribute's value to \a v.
+ Sets the attribute's value to \a value.
\sa value()
*/
-void QDomAttr::setValue(const QString& v)
+void QDomAttr::setValue(const QString &value)
{
if (!impl)
return;
- impl->setNodeValue(v);
+ impl->setNodeValue(value);
IMPL->m_specified = true;
}
@@ -3944,12 +3932,12 @@ void QDomElementPrivate::removeAttribute(const QString& aname)
QDomAttrPrivate* QDomElementPrivate::attributeNode(const QString& aname)
{
- return (QDomAttrPrivate*)m_attr->namedItem(aname);
+ return static_cast<QDomAttrPrivate *>(m_attr->namedItem(aname));
}
QDomAttrPrivate* QDomElementPrivate::attributeNodeNS(const QString& nsURI, const QString& localName)
{
- return (QDomAttrPrivate*)m_attr->namedItemNS(nsURI, localName);
+ return static_cast<QDomAttrPrivate *>(m_attr->namedItemNS(nsURI, localName));
}
QDomAttrPrivate* QDomElementPrivate::setAttributeNode(QDomAttrPrivate* newAttr)
@@ -3961,7 +3949,7 @@ QDomAttrPrivate* QDomElementPrivate::setAttributeNode(QDomAttrPrivate* newAttr)
newAttr->setParent(this);
- return (QDomAttrPrivate*)n;
+ return static_cast<QDomAttrPrivate *>(n);
}
QDomAttrPrivate* QDomElementPrivate::setAttributeNodeNS(QDomAttrPrivate* newAttr)
@@ -3973,12 +3961,12 @@ QDomAttrPrivate* QDomElementPrivate::setAttributeNodeNS(QDomAttrPrivate* newAttr
// Referencing is done by the maps
m_attr->setNamedItem(newAttr);
- return (QDomAttrPrivate*)n;
+ return static_cast<QDomAttrPrivate *>(n);
}
QDomAttrPrivate* QDomElementPrivate::removeAttributeNode(QDomAttrPrivate* oldAttr)
{
- return (QDomAttrPrivate*)m_attr->removeNamedItem(oldAttr->nodeName());
+ return static_cast<QDomAttrPrivate *>(m_attr->removeNamedItem(oldAttr->nodeName()));
}
bool QDomElementPrivate::hasAttribute(const QString& aname)
@@ -4000,7 +3988,7 @@ QString QDomElementPrivate::text()
if (p->isText() || p->isCDATASection())
t += p->nodeValue();
else if (p->isElement())
- t += ((QDomElementPrivate*)p)->text();
+ t += static_cast<QDomElementPrivate *>(p)->text();
p = p->next;
}
@@ -4038,32 +4026,83 @@ void QDomElementPrivate::save(QTextStream& s, int depth, int indent) const
/* Write out attributes. */
if (!m_attr->map.isEmpty()) {
+ /*
+ * To ensure that we always output attributes in a consistent
+ * order, sort the attributes before writing them into the
+ * stream. (Note that the order may be different than the one
+ * that e.g. we've read from a file, or the program order in
+ * which these attributes have been populated. We just want to
+ * guarantee reproducibile outputs.)
+ */
+ struct SavedAttribute {
+ QString prefix;
+ QString name;
+ QString encodedValue;
+ };
+
+ /* Gather all the attributes to save. */
+ QVarLengthArray<SavedAttribute, 8> attributesToSave;
+ attributesToSave.reserve(m_attr->map.size());
+
QDuplicateTracker<QString> outputtedPrefixes;
- auto it = m_attr->map.constBegin();
- for (; it != m_attr->map.constEnd(); ++it) {
- s << ' ';
- if (it.value()->namespaceURI.isNull()) {
- s << it.value()->name << "=\"" << encodeText(it.value()->value, true, true) << '\"';
- } else {
- s << it.value()->prefix << ':' << it.value()->name << "=\"" << encodeText(it.value()->value, true, true) << '\"';
- /* This is a fix for 138243, as good as it gets.
- *
- * QDomElementPrivate::save() output a namespace declaration if
- * the element is in a namespace, no matter what. This function do as well, meaning
- * that we get two identical namespace declaration if we don't have the if-
- * statement below.
- *
- * This doesn't work when the parent element has the same prefix as us but
- * a different namespace. However, this can only occur by the user modifying the element,
- * and we don't do fixups by that anyway, and hence it's the user responsibility to not
- * arrive in those situations. */
- if ((!it.value()->ownerNode ||
- it.value()->ownerNode->prefix != it.value()->prefix) &&
- !outputtedPrefixes.hasSeen(it.value()->prefix)) {
- s << " xmlns:" << it.value()->prefix << "=\"" << encodeText(it.value()->namespaceURI, true, true) << '\"';
- }
+ for (const auto &[key, value] : std::as_const(m_attr->map).asKeyValueRange()) {
+ Q_UNUSED(key); /* We extract the attribute name from the value. */
+ bool mayNeedXmlNS = false;
+
+ SavedAttribute attr;
+ attr.name = value->name;
+ attr.encodedValue = encodeText(value->value, true, true);
+ if (!value->namespaceURI.isNull()) {
+ attr.prefix = value->prefix;
+ mayNeedXmlNS = true;
+ }
+
+ attributesToSave.push_back(std::move(attr));
+
+ /*
+ * This is a fix for 138243, as good as it gets.
+ *
+ * QDomElementPrivate::save() output a namespace
+ * declaration if the element is in a namespace, no matter
+ * what. This function do as well, meaning that we get two
+ * identical namespace declaration if we don't have the if-
+ * statement below.
+ *
+ * This doesn't work when the parent element has the same
+ * prefix as us but a different namespace. However, this
+ * can only occur by the user modifying the element, and we
+ * don't do fixups by that anyway, and hence it's the user
+ * responsibility to avoid those situations.
+ */
+
+ if (mayNeedXmlNS
+ && ((!value->ownerNode || value->ownerNode->prefix != value->prefix)
+ && !outputtedPrefixes.hasSeen(value->prefix)))
+ {
+ SavedAttribute nsAttr;
+ nsAttr.prefix = QStringLiteral("xmlns");
+ nsAttr.name = value->prefix;
+ nsAttr.encodedValue = encodeText(value->namespaceURI, true, true);
+ attributesToSave.push_back(std::move(nsAttr));
}
}
+
+ /* Sort the attributes by prefix and name. */
+ const auto savedAttributeComparator = [](const SavedAttribute &lhs, const SavedAttribute &rhs)
+ {
+ const int cmp = QString::compare(lhs.prefix, rhs.prefix);
+ return (cmp < 0) || ((cmp == 0) && (lhs.name < rhs.name));
+ };
+
+ std::sort(attributesToSave.begin(), attributesToSave.end(), savedAttributeComparator);
+
+ /* Actually stream the sorted attributes. */
+ for (const auto &attr : attributesToSave) {
+ s << ' ';
+ if (!attr.prefix.isEmpty())
+ s << attr.prefix << ':';
+ s << attr.name << "=\"" << attr.encodedValue << '\"';
+ }
}
if (last) {
@@ -4097,7 +4136,7 @@ void QDomElementPrivate::save(QTextStream& s, int depth, int indent) const
*
**************************************************************/
-#define IMPL ((QDomElementPrivate*)impl)
+#define IMPL static_cast<QDomElementPrivate *>(impl)
/*!
\class QDomElement
@@ -4134,7 +4173,7 @@ void QDomElementPrivate::save(QTextStream& s, int depth, int indent) const
n.toText().data() directly on the node, because the node may not
be a text element.
- You can get a list of all the decendents of an element which have
+ You can get a list of all the descendents of an element which have
a specified tag name with elementsByTagName() or
elementsByTagNameNS().
@@ -4161,14 +4200,14 @@ QDomElement::QDomElement()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a element.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomElement::QDomElement(const QDomElement& x)
- : QDomNode(x)
+QDomElement::QDomElement(const QDomElement &element)
+ : QDomNode(element)
{
}
@@ -4178,16 +4217,13 @@ QDomElement::QDomElement(QDomElementPrivate* n)
}
/*!
- Assigns \a x to this DOM element.
+ Assigns \a other to this DOM element.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomElement& QDomElement::operator= (const QDomElement& x)
-{
- return (QDomElement&) QDomNode::operator=(x);
-}
+QDomElement &QDomElement::operator=(const QDomElement &other) = default;
/*!
\fn QDomNode::NodeType QDomElement::nodeType() const
@@ -4372,7 +4408,7 @@ QDomAttr QDomElement::setAttributeNode(const QDomAttr& newAttr)
{
if (!impl)
return QDomAttr();
- return QDomAttr(IMPL->setAttributeNode(((QDomAttrPrivate*)newAttr.impl)));
+ return QDomAttr(IMPL->setAttributeNode(static_cast<QDomAttrPrivate *>(newAttr.impl)));
}
/*!
@@ -4384,7 +4420,7 @@ QDomAttr QDomElement::removeAttributeNode(const QDomAttr& oldAttr)
{
if (!impl)
return QDomAttr(); // ### should this return oldAttr?
- return QDomAttr(IMPL->removeAttributeNode(((QDomAttrPrivate*)oldAttr.impl)));
+ return QDomAttr(IMPL->removeAttributeNode(static_cast<QDomAttrPrivate *>(oldAttr.impl)));
}
/*!
@@ -4546,7 +4582,7 @@ QDomAttr QDomElement::setAttributeNodeNS(const QDomAttr& newAttr)
{
if (!impl)
return QDomAttr();
- return QDomAttr(IMPL->setAttributeNodeNS(((QDomAttrPrivate*)newAttr.impl)));
+ return QDomAttr(IMPL->setAttributeNodeNS(static_cast<QDomAttrPrivate *>(newAttr.impl)));
}
/*!
@@ -4634,6 +4670,10 @@ QDomTextPrivate* QDomTextPrivate::splitText(int offset)
value.truncate(offset);
parent()->insertAfter(t, this);
+ Q_ASSERT(t->ref.loadRelaxed() == 2);
+
+ // We are not interested in this node
+ t->ref.deref();
return t;
}
@@ -4650,7 +4690,7 @@ void QDomTextPrivate::save(QTextStream& s, int, int) const
*
**************************************************************/
-#define IMPL ((QDomTextPrivate*)impl)
+#define IMPL static_cast<QDomTextPrivate *>(impl)
/*!
\class QDomText
@@ -4681,14 +4721,14 @@ QDomText::QDomText()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a text.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomText::QDomText(const QDomText& x)
- : QDomCharacterData(x)
+QDomText::QDomText(const QDomText &text)
+ : QDomCharacterData(text)
{
}
@@ -4698,16 +4738,13 @@ QDomText::QDomText(QDomTextPrivate* n)
}
/*!
- Assigns \a x to this DOM text.
+ Assigns \a other to this DOM text.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomText& QDomText::operator= (const QDomText& x)
-{
- return (QDomText&) QDomNode::operator=(x);
-}
+QDomText &QDomText::operator=(const QDomText &other) = default;
/*!
\fn QDomNode::NodeType QDomText::nodeType() const
@@ -4812,14 +4849,14 @@ QDomComment::QDomComment()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a comment.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomComment::QDomComment(const QDomComment& x)
- : QDomCharacterData(x)
+QDomComment::QDomComment(const QDomComment &comment)
+ : QDomCharacterData(comment)
{
}
@@ -4829,16 +4866,13 @@ QDomComment::QDomComment(QDomCommentPrivate* n)
}
/*!
- Assigns \a x to this DOM comment.
+ Assigns \a other to this DOM comment.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomComment& QDomComment::operator= (const QDomComment& x)
-{
- return (QDomComment&) QDomNode::operator=(x);
-}
+QDomComment &QDomComment::operator=(const QDomComment &other) = default;
/*!
\fn QDomNode::NodeType QDomComment::nodeType() const
@@ -4920,14 +4954,14 @@ QDomCDATASection::QDomCDATASection()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a cdataSection.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomCDATASection::QDomCDATASection(const QDomCDATASection& x)
- : QDomText(x)
+QDomCDATASection::QDomCDATASection(const QDomCDATASection &cdataSection)
+ : QDomText(cdataSection)
{
}
@@ -4937,16 +4971,13 @@ QDomCDATASection::QDomCDATASection(QDomCDATASectionPrivate* n)
}
/*!
- Assigns \a x to this CDATA section.
+ Assigns \a other to this CDATA section.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomCDATASection& QDomCDATASection::operator= (const QDomCDATASection& x)
-{
- return (QDomCDATASection&) QDomNode::operator=(x);
-}
+QDomCDATASection &QDomCDATASection::operator=(const QDomCDATASection &other) = default;
/*!
\fn QDomNode::NodeType QDomCDATASection::nodeType() const
@@ -5004,7 +5035,7 @@ void QDomNotationPrivate::save(QTextStream& s, int, int) const
*
**************************************************************/
-#define IMPL ((QDomNotationPrivate*)impl)
+#define IMPL static_cast<QDomNotationPrivate *>(impl)
/*!
\class QDomNotation
@@ -5044,14 +5075,14 @@ QDomNotation::QDomNotation()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a notation.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomNotation::QDomNotation(const QDomNotation& x)
- : QDomNode(x)
+QDomNotation::QDomNotation(const QDomNotation &notation)
+ : QDomNode(notation)
{
}
@@ -5061,16 +5092,13 @@ QDomNotation::QDomNotation(QDomNotationPrivate* n)
}
/*!
- Assigns \a x to this DOM notation.
+ Assigns \a other to this DOM notation.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomNotation& QDomNotation::operator= (const QDomNotation& x)
-{
- return (QDomNotation&) QDomNode::operator=(x);
-}
+QDomNotation &QDomNotation::operator=(const QDomNotation &other) = default;
/*!
\fn QDomNode::NodeType QDomNotation::nodeType() const
@@ -5197,7 +5225,7 @@ void QDomEntityPrivate::save(QTextStream& s, int, int) const
*
**************************************************************/
-#define IMPL ((QDomEntityPrivate*)impl)
+#define IMPL static_cast<QDomEntityPrivate *>(impl)
/*!
\class QDomEntity
@@ -5241,14 +5269,14 @@ QDomEntity::QDomEntity()
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a entity.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomEntity::QDomEntity(const QDomEntity& x)
- : QDomNode(x)
+QDomEntity::QDomEntity(const QDomEntity &entity)
+ : QDomNode(entity)
{
}
@@ -5258,16 +5286,13 @@ QDomEntity::QDomEntity(QDomEntityPrivate* n)
}
/*!
- Assigns \a x to this DOM entity.
+ Assigns \a other to this DOM entity.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomEntity& QDomEntity::operator= (const QDomEntity& x)
-{
- return (QDomEntity&) QDomNode::operator=(x);
-}
+QDomEntity &QDomEntity::operator=(const QDomEntity &other) = default;
/*!
\fn QDomNode::NodeType QDomEntity::nodeType() const
@@ -5393,14 +5418,14 @@ QDomEntityReference::QDomEntityReference()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a entityReference.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomEntityReference::QDomEntityReference(const QDomEntityReference& x)
- : QDomNode(x)
+QDomEntityReference::QDomEntityReference(const QDomEntityReference &entityReference)
+ : QDomNode(entityReference)
{
}
@@ -5410,16 +5435,13 @@ QDomEntityReference::QDomEntityReference(QDomEntityReferencePrivate* n)
}
/*!
- Assigns \a x to this entity reference.
+ Assigns \a other to this entity reference.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomEntityReference& QDomEntityReference::operator= (const QDomEntityReference& x)
-{
- return (QDomEntityReference&) QDomNode::operator=(x);
-}
+QDomEntityReference &QDomEntityReference::operator=(const QDomEntityReference &other) = default;
/*!
\fn QDomNode::NodeType QDomEntityReference::nodeType() const
@@ -5510,14 +5532,14 @@ QDomProcessingInstruction::QDomProcessingInstruction()
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a processingInstruction.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomProcessingInstruction::QDomProcessingInstruction(const QDomProcessingInstruction& x)
- : QDomNode(x)
+QDomProcessingInstruction::QDomProcessingInstruction(const QDomProcessingInstruction &processingInstruction)
+ : QDomNode(processingInstruction)
{
}
@@ -5527,16 +5549,14 @@ QDomProcessingInstruction::QDomProcessingInstruction(QDomProcessingInstructionPr
}
/*!
- Assigns \a x to this processing instruction.
+ Assigns \a other to this processing instruction.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomProcessingInstruction& QDomProcessingInstruction::operator= (const QDomProcessingInstruction& x)
-{
- return (QDomProcessingInstruction&) QDomNode::operator=(x);
-}
+QDomProcessingInstruction &
+QDomProcessingInstruction::operator=(const QDomProcessingInstruction &other) = default;
/*!
\fn QDomNode::NodeType QDomProcessingInstruction::nodeType() const
@@ -5569,15 +5589,14 @@ QString QDomProcessingInstruction::data() const
}
/*!
- Sets the data contained in the processing instruction to \a d.
+ Sets the data contained in the processing instruction to \a data.
\sa data()
*/
-void QDomProcessingInstruction::setData(const QString& d)
+void QDomProcessingInstruction::setData(const QString &data)
{
- if (!impl)
- return;
- impl->setNodeValue(d);
+ if (impl)
+ impl->setNodeValue(data);
}
/**************************************************************
@@ -5644,8 +5663,8 @@ void QDomDocumentPrivate::clear()
QDomNodePrivate::clear();
}
-bool QDomDocumentPrivate::setContent(QXmlStreamReader *reader, bool namespaceProcessing,
- QString *errorMsg, int *errorLine, int *errorColumn)
+QDomDocument::ParseResult QDomDocumentPrivate::setContent(QXmlStreamReader *reader,
+ QDomDocument::ParseOptions options)
{
clear();
impl = new QDomImplementationPrivate;
@@ -5653,23 +5672,16 @@ bool QDomDocumentPrivate::setContent(QXmlStreamReader *reader, bool namespacePro
type->ref.deref();
if (!reader) {
- qWarning("Failed to set content, XML reader is not initialized");
- return false;
+ const auto error = u"Failed to set content, XML reader is not initialized"_s;
+ qWarning("%s", qPrintable(error));
+ return { error };
}
- QDomParser domParser(this, reader, namespaceProcessing);
+ QDomParser domParser(this, reader, options);
- if (!domParser.parse()) {
- if (errorMsg)
- *errorMsg = std::get<0>(domParser.errorInfo());
- if (errorLine)
- *errorLine = std::get<1>(domParser.errorInfo());
- if (errorColumn)
- *errorColumn = std::get<2>(domParser.errorInfo());
- return false;
- }
-
- return true;
+ if (!domParser.parse())
+ return domParser.result();
+ return {};
}
QDomNodePrivate* QDomDocumentPrivate::cloneNode(bool deep)
@@ -5715,7 +5727,7 @@ QDomElementPrivate* QDomDocumentPrivate::createElementNS(const QString &nsURI, c
QDomDocumentFragmentPrivate* QDomDocumentPrivate::createDocumentFragment()
{
- QDomDocumentFragmentPrivate *f = new QDomDocumentFragmentPrivate(this, (QDomNodePrivate*)nullptr);
+ QDomDocumentFragmentPrivate *f = new QDomDocumentFragmentPrivate(this, nullptr);
f->ref.deref();
return f;
}
@@ -5813,34 +5825,38 @@ QDomNodePrivate* QDomDocumentPrivate::importNode(QDomNodePrivate *importedNode,
QDomNodePrivate *node = nullptr;
switch (importedNode->nodeType()) {
case QDomNode::AttributeNode:
- node = new QDomAttrPrivate((QDomAttrPrivate*)importedNode, true);
+ node = new QDomAttrPrivate(static_cast<QDomAttrPrivate *>(importedNode), true);
break;
case QDomNode::DocumentFragmentNode:
- node = new QDomDocumentFragmentPrivate((QDomDocumentFragmentPrivate*)importedNode, deep);
+ node = new QDomDocumentFragmentPrivate(
+ static_cast<QDomDocumentFragmentPrivate *>(importedNode), deep);
break;
case QDomNode::ElementNode:
- node = new QDomElementPrivate((QDomElementPrivate*)importedNode, deep);
+ node = new QDomElementPrivate(static_cast<QDomElementPrivate *>(importedNode), deep);
break;
case QDomNode::EntityNode:
- node = new QDomEntityPrivate((QDomEntityPrivate*)importedNode, deep);
+ node = new QDomEntityPrivate(static_cast<QDomEntityPrivate *>(importedNode), deep);
break;
case QDomNode::EntityReferenceNode:
- node = new QDomEntityReferencePrivate((QDomEntityReferencePrivate*)importedNode, false);
+ node = new QDomEntityReferencePrivate(
+ static_cast<QDomEntityReferencePrivate *>(importedNode), false);
break;
case QDomNode::NotationNode:
- node = new QDomNotationPrivate((QDomNotationPrivate*)importedNode, deep);
+ node = new QDomNotationPrivate(static_cast<QDomNotationPrivate *>(importedNode), deep);
break;
case QDomNode::ProcessingInstructionNode:
- node = new QDomProcessingInstructionPrivate((QDomProcessingInstructionPrivate*)importedNode, deep);
+ node = new QDomProcessingInstructionPrivate(
+ static_cast<QDomProcessingInstructionPrivate *>(importedNode), deep);
break;
case QDomNode::TextNode:
- node = new QDomTextPrivate((QDomTextPrivate*)importedNode, deep);
+ node = new QDomTextPrivate(static_cast<QDomTextPrivate *>(importedNode), deep);
break;
case QDomNode::CDATASectionNode:
- node = new QDomCDATASectionPrivate((QDomCDATASectionPrivate*)importedNode, deep);
+ node = new QDomCDATASectionPrivate(static_cast<QDomCDATASectionPrivate *>(importedNode),
+ deep);
break;
case QDomNode::CommentNode:
- node = new QDomCommentPrivate((QDomCommentPrivate*)importedNode, deep);
+ node = new QDomCommentPrivate(static_cast<QDomCommentPrivate *>(importedNode), deep);
break;
default:
break;
@@ -5927,7 +5943,7 @@ void QDomDocumentPrivate::saveDocument(QTextStream& s, const int indent, QDomNod
*
**************************************************************/
-#define IMPL ((QDomDocumentPrivate*)impl)
+#define IMPL static_cast<QDomDocumentPrivate *>(impl)
/*!
\class QDomDocument
@@ -5978,8 +5994,8 @@ void QDomDocumentPrivate::saveDocument(QTextStream& s, const int indent, QDomNod
representation of the document can be obtained using toString().
\note The DOM tree might end up reserving a lot of memory if the XML
- document is big. For such documents, the QXmlStreamReader or the
- QXmlQuery classes might be better solutions.
+ document is big. For such documents, the QXmlStreamReader class
+ might be a better solution.
It is possible to insert a node from another document into the
document using importNode().
@@ -6004,10 +6020,9 @@ void QDomDocumentPrivate::saveDocument(QTextStream& s, const int indent, QDomNod
\l{http://www.w3.org/TR/DOM-Level-2-Core/}{Level 2 Core}
Specifications.
- \sa {DOM Bookmarks Example}, {Simple DOM Model Example}
+ \sa {DOM Bookmarks Application}
*/
-
/*!
Constructs an empty document.
*/
@@ -6033,37 +6048,34 @@ QDomDocument::QDomDocument(const QString& name)
*/
QDomDocument::QDomDocument(const QDomDocumentType& doctype)
{
- impl = new QDomDocumentPrivate((QDomDocumentTypePrivate*)(doctype.impl));
+ impl = new QDomDocumentPrivate(static_cast<QDomDocumentTypePrivate *>(doctype.impl));
}
/*!
- Constructs a copy of \a x.
+ Constructs a copy of \a document.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocument::QDomDocument(const QDomDocument& x)
- : QDomNode(x)
+QDomDocument::QDomDocument(const QDomDocument &document)
+ : QDomNode(document)
{
}
-QDomDocument::QDomDocument(QDomDocumentPrivate* x)
- : QDomNode(x)
+QDomDocument::QDomDocument(QDomDocumentPrivate *pimpl)
+ : QDomNode(pimpl)
{
}
/*!
- Assigns \a x to this DOM document.
+ Assigns \a other to this DOM document.
The data of the copy is shared (shallow copy): modifying one node
will also change the other. If you want to make a deep copy, use
cloneNode().
*/
-QDomDocument& QDomDocument::operator= (const QDomDocument& x)
-{
- return (QDomDocument&) QDomNode::operator=(x);
-}
+QDomDocument &QDomDocument::operator=(const QDomDocument &other) = default;
/*!
Destroys the object and frees its resources.
@@ -6072,25 +6084,30 @@ QDomDocument::~QDomDocument()
{
}
+#if QT_DEPRECATED_SINCE(6, 8)
+QT_WARNING_PUSH
+QT_WARNING_DISABLE_DEPRECATED
/*!
\overload
+ \deprecated [6.8] Use the overloads taking ParseOptions instead.
This function reads the XML document from the string \a text, returning
true if the content was successfully parsed; otherwise returns \c false.
Since \a text is already a Unicode string, no encoding detection
is done.
*/
-bool QDomDocument::setContent(const QString& text, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
+bool QDomDocument::setContent(const QString& text, bool namespaceProcessing,
+ QString *errorMsg, int *errorLine, int *errorColumn)
{
- if (!impl)
- impl = new QDomDocumentPrivate();
-
- QXmlStreamReader streamReader(text);
- streamReader.setNamespaceProcessing(namespaceProcessing);
- return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn);
+ QXmlStreamReader reader(text);
+ reader.setNamespaceProcessing(namespaceProcessing);
+ return setContent(&reader, namespaceProcessing, errorMsg, errorLine, errorColumn);
}
/*!
+ \deprecated [6.8] Use the overload taking ParseOptions instead.
+ \overload
+
This function parses the XML document from the byte array \a
data and sets it as the content of the document. It tries to
detect the encoding of the document as required by the XML
@@ -6105,26 +6122,21 @@ bool QDomDocument::setContent(const QString& text, bool namespaceProcessing, QSt
If a parse error occurs, this function returns \c false and the error
message is placed in \c{*}\a{errorMsg}, the line number in
\c{*}\a{errorLine} and the column number in \c{*}\a{errorColumn}
- (unless the associated pointer is set to 0); otherwise this
- function returns \c true. The various error messages are described in
- the QXmlParseException class documentation. Note that, if you
- want to display these error messages to your application's users,
- they will be displayed in English unless they are explicitly
- translated.
+ (unless the associated pointer is set to \c nullptr); otherwise this
+ function returns \c true.
If \a namespaceProcessing is true, the function QDomNode::prefix()
returns a string for all elements and attributes. It returns an
empty string if the element or attribute has no prefix.
Text nodes consisting only of whitespace are stripped and won't
- appear in the QDomDocument. If this behavior is not desired,
- one can use the setContent() overload that allows a QXmlReader to be
- supplied.
+ appear in the QDomDocument.
If \a namespaceProcessing is false, the functions
QDomNode::prefix(), QDomNode::localName() and
QDomNode::namespaceURI() return an empty string.
+//! [entity-refs]
Entity references are handled as follows:
\list
\li References to internal general entities and character entities occurring in the
@@ -6139,22 +6151,41 @@ bool QDomDocument::setContent(const QString& text, bool namespaceProcessing, QSt
occurs outside of the content is replaced with an empty string.
\li Any unparsed entity reference is replaced with an empty string.
\endlist
+//! [entity-refs]
\sa QDomNode::namespaceURI(), QDomNode::localName(),
QDomNode::prefix(), QString::isNull(), QString::isEmpty()
*/
-bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
+bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing,
+ QString *errorMsg, int *errorLine, int *errorColumn)
{
- if (!impl)
- impl = new QDomDocumentPrivate();
+ QXmlStreamReader reader(data);
+ reader.setNamespaceProcessing(namespaceProcessing);
+ return setContent(&reader, namespaceProcessing, errorMsg, errorLine, errorColumn);
+}
- QXmlStreamReader streamReader(data);
- streamReader.setNamespaceProcessing(namespaceProcessing);
- return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn);
+static inline QDomDocument::ParseOptions toParseOptions(bool namespaceProcessing)
+{
+ return namespaceProcessing ? QDomDocument::ParseOption::UseNamespaceProcessing
+ : QDomDocument::ParseOption::Default;
+}
+
+static inline void unpackParseResult(const QDomDocument::ParseResult &parseResult,
+ QString *errorMsg, int *errorLine, int *errorColumn)
+{
+ if (!parseResult) {
+ if (errorMsg)
+ *errorMsg = parseResult.errorMessage;
+ if (errorLine)
+ *errorLine = static_cast<int>(parseResult.errorLine);
+ if (errorColumn)
+ *errorColumn = static_cast<int>(parseResult.errorColumn);
+ }
}
/*!
\overload
+ \deprecated [6.8] Use the overload taking ParseOptions instead.
This function reads the XML document from the IO device \a dev, returning
true if the content was successfully parsed; otherwise returns \c false.
@@ -6162,31 +6193,19 @@ bool QDomDocument::setContent(const QByteArray &data, bool namespaceProcessing,
\note This method will try to open \a dev in read-only mode if it is not
already open. In that case, the caller is responsible for calling close.
This will change in Qt 7, which will no longer open \a dev. Applications
- shoul therefore open the device themselves before calling setContent.
+ should therefore open the device themselves before calling setContent.
*/
-bool QDomDocument::setContent(QIODevice* dev, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
+bool QDomDocument::setContent(QIODevice* dev, bool namespaceProcessing,
+ QString *errorMsg, int *errorLine, int *errorColumn)
{
- if (!impl)
- impl = new QDomDocumentPrivate();
-
-#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
- if (!dev->isOpen()) {
- qWarning("QDomDocument called with unopened QIODevice. "
- "This will not be supported in future Qt versions");
- if (!dev->open(QIODevice::ReadOnly)) {
- qWarning("QDomDocument::setContent: Failed to open device");
- return false;
- }
- }
-#endif
-
- QXmlStreamReader streamReader(dev);
- streamReader.setNamespaceProcessing(namespaceProcessing);
- return IMPL->setContent(&streamReader, namespaceProcessing, errorMsg, errorLine, errorColumn);
+ ParseResult result = setContent(dev, toParseOptions(namespaceProcessing));
+ unpackParseResult(result, errorMsg, errorLine, errorColumn);
+ return bool(result);
}
/*!
\overload
+ \deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the string \a text, returning
true if the content was successfully parsed; otherwise returns \c false.
@@ -6202,6 +6221,7 @@ bool QDomDocument::setContent(const QString& text, QString *errorMsg, int *error
/*!
\overload
+ \deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the byte array \a buffer,
returning true if the content was successfully parsed; otherwise returns
@@ -6216,7 +6236,7 @@ bool QDomDocument::setContent(const QByteArray& buffer, QString *errorMsg, int *
/*!
\overload
- \deprecated
+ \deprecated [6.8] Use the overload returning ParseResult instead.
This function reads the XML document from the IO device \a dev, returning
true if the content was successfully parsed; otherwise returns \c false.
@@ -6231,6 +6251,7 @@ bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine,
/*!
\overload
\since 5.15
+ \deprecated [6.8] Use the overload taking ParseOptions instead.
This function reads the XML document from the QXmlStreamReader \a reader
and parses it. Returns \c true if the content was successfully parsed;
@@ -6243,16 +6264,169 @@ bool QDomDocument::setContent(QIODevice* dev, QString *errorMsg, int *errorLine,
If a parse error occurs, the error message is placed in \c{*}\a{errorMsg}, the line
number in \c{*}\a{errorLine} and the column number in \c{*}\a{errorColumn} (unless
- the associated pointer is set to 0).
+ the associated pointer is set to \c nullptr).
\sa QXmlStreamReader
*/
-bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg,
- int *errorLine, int *errorColumn)
+bool QDomDocument::setContent(QXmlStreamReader *reader, bool namespaceProcessing,
+ QString *errorMsg, int *errorLine, int *errorColumn)
+{
+ ParseResult result = setContent(reader, toParseOptions(namespaceProcessing));
+ unpackParseResult(result, errorMsg, errorLine, errorColumn);
+ return bool(result);
+}
+QT_WARNING_POP
+#endif // QT_DEPRECATED_SINCE(6, 8)
+
+/*!
+ \enum QDomDocument::ParseOption
+ \since 6.5
+
+ This enum describes the possible options that can be used when
+ parsing an XML document using the setContent() method.
+
+ \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()
+*/
+
+/*!
+ \struct QDomDocument::ParseResult
+ \since 6.5
+ \inmodule QtXml
+ \ingroup xml-tools
+ \brief The struct is used to store the result of QDomDocument::setContent().
+
+ The QDomDocument::ParseResult struct is used for storing the result of
+ QDomDocument::setContent(). If an error is found while parsing an XML
+ document, the message, line and column number of an error are stored in
+ \c ParseResult.
+
+ \sa QDomDocument::setContent()
+*/
+
+/*!
+ \variable QDomDocument::ParseResult::errorMessage
+
+ The field contains the text message of an error found by
+ QDomDocument::setContent() while parsing an XML document.
+
+ \sa QDomDocument::setContent()
+*/
+
+/*!
+ \variable QDomDocument::ParseResult::errorLine
+
+ The field contains the line number of an error found by
+ QDomDocument::setContent() while parsing an XML document.
+
+ \sa QDomDocument::setContent()
+*/
+
+/*!
+ \variable QDomDocument::ParseResult::errorColumn
+
+ The field contains the column number of an error found by
+ QDomDocument::setContent() while parsing an XML document.
+
+ \sa QDomDocument::setContent()
+*/
+
+/*!
+ \fn QDomDocument::ParseResult::operator bool() const
+
+ Returns \c false if any error is found by QDomDocument::setContent();
+ otherwise returns \c true.
+
+ \sa QDomDocument::setContent()
+*/
+
+/*!
+ \fn ParseResult QDomDocument::setContent(const QByteArray &data, ParseOptions options)
+ \fn ParseResult QDomDocument::setContent(QAnyStringView text, ParseOptions options)
+ \fn ParseResult QDomDocument::setContent(QIODevice *device, ParseOptions options)
+ \fn ParseResult QDomDocument::setContent(QXmlStreamReader *reader, ParseOptions options)
+
+ \since 6.5
+
+ This function parses the XML document from the byte array \a
+ data, string view \a text, IO \a device, or stream \a reader
+ and sets it as the content of the document. It tries to
+ detect the encoding of the document, in accordance with the
+ XML specification. Returns the result of parsing in ParseResult,
+ which explicitly converts to \c bool.
+
+ You can use the \a options parameter to specify different parsing
+ options, for example, to enable namespace processing, etc.
+
+ By default, namespace processing is disabled. If it's disabled, the
+ parser does no namespace processing when it reads the XML file. The
+ functions QDomNode::prefix(), QDomNode::localName() and
+ QDomNode::namespaceURI() return an empty string.
+
+ If namespace processing is enabled via the parse \a options, the parser
+ recognizes namespaces in the XML file and sets the prefix name, local
+ name and namespace URI to appropriate values. The functions
+ QDomNode::prefix(), QDomNode::localName() and QDomNode::namespaceURI()
+ return a string for all elements and attributes and return an empty
+ string if the element or attribute has no prefix.
+
+ Text nodes consisting only of whitespace are stripped and won't
+ 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
+
+ \note The overload taking IO \a device will try to open it in read-only
+ mode if it is not already open. In that case, the caller is responsible
+ for calling close. This will change in Qt 7, which will no longer open
+ the IO \a device. Applications should therefore open the device themselves
+ before calling setContent().
+
+ \sa ParseResult, ParseOptions
+*/
+QDomDocument::ParseResult QDomDocument::setContentImpl(const QByteArray &data, ParseOptions options)
+{
+ QXmlStreamReader reader(data);
+ reader.setNamespaceProcessing(options.testFlag(ParseOption::UseNamespaceProcessing));
+ return setContent(&reader, options);
+}
+
+QDomDocument::ParseResult QDomDocument::setContent(QAnyStringView data, ParseOptions options)
+{
+ QXmlStreamReader reader(data);
+ reader.setNamespaceProcessing(options.testFlag(ParseOption::UseNamespaceProcessing));
+ return setContent(&reader, options);
+}
+
+QDomDocument::ParseResult QDomDocument::setContent(QIODevice *device, ParseOptions options)
+{
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+ if (!device->isOpen()) {
+ qWarning("QDomDocument called with unopened QIODevice. "
+ "This will not be supported in future Qt versions.");
+ if (!device->open(QIODevice::ReadOnly)) {
+ const auto error = u"QDomDocument::setContent: Failed to open device."_s;
+ qWarning("%s", qPrintable(error));
+ return { error };
+ }
+ }
+#endif
+
+ QXmlStreamReader reader(device);
+ reader.setNamespaceProcessing(options.testFlag(ParseOption::UseNamespaceProcessing));
+ return setContent(&reader, options);
+}
+
+QDomDocument::ParseResult QDomDocument::setContent(QXmlStreamReader *reader, ParseOptions options)
{
if (!impl)
impl = new QDomDocumentPrivate();
- return IMPL->setContent(reader, namespaceProcessing, errorMsg, errorLine, errorColumn);
+ return IMPL->setContent(reader, options);
}
/*!
@@ -6631,7 +6805,7 @@ QDomElement QDomDocument::elementById(const QString& /*elementId*/)
QDomAttr QDomNode::toAttr() const
{
if (impl && impl->isAttr())
- return QDomAttr(((QDomAttrPrivate*)impl));
+ return QDomAttr(static_cast<QDomAttrPrivate *>(impl));
return QDomAttr();
}
@@ -6644,7 +6818,7 @@ QDomAttr QDomNode::toAttr() const
QDomCDATASection QDomNode::toCDATASection() const
{
if (impl && impl->isCDATASection())
- return QDomCDATASection(((QDomCDATASectionPrivate*)impl));
+ return QDomCDATASection(static_cast<QDomCDATASectionPrivate *>(impl));
return QDomCDATASection();
}
@@ -6657,7 +6831,7 @@ QDomCDATASection QDomNode::toCDATASection() const
QDomDocumentFragment QDomNode::toDocumentFragment() const
{
if (impl && impl->isDocumentFragment())
- return QDomDocumentFragment(((QDomDocumentFragmentPrivate*)impl));
+ return QDomDocumentFragment(static_cast<QDomDocumentFragmentPrivate *>(impl));
return QDomDocumentFragment();
}
@@ -6670,7 +6844,7 @@ QDomDocumentFragment QDomNode::toDocumentFragment() const
QDomDocument QDomNode::toDocument() const
{
if (impl && impl->isDocument())
- return QDomDocument(((QDomDocumentPrivate*)impl));
+ return QDomDocument(static_cast<QDomDocumentPrivate *>(impl));
return QDomDocument();
}
@@ -6683,7 +6857,7 @@ QDomDocument QDomNode::toDocument() const
QDomDocumentType QDomNode::toDocumentType() const
{
if (impl && impl->isDocumentType())
- return QDomDocumentType(((QDomDocumentTypePrivate*)impl));
+ return QDomDocumentType(static_cast<QDomDocumentTypePrivate *>(impl));
return QDomDocumentType();
}
@@ -6696,7 +6870,7 @@ QDomDocumentType QDomNode::toDocumentType() const
QDomElement QDomNode::toElement() const
{
if (impl && impl->isElement())
- return QDomElement(((QDomElementPrivate*)impl));
+ return QDomElement(static_cast<QDomElementPrivate *>(impl));
return QDomElement();
}
@@ -6709,7 +6883,7 @@ QDomElement QDomNode::toElement() const
QDomEntityReference QDomNode::toEntityReference() const
{
if (impl && impl->isEntityReference())
- return QDomEntityReference(((QDomEntityReferencePrivate*)impl));
+ return QDomEntityReference(static_cast<QDomEntityReferencePrivate *>(impl));
return QDomEntityReference();
}
@@ -6722,7 +6896,7 @@ QDomEntityReference QDomNode::toEntityReference() const
QDomText QDomNode::toText() const
{
if (impl && impl->isText())
- return QDomText(((QDomTextPrivate*)impl));
+ return QDomText(static_cast<QDomTextPrivate *>(impl));
return QDomText();
}
@@ -6735,7 +6909,7 @@ QDomText QDomNode::toText() const
QDomEntity QDomNode::toEntity() const
{
if (impl && impl->isEntity())
- return QDomEntity(((QDomEntityPrivate*)impl));
+ return QDomEntity(static_cast<QDomEntityPrivate *>(impl));
return QDomEntity();
}
@@ -6748,7 +6922,7 @@ QDomEntity QDomNode::toEntity() const
QDomNotation QDomNode::toNotation() const
{
if (impl && impl->isNotation())
- return QDomNotation(((QDomNotationPrivate*)impl));
+ return QDomNotation(static_cast<QDomNotationPrivate *>(impl));
return QDomNotation();
}
@@ -6761,7 +6935,7 @@ QDomNotation QDomNode::toNotation() const
QDomProcessingInstruction QDomNode::toProcessingInstruction() const
{
if (impl && impl->isProcessingInstruction())
- return QDomProcessingInstruction(((QDomProcessingInstructionPrivate*)impl));
+ return QDomProcessingInstruction(static_cast<QDomProcessingInstructionPrivate *>(impl));
return QDomProcessingInstruction();
}
@@ -6774,7 +6948,7 @@ QDomProcessingInstruction QDomNode::toProcessingInstruction() const
QDomCharacterData QDomNode::toCharacterData() const
{
if (impl && impl->isCharacterData())
- return QDomCharacterData(((QDomCharacterDataPrivate*)impl));
+ return QDomCharacterData(static_cast<QDomCharacterDataPrivate *>(impl));
return QDomCharacterData();
}
@@ -6787,10 +6961,16 @@ QDomCharacterData QDomNode::toCharacterData() const
QDomComment QDomNode::toComment() const
{
if (impl && impl->isComment())
- return QDomComment(((QDomCommentPrivate*)impl));
+ return QDomComment(static_cast<QDomCommentPrivate *>(impl));
return QDomComment();
}
+/*!
+ \variable QDomNode::impl
+ \internal
+ Pointer to private data structure.
+*/
+
QT_END_NAMESPACE
#endif // QT_NO_DOM