scour: Fix another variant of the crash from #260 (#264)

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2020-11-22 14:00:43 +00:00 committed by GitHub
parent 92e64f0d7f
commit b8a071f995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 30 deletions

View file

@ -1200,11 +1200,11 @@ def mergeSiblingGroupsWithCommonAttributes(elem):
for node in nodes: for node in nodes:
if node.nodeType == Node.ELEMENT_NODE and node.nodeName == 'g' and node.namespaceURI == NS['SVG']: if node.nodeType == Node.ELEMENT_NODE and node.nodeName == 'g' and node.namespaceURI == NS['SVG']:
# Merge # Merge
primaryGroup.childNodes.extend(node.childNodes) for child in node.childNodes[:]:
node.childNodes = [] primaryGroup.appendChild(child)
elem.removeChild(node).unlink()
else: else:
primaryGroup.childNodes.append(node) primaryGroup.appendChild(node)
elem.childNodes.remove(node)
# each child gets the same treatment, recursively # each child gets the same treatment, recursively
for childNode in elem.childNodes: for childNode in elem.childNodes:

View file

@ -2099,6 +2099,12 @@ class GroupSiblingMerge(unittest.TestCase):
self.assertEqual(doc.getElementsByTagName('g').length, 8, self.assertEqual(doc.getElementsByTagName('g').length, 8,
'Sibling merging is disabled by --disable-group-collapsing') 'Sibling merging is disabled by --disable-group-collapsing')
def test_sibling_merge_crash(self):
doc = scourXmlFile('unittests/group-sibling-merge-crash.svg',
parse_args(['']))
self.assertEqual(doc.getElementsByTagName('g').length, 1,
'Sibling merge should work without causing crashes')
class GroupCreation(unittest.TestCase): class GroupCreation(unittest.TestCase):

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" viewBox="0 0 141.732 141.732" xml:space="preserve">
<g>
<g clip-path="url(#SVGID_2_)">
<path d="M1,1" fill="#fdebc8" stroke="#000" stroke-width=".5" stroke-miterlimit="10"/>
</g>
<g clip-path="url(#SVGID_2_)">
<g>
<path opacity=".5" clip-path="url(#SVGID_4_)" fill="#fff" d="M1,1"/>
</g>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 528 B