From 7864455c13b35f4b5e9630f6cd0f5da60d290717 Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Sat, 4 Apr 2009 10:48:49 -0500 Subject: [PATCH] Fix removing unused chained references. Added another test case. --- scour.py | 8 +++++--- tests/chained.svg | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 tests/chained.svg diff --git a/scour.py b/scour.py index ca90f0c..1e846b9 100755 --- a/scour.py +++ b/scour.py @@ -105,6 +105,8 @@ def findElementsWithId(node,elems={}): # returns the number of times an id is referenced # currently looks at fill, stroke and xlink:href attributes def findReferencedElements(node,ids={}): + # TODO: error here (ids is not cleared upon next invocation), the + # input argument ids is clunky here (see below how it is called) href = node.getAttributeNS(XLINKNS,'href') # if xlink:href is set, then grab the id @@ -169,14 +171,14 @@ def vacuumDefs(doc): for elem in aDef.childNodes: if( elem.nodeType == 1 and elem.getAttribute('id') == '' ): aDef.removeChild(elem) - numElemsRemoved += + numElemsRemoved += 1 num += 1 return num bContinueLooping = True while bContinueLooping: - identifiedElements = findElementsWithId(doc.documentElement) - referencedIDs = findReferencedElements(doc.documentElement) + identifiedElements = findElementsWithId(doc.documentElement, {}) + referencedIDs = findReferencedElements(doc.documentElement, {}) bContinueLooping = ((removeUnreferencedIDs(referencedIDs, identifiedElements) + vacuumDefs(doc)) > 0) # output the document diff --git a/tests/chained.svg b/tests/chained.svg new file mode 100644 index 0000000..461d323 --- /dev/null +++ b/tests/chained.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file