diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 2ac044004e0..b25cdf487fc 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -649,7 +649,7 @@ bool QDomNodeListPrivate::operator!=(const QDomNodeListPrivate &other) const return !operator==(other); } -void QDomNodeListPrivate::createList() +void QDomNodeListPrivate::createList() const { if (!node_impl) return; @@ -703,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); @@ -720,15 +725,9 @@ 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(this); - that->createList(); - } - return list.size(); } diff --git a/src/xml/dom/qdom_p.h b/src/xml/dom/qdom_p.h index fb71f8ce236..b2ecd534c88 100644 --- a/src/xml/dom/qdom_p.h +++ b/src/xml/dom/qdom_p.h @@ -142,7 +142,8 @@ public: bool operator==(const QDomNodeListPrivate &) const; bool operator!=(const QDomNodeListPrivate &) const; - void createList(); + void createList() const; + bool maybeCreateList() const; QDomNodePrivate *item(int index); int length() const; @@ -153,8 +154,8 @@ public: QDomNodePrivate *node_impl; QString tagname; QString nsURI; - QList list; - long timestamp; + mutable QList list; + mutable long timestamp; }; class QDomNamedNodeMapPrivate