Merge pull request #14 from Ede123/namespaces
Remove unused XML namespace declarations
This commit is contained in:
commit
46a2ada361
1 changed files with 25 additions and 10 deletions
|
|
@ -2877,6 +2877,10 @@ def scourString(in_string, options=None):
|
||||||
global numBytesSavedInTransforms
|
global numBytesSavedInTransforms
|
||||||
doc = xml.dom.minidom.parseString(in_string)
|
doc = xml.dom.minidom.parseString(in_string)
|
||||||
|
|
||||||
|
# remove <metadata> if the user wants to
|
||||||
|
if options.remove_metadata:
|
||||||
|
removeMetadataElements(doc)
|
||||||
|
|
||||||
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
|
# for whatever reason this does not always remove all inkscape/sodipodi attributes/elements
|
||||||
# on the first pass, so we do it multiple times
|
# on the first pass, so we do it multiple times
|
||||||
# does it have to do with removal of children affecting the childlist?
|
# does it have to do with removal of children affecting the childlist?
|
||||||
|
|
@ -2903,7 +2907,17 @@ def scourString(in_string, options=None):
|
||||||
doc.documentElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
|
doc.documentElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
|
||||||
# TODO: throw error or warning?
|
# 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
|
attrList = doc.documentElement.attributes
|
||||||
xmlnsDeclsToRemove = []
|
xmlnsDeclsToRemove = []
|
||||||
redundantPrefixes = []
|
redundantPrefixes = []
|
||||||
|
|
@ -2911,12 +2925,16 @@ def scourString(in_string, options=None):
|
||||||
attr = attrList.item(i)
|
attr = attrList.item(i)
|
||||||
name = attr.nodeName
|
name = attr.nodeName
|
||||||
val = attr.nodeValue
|
val = attr.nodeValue
|
||||||
if name[0:6] == 'xmlns:' and val == 'http://www.w3.org/2000/svg':
|
if name[0:6] == 'xmlns:':
|
||||||
|
if val == 'http://www.w3.org/2000/svg':
|
||||||
redundantPrefixes.append(name[6:])
|
redundantPrefixes.append(name[6:])
|
||||||
xmlnsDeclsToRemove.append(name)
|
xmlnsDeclsToRemove.append(name)
|
||||||
|
elif xmlnsUnused(name[6:], val):
|
||||||
|
xmlnsDeclsToRemove.append(name)
|
||||||
|
|
||||||
for attrName in xmlnsDeclsToRemove:
|
for attrName in xmlnsDeclsToRemove:
|
||||||
doc.documentElement.removeAttribute(attrName)
|
doc.documentElement.removeAttribute(attrName)
|
||||||
|
numAttrsRemoved += 1
|
||||||
|
|
||||||
for prefix in redundantPrefixes:
|
for prefix in redundantPrefixes:
|
||||||
remapNamespacePrefix(doc.documentElement, prefix, '')
|
remapNamespacePrefix(doc.documentElement, prefix, '')
|
||||||
|
|
@ -2926,6 +2944,7 @@ def scourString(in_string, options=None):
|
||||||
|
|
||||||
if options.strip_xml_space_attribute and doc.documentElement.hasAttribute('xml:space'):
|
if options.strip_xml_space_attribute and doc.documentElement.hasAttribute('xml:space'):
|
||||||
doc.documentElement.removeAttribute('xml:space')
|
doc.documentElement.removeAttribute('xml:space')
|
||||||
|
numAttrsRemoved += 1
|
||||||
|
|
||||||
# repair style (remove unnecessary style properties and change them into XML attributes)
|
# repair style (remove unnecessary style properties and change them into XML attributes)
|
||||||
numStylePropsFixed = repairStyle(doc.documentElement, options)
|
numStylePropsFixed = repairStyle(doc.documentElement, options)
|
||||||
|
|
@ -2934,10 +2953,6 @@ def scourString(in_string, options=None):
|
||||||
if options.simple_colors:
|
if options.simple_colors:
|
||||||
numBytesSavedInColors = convertColors(doc.documentElement)
|
numBytesSavedInColors = convertColors(doc.documentElement)
|
||||||
|
|
||||||
# remove <metadata> if the user wants to
|
|
||||||
if options.remove_metadata:
|
|
||||||
removeMetadataElements(doc)
|
|
||||||
|
|
||||||
# remove unreferenced gradients/patterns outside of defs
|
# remove unreferenced gradients/patterns outside of defs
|
||||||
# and most unreferenced elements inside of defs
|
# and most unreferenced elements inside of defs
|
||||||
while removeUnreferencedElements(doc, options.keep_defs) > 0:
|
while removeUnreferencedElements(doc, options.keep_defs) > 0:
|
||||||
|
|
@ -3286,8 +3301,8 @@ def start(options, input, output):
|
||||||
|
|
||||||
# GZ: not using globals would be good too
|
# GZ: not using globals would be good too
|
||||||
if not options.quiet:
|
if not options.quiet:
|
||||||
print(' File:', input.name, \
|
print(' File:', input.name, os.linesep + \
|
||||||
os.linesep + ' Time taken:', str(end-start) + 's' + os.linesep, \
|
' Time taken:', str(end-start) + 's', os.linesep + \
|
||||||
getReport(), file=sys.stderr)
|
getReport(), file=sys.stderr)
|
||||||
|
|
||||||
oldsize = len(in_string)
|
oldsize = len(in_string)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue