removeDuplicateGradients: Refactor how duplicates are passed around

This commit is mostly to enable the following commit to make
improvements.  It does reduce the number of duplicate getAttribute
calls by a tiny bit but it is unlikely to matter in practice.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2020-05-19 18:02:25 +00:00
parent ace24df5c3
commit 9e3a5f2e40
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC

View file

@ -1536,7 +1536,7 @@ def removeDuplicateGradients(doc):
global _num_elements_removed global _num_elements_removed
num = 0 num = 0
gradientsToRemove = {} gradients_to_remove = []
for gradType in ['linearGradient', 'radialGradient']: for gradType in ['linearGradient', 'radialGradient']:
grads = doc.getElementsByTagName(gradType) grads = doc.getElementsByTagName(gradType)
@ -1553,35 +1553,35 @@ def removeDuplicateGradients(doc):
continue continue
master = bucket[0] master = bucket[0]
duplicates = bucket[1:] duplicates = bucket[1:]
duplicates_ids = [d.getAttribute('id') for d in duplicates]
master_id = master.getAttribute('id') master_id = master.getAttribute('id')
if not master_id: if not master_id:
# If our selected "master" copy does not have an ID, # If our selected "master" copy does not have an ID,
# then replace it with one that does (assuming any of # then replace it with one that does (assuming any of
# them has one). This avoids broken images like we # them has one). This avoids broken images like we
# saw in GH#203 # saw in GH#203
for i in range(len(duplicates)): for i in range(len(duplicates_ids)):
dup = duplicates[i] dup_id = duplicates_ids[i]
dup_id = dup.getAttribute('id')
if dup_id: if dup_id:
# We do not bother updating the master field
# as it is not used any more.
master_id = duplicates_ids[i]
duplicates[i] = master duplicates[i] = master
master = dup # Clear the old id to avoid a redundant remapping
duplicates_ids[i] = ""
break break
gradientsToRemove[master] = duplicates gradients_to_remove.append((master_id, duplicates_ids, duplicates))
# get a collection of all elements that are referenced and their referencing elements # get a collection of all elements that are referenced and their referencing elements
referencedIDs = findReferencedElements(doc.documentElement) referencedIDs = findReferencedElements(doc.documentElement)
for masterGrad in gradientsToRemove: for master_id, duplicates_ids, duplicates in gradients_to_remove:
master_id = masterGrad.getAttribute('id') for dup_id, dupGrad in zip(duplicates_ids, duplicates):
for dupGrad in gradientsToRemove[masterGrad]:
# if the duplicate gradient no longer has a parent that means it was # if the duplicate gradient no longer has a parent that means it was
# already re-mapped to another master gradient # already re-mapped to another master gradient
if not dupGrad.parentNode: if not dupGrad.parentNode:
continue continue
# for each element that referenced the gradient we are going to replace dup_id with master_id
dup_id = dupGrad.getAttribute('id')
# With --keep-unreferenced-defs, we can end up with # With --keep-unreferenced-defs, we can end up with
# unreferenced gradients. See GH#156. # unreferenced gradients. See GH#156.
if dup_id in referencedIDs: if dup_id in referencedIDs: