From b979fe19e563a559d03f15da818f95ddff00bc56 Mon Sep 17 00:00:00 2001 From: Eduard Braun Date: Tue, 17 Nov 2015 22:30:23 +0100 Subject: [PATCH] Remove unused XML namespace declarations --- scour/scour.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 978d45d..010f686 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -2903,7 +2903,17 @@ def scourString(in_string, options=None): doc.documentElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg') # TODO: throw error or warning? - # check for redundant SVG namespace declaration + # check for redundant and unused SVG namespace declarations + def xmlnsUnused(prefix, namespace): + if doc.getElementsByTagNameNS(namespace, "*"): + return False + else: + for element in doc.getElementsByTagName("*"): + for attrName in six.iterkeys(element.attributes): + if attrName.startswith(prefix): + return False + return True + attrList = doc.documentElement.attributes xmlnsDeclsToRemove = [] redundantPrefixes = [] @@ -2911,9 +2921,12 @@ def scourString(in_string, options=None): attr = attrList.item(i) name = attr.nodeName val = attr.nodeValue - if name[0:6] == 'xmlns:' and val == 'http://www.w3.org/2000/svg': - redundantPrefixes.append(name[6:]) - xmlnsDeclsToRemove.append(name) + if name[0:6] == 'xmlns:': + if val == 'http://www.w3.org/2000/svg': + redundantPrefixes.append(name[6:]) + xmlnsDeclsToRemove.append(name) + elif xmlnsUnused(name[6:], val): + xmlnsDeclsToRemove.append(name) for attrName in xmlnsDeclsToRemove: doc.documentElement.removeAttribute(attrName)