Fix removal of common attributes if parent has non-whitespace text nodes

This commit is contained in:
JSCHILL1 2009-09-24 09:54:56 -05:00
parent 473e18500a
commit 7652fbc76c
6 changed files with 3823 additions and 787 deletions

View file

@ -36,6 +36,9 @@
# Next Up:
# + remove unused attributes in parent elements
# + prevent elements from being stripped if they are referenced in a <style> element
# + only move common attributes and remove unused attributes after removing duplicate gradients
# + only move common attributes to parent if the parent contains non-whitespace text nodes
# - TODO: fix the removal of comment elements (between <?xml?> and <svg>)
# (for instance, filter, marker, pattern) - need a crude CSS parser
# - add an option to remove ids if they match the Inkscape-style of IDs
# - investigate point-reducing algorithms
@ -66,7 +69,7 @@ except ImportError:
Decimal = FixedPoint
APP = 'scour'
VER = '0.20'
VER = '0.21'
COPYRIGHT = 'Copyright Jeff Schiller, 2009'
NS = { 'SVG': 'http://www.w3.org/2000/svg',
@ -431,7 +434,7 @@ def findReferencedElements(node, ids=None):
return ids
# else if xlink:href is set, then grab the id
href = node.getAttributeNS(NS['XLINK'],'href')
href = node.getAttributeNS(NS['XLINK'],'href')
if href != '' and len(href) > 1 and href[0] == '#':
# we remove the hash mark from the beginning of the id
id = href[1:]
@ -641,7 +644,8 @@ def moveCommonAttributesToParentGroup(elem):
"""
This recursively calls this function on all children of the passed in element
and then iterates over all child elements and removes common inheritable attributes
from the children and places them in the parent group.
from the children and places them in the parent group. But only if the parent contains
nothing but element children and whitespace.
"""
num = 0
@ -651,6 +655,10 @@ def moveCommonAttributesToParentGroup(elem):
if child.nodeType == 1:
childElements.append(child)
num += moveCommonAttributesToParentGroup(child)
# else if the parent has non-whitespace text children, do not
# try to move common attributes
elif child.nodeType == 3 and child.nodeValue.strip():
return num
# only process the children if there are more than one element
if len(childElements) <= 1: return num
@ -913,11 +921,14 @@ def removeDuplicateGradients(doc):
referencedIDs = findReferencedElements(doc.documentElement)
for masterGrad in gradientsToRemove.keys():
master_id = masterGrad.getAttribute('id')
# print 'master='+master_id
for dupGrad in gradientsToRemove[masterGrad]:
# if the duplicate gradient no longer has a parent that means it was
# already re-mapped to another master gradient
if not dupGrad.parentNode: continue
dup_id = dupGrad.getAttribute('id')
# print 'dup='+dup_id
# print referencedIDs[dup_id]
# for each element that referenced the gradient we are going to remove
for elem in referencedIDs[dup_id][1]:
# find out which attribute referenced the duplicate gradient
@ -2196,12 +2207,6 @@ def scourString(in_string, options=None):
while removeNestedGroups(doc.documentElement) > 0:
pass
# move common attributes to parent group
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
# remove unused attributes from parent
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
while removeDuplicateGradientStops(doc) > 0:
pass
@ -2213,6 +2218,12 @@ def scourString(in_string, options=None):
while removeDuplicateGradients(doc) > 0:
pass
# move common attributes to parent group
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
# remove unused attributes from parent
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
# clean path data
for elem in doc.documentElement.getElementsByTagName('path') :
if elem.getAttribute('d') == '':