From 58ddeb855b510f88a1be02a108a48a440866450b Mon Sep 17 00:00:00 2001 From: Louis Simard Date: Thu, 3 May 2012 15:33:11 -0400 Subject: [PATCH] Fix bug 833666, "scour does not clean comments if file starts with a comment", by correctly and efficiently iterating over the elements of a live sequence. Unit tests are updated. --- scour.py | 8 ++++++-- unittests/comment-beside-xml-decl.svg | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scour.py b/scour.py index 9b40e99..bb474cd 100755 --- a/scour.py +++ b/scour.py @@ -2575,7 +2575,9 @@ def removeComments(element) : if isinstance(element, xml.dom.minidom.Document): # must process the document object separately, because its # documentElement's nodes have None as their parentNode - for subelement in element.childNodes: + # iterate in reverse order to prevent mess-ups with renumbering + for index in xrange(len(element.childNodes) - 1, -1, -1): + subelement = element.childNodes[index] if isinstance(subelement, xml.dom.minidom.Comment): numCommentBytes += len(subelement.data) element.removeChild(subelement) @@ -2585,7 +2587,9 @@ def removeComments(element) : numCommentBytes += len(element.data) element.parentNode.removeChild(element) else: - for subelement in element.childNodes: + # iterate in reverse order to prevent mess-ups with renumbering + for index in xrange(len(element.childNodes) - 1, -1, -1): + subelement = element.childNodes[index] removeComments(subelement) def embedRasters(element, options) : diff --git a/unittests/comment-beside-xml-decl.svg b/unittests/comment-beside-xml-decl.svg index 86e6413..cd3ecff 100644 --- a/unittests/comment-beside-xml-decl.svg +++ b/unittests/comment-beside-xml-decl.svg @@ -1,4 +1,10 @@ + + + +