findReferencedElements: Use a set instead of list for tracking nodes

Except for one caller, nothing cares what kind of collection is used.
By migrating to a set, we can enable a future rewrite.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2020-05-18 20:26:21 +00:00
parent 0e82b8dcad
commit 3d29029c72
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC

View file

@ -556,7 +556,7 @@ def findReferencedElements(node, ids=None):
Returns IDs of all referenced elements Returns IDs of all referenced elements
- node is the node at which to start the search. - node is the node at which to start the search.
- returns a map which has the id as key and - returns a map which has the id as key and
each value is is a list of nodes each value is is a set of nodes
Currently looks at 'xlink:href' and all attributes in 'referencingProps' Currently looks at 'xlink:href' and all attributes in 'referencingProps'
""" """
@ -586,9 +586,9 @@ def findReferencedElements(node, ids=None):
# we remove the hash mark from the beginning of the id # we remove the hash mark from the beginning of the id
id = href[1:] id = href[1:]
if id in ids: if id in ids:
ids[id].append(node) ids[id].add(node)
else: else:
ids[id] = [node] ids[id] = {node}
# now get all style properties and the fill, stroke, filter attributes # now get all style properties and the fill, stroke, filter attributes
styles = node.getAttribute('style').split(';') styles = node.getAttribute('style').split(';')
@ -619,9 +619,9 @@ def findReferencingProperty(node, prop, val, ids):
if len(val) >= 7 and val[0:5] == 'url(#': if len(val) >= 7 and val[0:5] == 'url(#':
id = val[5:val.find(')')] id = val[5:val.find(')')]
if id in ids: if id in ids:
ids[id].append(node) ids[id].add(node)
else: else:
ids[id] = [node] ids[id] = {node}
# if the url has a quote in it, we need to compensate # if the url has a quote in it, we need to compensate
elif len(val) >= 8: elif len(val) >= 8:
id = None id = None
@ -633,9 +633,9 @@ def findReferencingProperty(node, prop, val, ids):
id = val[6:val.find("')")] id = val[6:val.find("')")]
if id is not None: if id is not None:
if id in ids: if id in ids:
ids[id].append(node) ids[id].add(node)
else: else:
ids[id] = [node] ids[id] = {node}
def removeUnusedDefs(doc, defElem, elemsToRemove=None, referencedIDs=None): def removeUnusedDefs(doc, defElem, elemsToRemove=None, referencedIDs=None):
@ -1457,7 +1457,7 @@ def collapseSinglyReferencedGradients(doc):
elem.namespaceURI == NS['SVG'] elem.namespaceURI == NS['SVG']
): ):
# found a gradient that is referenced by only 1 other element # found a gradient that is referenced by only 1 other element
refElem = nodes[0] refElem = nodes.pop()
if refElem.nodeType == Node.ELEMENT_NODE and refElem.nodeName in ['linearGradient', 'radialGradient'] \ if refElem.nodeType == Node.ELEMENT_NODE and refElem.nodeName in ['linearGradient', 'radialGradient'] \
and refElem.namespaceURI == NS['SVG']: and refElem.namespaceURI == NS['SVG']:
# elem is a gradient referenced by only one other gradient (refElem) # elem is a gradient referenced by only one other gradient (refElem)