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

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 209 KiB

After

Width:  |  Height:  |  Size: 227 KiB

Before After
Before After

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
SCOURVER="0.20" SCOURVER="0.21"
cd .. cd ..
zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/yocto_css.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/yocto_css.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html
cd scour cd scour

View file

@ -9,6 +9,16 @@
<p>Copyright 2009, Jeff Schiller</p> <p>Copyright 2009, Jeff Schiller</p>
<section id="0.21">
<header>
<h2><a href="#0.21">Version 0.21</a></h2>
</header>
<p>TBD</p>
<ul>
<li>Fix <a href="https://bugs.launchpad.net/scour/+bug/427309">Bug 427309</a> by updated Scour inkscape extension file to include yocto_css.py</li>
</ul>
</section>
<section id="0.20"> <section id="0.20">
<header> <header>
<h2><a href="#0.20">Version 0.20</a></h2> <h2><a href="#0.20">Version 0.20</a></h2>

View file

@ -36,6 +36,9 @@
# Next Up: # Next Up:
# + remove unused attributes in parent elements # + remove unused attributes in parent elements
# + prevent elements from being stripped if they are referenced in a <style> element # + 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 # (for instance, filter, marker, pattern) - need a crude CSS parser
# - add an option to remove ids if they match the Inkscape-style of IDs # - add an option to remove ids if they match the Inkscape-style of IDs
# - investigate point-reducing algorithms # - investigate point-reducing algorithms
@ -66,7 +69,7 @@ except ImportError:
Decimal = FixedPoint Decimal = FixedPoint
APP = 'scour' APP = 'scour'
VER = '0.20' VER = '0.21'
COPYRIGHT = 'Copyright Jeff Schiller, 2009' COPYRIGHT = 'Copyright Jeff Schiller, 2009'
NS = { 'SVG': 'http://www.w3.org/2000/svg', NS = { 'SVG': 'http://www.w3.org/2000/svg',
@ -641,7 +644,8 @@ def moveCommonAttributesToParentGroup(elem):
""" """
This recursively calls this function on all children of the passed in element 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 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 num = 0
@ -651,6 +655,10 @@ def moveCommonAttributesToParentGroup(elem):
if child.nodeType == 1: if child.nodeType == 1:
childElements.append(child) childElements.append(child)
num += moveCommonAttributesToParentGroup(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 # only process the children if there are more than one element
if len(childElements) <= 1: return num if len(childElements) <= 1: return num
@ -913,11 +921,14 @@ def removeDuplicateGradients(doc):
referencedIDs = findReferencedElements(doc.documentElement) referencedIDs = findReferencedElements(doc.documentElement)
for masterGrad in gradientsToRemove.keys(): for masterGrad in gradientsToRemove.keys():
master_id = masterGrad.getAttribute('id') master_id = masterGrad.getAttribute('id')
# print 'master='+master_id
for dupGrad in gradientsToRemove[masterGrad]: for dupGrad in gradientsToRemove[masterGrad]:
# if the duplicate gradient no longer has a parent that means it was # if the duplicate gradient no longer has a parent that means it was
# already re-mapped to another master gradient # already re-mapped to another master gradient
if not dupGrad.parentNode: continue if not dupGrad.parentNode: continue
dup_id = dupGrad.getAttribute('id') 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 each element that referenced the gradient we are going to remove
for elem in referencedIDs[dup_id][1]: for elem in referencedIDs[dup_id][1]:
# find out which attribute referenced the duplicate gradient # find out which attribute referenced the duplicate gradient
@ -2196,12 +2207,6 @@ def scourString(in_string, options=None):
while removeNestedGroups(doc.documentElement) > 0: while removeNestedGroups(doc.documentElement) > 0:
pass pass
# move common attributes to parent group
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
# remove unused attributes from parent
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
while removeDuplicateGradientStops(doc) > 0: while removeDuplicateGradientStops(doc) > 0:
pass pass
@ -2213,6 +2218,12 @@ def scourString(in_string, options=None):
while removeDuplicateGradients(doc) > 0: while removeDuplicateGradients(doc) > 0:
pass pass
# move common attributes to parent group
numAttrsRemoved += moveCommonAttributesToParentGroup(doc.documentElement)
# remove unused attributes from parent
numAttrsRemoved += removeUnusedAttributesOnParent(doc.documentElement)
# clean path data # clean path data
for elem in doc.documentElement.getElementsByTagName('path') : for elem in doc.documentElement.getElementsByTagName('path') :
if elem.getAttribute('d') == '': if elem.getAttribute('d') == '':

View file

@ -891,6 +891,12 @@ class RemoveCommonAttributesFromChild(unittest.TestCase):
self.assertNotEquals( r.getAttribute('fill'), '#0F0', self.assertNotEquals( r.getAttribute('fill'), '#0F0',
'Did not remove common fill attribute from child') 'Did not remove common fill attribute from child')
class DontRemoveCommonAttributesIfParentHasTextNodes(unittest.TestCase):
def runTest(self):
text = scour.scourXmlFile('unittests/move-common-attributes-to-parent.svg').getElementsByTagNameNS(SVGNS, 'text')[0]
self.assertNotEquals( text.getAttribute('font-style'), 'italic',
'Removed common attributes when parent contained text elements')
class PropagateCommonAttributesUp(unittest.TestCase): class PropagateCommonAttributesUp(unittest.TestCase):
def runTest(self): def runTest(self):
g = scour.scourXmlFile('unittests/move-common-attributes-to-grandparent.svg').getElementsByTagNameNS(SVGNS, 'g')[0] g = scour.scourXmlFile('unittests/move-common-attributes-to-grandparent.svg').getElementsByTagNameNS(SVGNS, 'g')[0]

View file

@ -5,4 +5,9 @@
<rect fill="#0F0" width="200" height="100" /> <rect fill="#0F0" width="200" height="100" />
<circle fill="#0F0" stroke="0F0" cx="50" cy="50" r="20" /> <circle fill="#0F0" stroke="0F0" cx="50" cy="50" r="20" />
</g> </g>
<text>Hello
<tspan font-style="italic">World!</tspan>
Goodbye
<tspan font-style="italic">Cruel World!</tspan>
</text>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 317 B

After

Width:  |  Height:  |  Size: 435 B

Before After
Before After