Simplify and fix "removeComments()"

* The separate treatment of comments at the documentElement's level is not necessary - they have a parent (as tested in Python 3.5.0 and 2.7.11 and 2.6.6)! It might not have worked before due to a typo - note the "if isinstance(element,...)" and "len(element.data)" which should both refer to "subelement" instead - or a bug in very old versions of Python).
* Fix the iteration over childNodes (i.e. replace "for subelement in element.childNodes:" with ""for subelement in element.childNodes[:]:". We have to create a copy of the list to iterate over, otherwise we'd be iterating over a list as we change it which leads to unpredictable results.

Fixes #25
This commit is contained in:
Eduard Braun 2015-12-10 22:50:31 +01:00
parent 1a6ff29c14
commit c698522c28

View file

@ -2587,20 +2587,11 @@ def removeComments(element) :
""" """
global numCommentBytes global numCommentBytes
if isinstance(element, xml.dom.minidom.Document): if isinstance(element, xml.dom.minidom.Comment):
# must process the document object separately, because its
# documentElement's nodes have None as their parentNode
for subelement in element.childNodes:
if isinstance(element, xml.dom.minidom.Comment):
numCommentBytes += len(element.data)
element.documentElement.removeChild(subelement)
else:
removeComments(subelement)
elif isinstance(element, xml.dom.minidom.Comment):
numCommentBytes += len(element.data) numCommentBytes += len(element.data)
element.parentNode.removeChild(element) element.parentNode.removeChild(element)
else: else:
for subelement in element.childNodes: for subelement in element.childNodes[:]:
removeComments(subelement) removeComments(subelement)