From 731515f8241592189d4c3b6507b40e4b854c6e0d Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sat, 18 Aug 2018 15:18:18 +0000 Subject: [PATCH] Preserve xhref:href attr when collapsing referenced gradients Closes: #198 Closes: #202 Signed-off-by: Niels Thykier --- scour/scour.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index a46af61..5c380d0 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -1338,8 +1338,16 @@ def collapseSinglyReferencedGradients(doc): if refElem.getAttribute(attr) == '' and not elem.getAttribute(attr) == '': refElem.setAttributeNS(None, attr, elem.getAttribute(attr)) - # now remove the xlink:href from refElem - refElem.removeAttributeNS(NS['XLINK'], 'href') + target_href = elem.getAttributeNS(NS['XLINK'], 'href') + if target_href: + # If the elem node had an xlink:href, then the + # refElem have to point to it as well to + # perserve the semantics of the image. + refElem.setAttributeNS(NS['XLINK'], 'href', target_href) + else: + # The elem node had no xlink:href reference, + # so we can simply remove the attribute. + refElem.removeAttributeNS(NS['XLINK'], 'href') # now delete elem elem.parentNode.removeChild(elem)