[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: QDomElement pitfal (Was: comment in rc file affects line below)
From: Waldo Bastian <bastian () kde ! org>
Date: 2004-05-11 21:24:59
Message-ID: 200405112325.00180.bastian () kde ! org
[Download RAW message or body]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
(Wasn't there a webpage with common programming mistakes?)
Don't use:
for ( QDomElement e = baseElement.firstChild().toElement(); !e.isNull();
e = e.nextSibling().toElement() )
{
...
But use:
for ( QDomNode n = baseElement.firstChild(); !n.isNull();
n = n.nextSibling() )
{
QDomElement e = n.toElement();
if (e.isNull()) continue;
...
Otherwise your loop aborts on the first non-element ( text or comments for
example ) This makes the removeDOMcomments() calls obsolete.
Patch for kdeui attached.
Cheers,
Waldo
On Tue May 11 2004 20:57, Paulo Moura Guedes wrote:
> Hi,
>
> I noticed that if I have,
>
> <Menu name="help"><text>&Help</text>
> #<Action name="about_app"/>
> <Action name="report_bug"/>
> </Menu>
>
> report bug doesn't show on the help menu but if put report_bug line before
> about_app it just works.
> Is this expected behavior?
>
> P.S. The rc file belongs to the kpart of the application
- --
bastian@kde.org | Novell BrainShare Europe 2004 | bastian@suse.com
bastian@kde.org | 12-18 September, Barcelona, Spain | bastian@suse.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQFAoUSsN4pvrENfboIRAoXOAJ94eaiH6+T4vBWsZe7GggChNpr8IQCfaNJE
r2oZo/oK936NA3x95Nkcgvg=
=UGx5
-----END PGP SIGNATURE-----
["kdeui_qdomelement.patch" (text/x-diff)]
? ::enable
? DEADJOE
? cvs.blame
? cvs.diff
? errors
? kcolordrag.patch
? kmenubar.diff
? kmessagebox.h@see
? ktoolbar.hack.patch
? ktoolbarbutton.diff
Index: kdockwidget.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kdockwidget.cpp,v
retrieving revision 1.158
diff -u -p -r1.158 kdockwidget.cpp
--- kdockwidget.cpp 30 Mar 2004 11:47:25 -0000 1.158
+++ kdockwidget.cpp 11 May 2004 21:21:25 -0000
@@ -2122,11 +2122,11 @@ static QStrList listEntry(QDomElement &b
{
QStrList list;
- QDomElement subel = base.namedItem(tagName).firstChild().toElement();
- while (!subel.isNull()) {
+ for( QDomNode n = base.namedItem(tagName).firstChild(); !n.isNull(); n = \
n.nextSibling() ) + {
+ QDomElement subel = n.toElement();
if (subel.tagName() == subTagName)
list.append(subel.firstChild().toText().data().latin1());
- subel = subel.nextSibling().toElement();
}
return list;
@@ -2274,16 +2274,13 @@ void KDockManager::readConfig(QDomElemen
}
// firstly, recreate all common dockwidgets
- QDomElement childEl = base.firstChild().toElement();
- while (!childEl.isNull() ) {
- KDockWidget *obj = 0;
+ for( QDomNode n = base.firstChild(); !n.isNull(); n = n.nextSibling() )
+ {
+ QDomElement childEl = n.toElement();
+ if (childEl.tagName() != "dock") continue;
- if (childEl.tagName() != "dock") {
- childEl = childEl.nextSibling().toElement();
- continue;
- }
// Read an ordinary dock widget
- obj = getDockWidgetFromName(stringEntry(childEl, "name"));
+ KDockWidget *obj = getDockWidgetFromName(stringEntry(childEl, "name"));
obj->setTabPageLabel(stringEntry(childEl, "tabCaption"));
obj->setToolTipString(stringEntry(childEl, "tabToolTip"));
@@ -2300,13 +2297,14 @@ void KDockManager::readConfig(QDomElemen
KDockWidgetHeader *h = static_cast<KDockWidgetHeader*>(obj->header);
h->setDragEnabled(boolEntry(childEl, "dragEnabled"));
}
-
- childEl = childEl.nextSibling().toElement();
}
// secondly, now iterate again and create the groups and tabwidgets, apply the \
dockwidgets to them
- childEl = base.firstChild().toElement();
- while (!childEl.isNull() ) {
+ for( QDomNode n = base.firstChild(); !n.isNull(); n = n.nextSibling() )
+ {
+ QDomElement childEl = n.toElement();
+ if (childEl.isNull()) continue;
+
KDockWidget *obj = 0;
if (childEl.tagName() == "dockContainer") {
@@ -2367,7 +2365,6 @@ void KDockManager::readConfig(QDomElemen
}
}
} else {
- childEl = childEl.nextSibling().toElement();
continue;
}
@@ -2384,21 +2381,19 @@ void KDockManager::readConfig(QDomElemen
KDockWidgetHeader *h = static_cast<KDockWidgetHeader*>(obj->header);
h->setDragEnabled(boolEntry(childEl, "dragEnabled"));
}
-
- childEl = childEl.nextSibling().toElement();
}
// thirdly, now that all ordinary dockwidgets are created,
// iterate them again and link them with their corresponding dockwidget for the \
dockback action
- childEl = base.firstChild().toElement();
- while (!childEl.isNull() ) {
- KDockWidget *obj = 0;
+ for( QDomNode n = base.firstChild(); !n.isNull(); n = n.nextSibling() )
+ {
+ QDomElement childEl = n.toElement();
- if (childEl.tagName() != "dock" && childEl.tagName() != "tabGroup") {
- childEl = childEl.nextSibling().toElement();
+ if (childEl.tagName() != "dock" && childEl.tagName() != "tabGroup")
continue;
- }
+ KDockWidget *obj = 0;
+
if (!boolEntry(childEl, "hasParent")) {
// Read a common toplevel dock widget
obj = getDockWidgetFromName(stringEntry(childEl, "name"));
@@ -2409,7 +2404,6 @@ void KDockManager::readConfig(QDomElemen
obj->formerDockPos = KDockWidget::DockPosition(numberEntry(childEl, \
"dockBackToPos")); obj->updateHeader();
}
- childEl = childEl.nextSibling().toElement();
}
if (main->inherits("KDockMainWindow")) {
Index: kedittoolbar.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kedittoolbar.cpp,v
retrieving revision 1.93
diff -u -p -r1.93 kedittoolbar.cpp
--- kedittoolbar.cpp 5 May 2004 13:48:09 -0000 1.93
+++ kedittoolbar.cpp 11 May 2004 21:21:25 -0000
@@ -159,19 +159,22 @@ public:
/**
* Return a list of toolbar elements given a toplevel element
*/
- ToolbarList findToolbars(QDomElement elem)
+ ToolbarList findToolbars(QDomNode n)
{
static const QString &tagToolbar = KGlobal::staticQString( "ToolBar" );
static const QString &attrNoEdit = KGlobal::staticQString( "noEdit" );
ToolbarList list;
- for( ; !elem.isNull(); elem = elem.nextSibling().toElement() )
+ for( ; !n.isNull(); n = n.nextSibling() )
{
+ QDomElement elem = n.toElement();
+ if (elem.isNull())
+ continue;
+
if (elem.tagName() == tagToolbar && elem.attribute( attrNoEdit ) != "true" )
list.append(elem);
- QDomElement child = elem.firstChild().toElement();
- list += findToolbars(child);
+ list += findToolbars(elem.firstChild());
}
return list;
@@ -211,9 +214,9 @@ public:
QDomElement findElementForToolbarItem( const ToolbarItem* item ) const
{
static const QString &attrName = KGlobal::staticQString( "name" );
- QDomElement elem = m_currentToolbarElem.firstChild().toElement();
- for( ; !elem.isNull(); elem = elem.nextSibling().toElement())
+ for(QDomNode n = m_currentToolbarElem.firstChild(); !n.isNull(); n = \
n.nextSibling()) {
+ QDomElement elem = n.toElement();
if ((elem.attribute(attrName) == item->internalName()) &&
(elem.tagName() == item->internalTag()))
return elem;
@@ -436,7 +439,6 @@ void KEditToolbarWidget::initNonKPart(KA
local.m_type = XmlData::Local;
local.m_document.setContent(localXML);
elem = local.m_document.documentElement().toElement();
- KXMLGUIFactory::removeDOMComments( elem );
local.m_barList = d->findToolbars(elem);
local.m_actionCollection = collection;
d->m_xmlFiles.append(local);
@@ -485,7 +487,6 @@ void KEditToolbarWidget::initKPart(KXMLG
data.m_type = XmlData::Part;
data.m_document.setContent( KXMLGUIFactory::readConfigFile( client->xmlFile(), \
client->instance() ) ); elem = data.m_document.documentElement().toElement();
- KXMLGUIFactory::removeDOMComments( elem );
data.m_barList = d->findToolbars(elem);
data.m_actionCollection = client->actionCollection();
d->m_xmlFiles.append(data);
Index: kmainwindow.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmainwindow.cpp,v
retrieving revision 1.125
diff -u -p -r1.125 kmainwindow.cpp
--- kmainwindow.cpp 9 May 2004 18:34:21 -0000 1.125
+++ kmainwindow.cpp 11 May 2004 21:21:26 -0000
@@ -483,8 +483,11 @@ void KMainWindow::createGUI( const QStri
QDomDocument doc = domDocument();
- QDomElement e = doc.documentElement().firstChild().toElement();
- for (; !e.isNull(); e = e.nextSibling().toElement() ) {
+ for( QDomNode n = doc.documentElement().firstChild();
+ !n.isNull(); n = n.nextSibling())
+ {
+ QDomElement e = n.toElement();
+
if ( e.tagName().lower() == "toolbar" )
factory_->resetContainer( e.attribute( "name" ) );
else if ( e.tagName().lower() == "menubar" )
Index: ktoolbar.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ktoolbar.cpp,v
retrieving revision 1.380
diff -u -p -r1.380 ktoolbar.cpp
--- ktoolbar.cpp 9 May 2004 21:31:43 -0000 1.380
+++ ktoolbar.cpp 11 May 2004 21:21:27 -0000
@@ -966,15 +966,14 @@ void KToolBar::saveState()
// first, try to save to the xml file
if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() ) {
//kdDebug(220) << name() << " saveState: saving to " << \
d->m_xmlguiClient->xmlFile() << endl;
- // go down one level to get to the right tags
- QDomElement elem = \
d->m_xmlguiClient->domDocument().documentElement().toElement();
- elem = elem.firstChild().toElement();
QString barname(!::qstrcmp(name(), "unnamed") ? "mainToolBar" : name());
- QDomElement current;
- // now try to find our toolbar
+ // try to find our toolbar
d->modified = false;
- for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
- current = elem;
+ // go down one level to get to the right tags
+ QDomElement current;
+ for( QDomNode n = \
d->m_xmlguiClient->domDocument().documentElement().firstChild(); + \
!n.isNull(); n = n.nextSibling()) { + current = n.toElement();
if ( current.tagName().lower() != "toolbar" )
continue;
@@ -997,10 +996,11 @@ void KToolBar::saveState()
// make sure we don't append if this toolbar already exists locally
bool just_append = true;
- elem = local.documentElement().toElement();
- KXMLGUIFactory::removeDOMComments( elem );
- elem = elem.firstChild().toElement();
- for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
+
+ for( QDomNode n = local.documentElement().firstChild();
+ !n.isNull(); n = n.nextSibling()) {
+ QDomElement elem = n.toElement();
+
if ( elem.tagName().lower() != "toolbar" )
continue;
Index: kxmlguiclient.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguiclient.cpp,v
retrieving revision 1.83
diff -u -p -r1.83 kxmlguiclient.cpp
--- kxmlguiclient.cpp 26 Feb 2004 21:14:59 -0000 1.83
+++ kxmlguiclient.cpp 11 May 2004 21:21:28 -0000
@@ -228,7 +228,6 @@ void KXMLGUIClient::setDOMDocument( cons
QDomElement base = d->m_doc.documentElement();
QDomElement e = document.documentElement();
- KXMLGUIFactory::removeDOMComments( e );
// merge our original (global) xml with our new one
mergeXML(base, e, actionCollection());
@@ -244,7 +243,6 @@ void KXMLGUIClient::setDOMDocument( cons
else
{
d->m_doc = document;
- KXMLGUIFactory::removeDOMComments( d->m_doc );
}
setXMLGUIBuildDocument( QDomDocument() );
@@ -277,10 +275,15 @@ bool KXMLGUIClient::mergeXML( QDomElemen
QString tag;
- QDomElement e = base.firstChild().toElement();
// iterate over all elements in the container (of the global DOM tree)
- while ( !e.isNull() )
+ QDomNode n = base.firstChild();
+ while ( !n.isNull() )
{
+ QDomElement e = n.toElement();
+ n = n.nextSibling(); // Advance now so that we can safely delete e
+ if (e.isNull())
+ continue;
+
tag = e.tagName();
// if there's an action tag in the global tree and the action is
@@ -292,9 +295,7 @@ bool KXMLGUIClient::mergeXML( QDomElemen
(kapp && !kapp->authorizeKAction(name)))
{
// remove this child as we aren't using it
- QDomElement oldChild = e;
- e = e.nextSibling().toElement();
- base.removeChild( oldChild );
+ base.removeChild( e );
continue;
}
}
@@ -314,9 +315,7 @@ bool KXMLGUIClient::mergeXML( QDomElemen
( prev.tagName() == tagText ) )
{
// the previous element was a weak separator or didn't exist
- QDomElement oldChild = e;
- e = e.nextSibling().toElement();
- base.removeChild( oldChild );
+ base.removeChild( e );
continue;
}
}
@@ -326,18 +325,13 @@ bool KXMLGUIClient::mergeXML( QDomElemen
// elements we delete this element
else if ( tag == tagMergeLocal )
{
- QDomElement currElement = e;
-
- // switch our iterator "e" to the next sibling, so that we don't
- // process the local tree's inserted items!
- e = e.nextSibling().toElement();
-
- QDomElement it = additive.firstChild().toElement();
+ QDomNode it = additive.firstChild();
while ( !it.isNull() )
{
- QDomElement newChild = it;
-
- it = it.nextSibling().toElement();
+ QDomElement newChild = it.toElement();
+ it = it.nextSibling();
+ if (newChild.isNull() )
+ continue;
if ( newChild.tagName() == tagText )
continue;
@@ -346,7 +340,7 @@ bool KXMLGUIClient::mergeXML( QDomElemen
continue;
QString itAppend( newChild.attribute( attrAppend ) );
- QString elemName( currElement.attribute( attrName ) );
+ QString elemName( e.attribute( attrName ) );
if ( ( itAppend.isNull() && elemName.isEmpty() ) ||
( itAppend == elemName ) )
@@ -356,11 +350,11 @@ bool KXMLGUIClient::mergeXML( QDomElemen
// be merged in, later
QDomElement matchingElement = findMatchingElement( newChild, base );
if ( matchingElement.isNull() || newChild.tagName() == tagSeparator )
- base.insertBefore( newChild, currElement );
+ base.insertBefore( newChild, e );
}
}
- base.removeChild( currElement );
+ base.removeChild( e );
continue;
}
@@ -372,23 +366,17 @@ bool KXMLGUIClient::mergeXML( QDomElemen
{
// handle the text tag
if ( tag == tagText )
- {
- e = e.nextSibling().toElement();
continue;
- }
QDomElement matchingElement = findMatchingElement( e, additive );
- QDomElement currElement = e;
- e = e.nextSibling().toElement();
-
if ( !matchingElement.isNull() )
{
matchingElement.setAttribute( attrAlreadyVisited, (uint)1 );
- if ( mergeXML( currElement, matchingElement, actionCollection ) )
+ if ( mergeXML( e, matchingElement, actionCollection ) )
{
- base.removeChild( currElement );
+ base.removeChild( e );
continue;
}
@@ -397,7 +385,7 @@ bool KXMLGUIClient::mergeXML( QDomElemen
for(uint i = 0; i < attribs.count(); i++)
{
QDomNode node = attribs.item(i);
- currElement.setAttribute(node.nodeName(), node.nodeValue());
+ e.setAttribute(node.nodeName(), node.nodeValue());
}
continue;
@@ -409,31 +397,29 @@ bool KXMLGUIClient::mergeXML( QDomElemen
// this container. However we have to call mergeXML recursively
// and make it check if there are actions implemented for this
// container. *If* none, then we can remove this container now
- if ( mergeXML( currElement, QDomElement(), actionCollection ) )
- base.removeChild( currElement );
+ if ( mergeXML( e, QDomElement(), actionCollection ) )
+ base.removeChild( e );
continue;
}
}
-
- //I think this can be removed ;-)
- e = e.nextSibling().toElement();
}
//here we append all child elements which were not inserted
//previously via the LocalMerge tag
- e = additive.firstChild().toElement();
- while ( !e.isNull() )
+ n = additive.firstChild();
+ while ( !n.isNull() )
{
+ QDomElement e = n.toElement();
+ n = n.nextSibling(); // Advance now so that we can safely delete e
+ if (e.isNull())
+ continue;
+
QDomElement matchingElement = findMatchingElement( e, base );
if ( matchingElement.isNull() )
{
- QDomElement newChild = e;
- e = e.nextSibling().toElement();
- base.appendChild( newChild );
+ base.appendChild( e );
}
- else
- e = e.nextSibling().toElement();
}
// do one quick check to make sure that the last element was not
@@ -441,16 +427,22 @@ bool KXMLGUIClient::mergeXML( QDomElemen
QDomElement last = base.lastChild().toElement();
if ( (last.tagName() == tagSeparator) && (!last.attribute( attrWeakSeparator \
).isNull()) ) {
- base.removeChild( base.lastChild() );
+ base.removeChild( last );
}
// now we check if we are empty (in which case we return "true", to
// indicate the caller that it can delete "us" (the base element
// argument of "this" call)
bool deleteMe = true;
- e = base.firstChild().toElement();
- for ( ; !e.isNull(); e = e.nextSibling().toElement() )
+
+ n = base.firstChild();
+ while ( !n.isNull() )
{
+ QDomElement e = n.toElement();
+ n = n.nextSibling(); // Advance now so that we can safely delete e
+ if (e.isNull())
+ continue;
+
tag = e.tagName();
if ( tag == tagAction )
@@ -511,9 +503,14 @@ QDomElement KXMLGUIClient::findMatchingE
static const QString &tagMergeLocal = KGlobal::staticQString( "MergeLocal" );
static const QString &attrName = KGlobal::staticQString( "name" );
- QDomElement e = additive.firstChild().toElement();
- for ( ; !e.isNull(); e = e.nextSibling().toElement() )
+ QDomNode n = additive.firstChild();
+ while ( !n.isNull() )
{
+ QDomElement e = n.toElement();
+ n = n.nextSibling(); // Advance now so that we can safely delete e
+ if (e.isNull())
+ continue;
+
// skip all action and merge tags as we will never use them
if ( ( e.tagName() == tagAction ) || ( e.tagName() == tagMergeLocal ) )
{
@@ -529,7 +526,7 @@ QDomElement KXMLGUIClient::findMatchingE
}
// nope, return a (now) null element
- return e;
+ return QDomElement();
}
void KXMLGUIClient::conserveMemory()
@@ -796,9 +793,10 @@ KXMLGUIClient::ActionPropertiesMap KXMLG
return properties;
QDomNode n = actionPropElement.firstChild();
- for (; !n.isNull(); n = n.nextSibling() )
+ while(!n.isNull())
{
QDomElement e = n.toElement();
+ n = n.nextSibling(); // Advance now so that we can safely delete e
if ( e.isNull() )
continue;
Index: kxmlguifactory.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory.cpp,v
retrieving revision 1.144
diff -u -p -r1.144 kxmlguifactory.cpp
--- kxmlguifactory.cpp 28 Apr 2004 14:12:07 -0000 1.144
+++ kxmlguifactory.cpp 11 May 2004 21:21:28 -0000
@@ -488,9 +488,10 @@ void KXMLGUIFactory::applyActionProperti
{
static const QString &tagAction = KGlobal::staticQString( "action" );
- QDomElement e = actionPropElement.firstChild().toElement();
- for (; !e.isNull(); e = e.nextSibling().toElement() )
+ for (QDomNode n = actionPropElement.firstChild();
+ !n.isNull(); n = n.nextSibling() )
{
+ QDomElement e = n.toElement();
if ( e.tagName().lower() != tagAction )
continue;
Index: kxmlguifactory_p.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory_p.cpp,v
retrieving revision 1.29
diff -u -p -r1.29 kxmlguifactory_p.cpp
--- kxmlguifactory_p.cpp 3 May 2004 08:59:47 -0000 1.29
+++ kxmlguifactory_p.cpp 11 May 2004 21:21:28 -0000
@@ -367,13 +367,15 @@ QDomElement ContainerNode::findElementFo
{
static const QString &attrName = KGlobal::staticQString( "name" );
- QDomElement e;
// ### slow
- for ( e = baseElement.firstChild().toElement(); !e.isNull();
- e = e.nextSibling().toElement() )
+ for ( QDomNode n = baseElement.firstChild(); !n.isNull();
+ n = n.nextSibling() )
+ {
+ QDomElement e = n.toElement();
if ( e.tagName().lower() == childNode->tagName &&
e.attribute( attrName ) == childNode->name )
return e;
+ }
return QDomElement();
}
@@ -543,9 +545,12 @@ BuildHelper::BuildHelper( BuildState &st
void BuildHelper::build( const QDomElement &element )
{
- QDomElement e = element.firstChild().toElement();
- for (; !e.isNull(); e = e.nextSibling().toElement() )
+ for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling() )
+ {
+ QDomElement e = n.toElement();
+ if (e.isNull()) continue;
processElement( e );
+ }
}
void BuildHelper::processElement( const QDomElement &e )
@@ -633,9 +638,11 @@ void BuildHelper::processStateElement( c
if ( !stateName || !stateName.length() ) return;
- QDomElement e = element.firstChild().toElement();
+ for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling() )
+ {
+ QDomElement e = n.toElement();
+ if (e.isNull()) continue;
- for (; !e.isNull(); e = e.nextSibling().toElement() ) {
QString tagName = e.tagName().lower();
if ( tagName != "enable" && tagName != "disable" )
@@ -644,9 +651,9 @@ void BuildHelper::processStateElement( c
bool processingActionsToEnable = (tagName == "enable");
// process action names
- QDomElement actionEl = e.firstChild().toElement();
-
- for (; !actionEl.isNull(); actionEl = actionEl.nextSibling().toElement() ) {
+ for (QDomNode n2 = element.firstChild(); !n2.isNull(); n2 = n2.nextSibling() \
) + {
+ QDomElement actionEl = n2.toElement();
if ( actionEl.tagName().lower() != "action" ) continue;
QString actionName = actionEl.attribute( "name" );
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic