This commit is contained in:
Jonathan Neuhauser 2022-09-15 21:41:45 +02:00 committed by GitHub
commit e9e412e683
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View file

@ -646,7 +646,7 @@ def removeUnusedDefs(doc, defElem, elemsToRemove=None, referencedIDs=None):
# removeUnusedDefs do not change the XML itself; therefore there is no point in # removeUnusedDefs do not change the XML itself; therefore there is no point in
# recomputing findReferencedElements when we recurse into child nodes. # recomputing findReferencedElements when we recurse into child nodes.
if referencedIDs is None: if referencedIDs is None:
referencedIDs = findReferencedElements(doc.documentElement) referencedIDs = set(findReferencedElements(doc.documentElement).keys())
keepTags = ['font', 'style', 'metadata', 'script', 'title', 'desc'] keepTags = ['font', 'style', 'metadata', 'script', 'title', 'desc']
for elem in defElem.childNodes: for elem in defElem.childNodes:
@ -660,10 +660,13 @@ def removeUnusedDefs(doc, defElem, elemsToRemove=None, referencedIDs=None):
# we only inspect the children of a group in a defs if the group # we only inspect the children of a group in a defs if the group
# is not referenced anywhere else # is not referenced anywhere else
if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']: if elem.nodeName == 'g' and elem.namespaceURI == NS['SVG']:
elemsToRemove = removeUnusedDefs(doc, elem, elemsToRemove, referencedIDs=referencedIDs) removeUnusedDefs(doc, elem, elemsToRemove, referencedIDs=referencedIDs)
# we only remove if it is not one of our tags we always keep (see above) # we only remove if it is not one of our tags we always keep (see above)
# also we can't remove an element if a child is referenced
elif elem.nodeName not in keepTags: elif elem.nodeName not in keepTags:
elemsToRemove.append(elem) if (set(findElementsWithId(elem).keys()).intersection(referencedIDs) == set() or
elem.tagName == 'defs'):
elemsToRemove.append(elem)
return elemsToRemove return elemsToRemove

View file

@ -379,6 +379,13 @@ class KeepUnreferencedDefs(unittest.TestCase):
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'circle')), 1, self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'circle')), 1,
'Unreferenced circle removed from defs with `--keep-unreferenced-defs`') 'Unreferenced circle removed from defs with `--keep-unreferenced-defs`')
class KeepUnreferencedElementWithReferencedChild(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/referenced_child.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'rect')), 1,
'Referenced element was deleted')
class DoNotRemoveChainedRefsInDefs(unittest.TestCase): class DoNotRemoveChainedRefsInDefs(unittest.TestCase):

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<clipPath id="unusedclip">
<rect id="the_path" width="100" height="100"/>
</clipPath>
</defs>
<use fill="red" width="100%" height="100%" xlink:href="#the_path"/>
</svg>

After

Width:  |  Height:  |  Size: 343 B