Fix removal of namespaced elements and attributes. Started array of SVG attributes.

This commit is contained in:
JSCHILL1 2009-04-07 20:47:33 -05:00
parent b44c5d0903
commit 2dc788aa3f

View file

@ -53,6 +53,31 @@ unwanted_ns = [ NS['SODIPODI'], NS['INKSCAPE'], NS['ADOBE_ILLUSTRATOR'],
NS['ADOBE_GRAPHS'], NS['ADOBE_SVG_VIEWER'], NS['ADOBE_VARIABLES'], NS['ADOBE_GRAPHS'], NS['ADOBE_SVG_VIEWER'], NS['ADOBE_VARIABLES'],
NS['ADOBE_SFW'], NS['ADOBE_EXTENSIBILITY'] ] NS['ADOBE_SFW'], NS['ADOBE_EXTENSIBILITY'] ]
svgAttributes = [
'clip-rule',
'fill',
'fill-opacity',
'fill-rule',
'filter',
'font-family',
'font-size',
'font-stretch',
'font-style',
'font-variant',
'font-weight',
'line-height',
'opacity',
'stop-color',
'stop-opacity',
'stroke',
'stroke-dashoffset',
'stroke-linecap',
'stroke-linejoin',
'stroke-miterlimit',
'stroke-opacity',
'stroke-width',
]
def printHeader(): def printHeader():
print APP , VER print APP , VER
print COPYRIGHT print COPYRIGHT
@ -156,12 +181,15 @@ def removeNamespacedAttributes(node, namespaces):
if node.nodeType == 1 : if node.nodeType == 1 :
# remove all namespace'd attributes from this element # remove all namespace'd attributes from this element
attrList = node.attributes attrList = node.attributes
attrsToRemove = []
for attrNum in range(attrList.length): for attrNum in range(attrList.length):
attr = attrList.item(attrNum) attr = attrList.item(attrNum)
if attr != None and attr.namespaceURI in namespaces: if attr != None and attr.namespaceURI in namespaces:
num += 1 attrsToRemove.append(attr.nodeName)
numAttrsRemoved += 1 for attrName in attrsToRemove :
node.removeAttribute(attr.nodeName) num += 1
numAttrsRemoved += 1
node.removeAttribute(attrName)
# now recurse for children # now recurse for children
for child in node.childNodes: for child in node.childNodes:
@ -174,11 +202,14 @@ def removeNamespacedElements(node, namespaces):
if node.nodeType == 1 : if node.nodeType == 1 :
# remove all namespace'd child nodes from this element # remove all namespace'd child nodes from this element
childList = node.childNodes childList = node.childNodes
childrenToRemove = []
for child in childList: for child in childList:
if child != None and child.namespaceURI in namespaces: if child != None and child.namespaceURI in namespaces:
num += 1 childrenToRemove.append(child)
numElemsRemoved += 1 for child in childrenToRemove :
node.removeChild(child) num += 1
numElemsRemoved += 1
node.removeChild(child)
# now recurse for children # now recurse for children
for child in node.childNodes: for child in node.childNodes:
@ -249,6 +280,10 @@ def repairStyle(node):
return num return num
# does nothing at the moment but waste time
def cleanPath(element) :
path = element.getAttribute('d')
# parse command-line arguments # parse command-line arguments
args = sys.argv[1:] args = sys.argv[1:]
@ -332,6 +367,10 @@ for tag in ['defs', 'metadata', 'g'] :
elem.parentNode.removeChild(elem) elem.parentNode.removeChild(elem)
numElemsRemoved += 1 numElemsRemoved += 1
# clean path data
for elem in doc.documentElement.getElementsByTagNameNS(NS['SVG'], 'path') :
cleanPath(elem)
# output the document # output the document
doc.documentElement.writexml(output) doc.documentElement.writexml(output)