Fix removing unused chained references. Added another test case.
This commit is contained in:
parent
2aaef4ebbd
commit
7864455c13
2 changed files with 13 additions and 3 deletions
8
scour.py
8
scour.py
|
|
@ -105,6 +105,8 @@ def findElementsWithId(node,elems={}):
|
||||||
# returns the number of times an id is referenced
|
# returns the number of times an id is referenced
|
||||||
# currently looks at fill, stroke and xlink:href attributes
|
# currently looks at fill, stroke and xlink:href attributes
|
||||||
def findReferencedElements(node,ids={}):
|
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')
|
href = node.getAttributeNS(XLINKNS,'href')
|
||||||
|
|
||||||
# if xlink:href is set, then grab the id
|
# if xlink:href is set, then grab the id
|
||||||
|
|
@ -169,14 +171,14 @@ def vacuumDefs(doc):
|
||||||
for elem in aDef.childNodes:
|
for elem in aDef.childNodes:
|
||||||
if( elem.nodeType == 1 and elem.getAttribute('id') == '' ):
|
if( elem.nodeType == 1 and elem.getAttribute('id') == '' ):
|
||||||
aDef.removeChild(elem)
|
aDef.removeChild(elem)
|
||||||
numElemsRemoved +=
|
numElemsRemoved += 1
|
||||||
num += 1
|
num += 1
|
||||||
return num
|
return num
|
||||||
|
|
||||||
bContinueLooping = True
|
bContinueLooping = True
|
||||||
while bContinueLooping:
|
while bContinueLooping:
|
||||||
identifiedElements = findElementsWithId(doc.documentElement)
|
identifiedElements = findElementsWithId(doc.documentElement, {})
|
||||||
referencedIDs = findReferencedElements(doc.documentElement)
|
referencedIDs = findReferencedElements(doc.documentElement, {})
|
||||||
bContinueLooping = ((removeUnreferencedIDs(referencedIDs, identifiedElements) + vacuumDefs(doc)) > 0)
|
bContinueLooping = ((removeUnreferencedIDs(referencedIDs, identifiedElements) + vacuumDefs(doc)) > 0)
|
||||||
|
|
||||||
# output the document
|
# output the document
|
||||||
|
|
|
||||||
8
tests/chained.svg
Normal file
8
tests/chained.svg
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="g1" />
|
||||||
|
<linearGradient id="g2" xlink:href="#g1" />
|
||||||
|
<linearGradient id="g3" xlink:href="#g2" />
|
||||||
|
</defs>
|
||||||
|
<circle cx="50" cy="50" r="20" fill='blue' />
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 274 B |
Loading…
Add table
Add a link
Reference in a new issue