Prevent removal of metadata elements if they have only text nodes. Also added some unit tests.

This commit is contained in:
JSCHILL1 2009-04-15 10:18:32 -05:00
parent 7cfb655cff
commit 2cc9c00ef7
7 changed files with 58 additions and 4 deletions

View file

@ -51,7 +51,10 @@
# when being run as main (for unit testing) # when being run as main (for unit testing)
# - Removed duplicate gradient stops # - Removed duplicate gradient stops
# - Convert all colors to #RRGGBB format # - Convert all colors to #RRGGBB format
# - # - prevent metadata from being removed if they contain only text nodes
# - rework command-line argument processing so that options are configurable
# - remove unreferenced patterns? https://bugs.edge.launchpad.net/ubuntu/+source/human-icon-theme/+bug/361667/
# necessary to get true division # necessary to get true division
from __future__ import division from __future__ import division
@ -597,7 +600,7 @@ def scourString(in_string):
removeElem = not elem.hasChildNodes() removeElem = not elem.hasChildNodes()
if removeElem == False : if removeElem == False :
for child in elem.childNodes : for child in elem.childNodes :
if child.nodeType in [1, 4, 8] : if child.nodeType in [1, 3, 4, 8] :
break break
else: else:
removeElem = True removeElem = True

View file

@ -95,6 +95,30 @@ class NoAdobeXPathElements(unittest.TestCase):
lambda e: e.namespaceURI != 'http://ns.adobe.com/XPath/1.0/'), False, lambda e: e.namespaceURI != 'http://ns.adobe.com/XPath/1.0/'), False,
'Found Adobe XPath elements' ) 'Found Adobe XPath elements' )
#class NoInkscapeAttributes(unittest.TestCase):
# def runTest(self):
# self.assertNotEquals(walkTree(scour.scourXmlFile('unittests/inkscape.svg').documentElement,
# lambda e: for a in e.attributes: a.namespaceURI
# False,
# 'Found Inkscape attributes')
class DoNotRemoveMetadataWithOnlyText(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/metadata-with-text.svg')
self.assertEquals(len(doc.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'metadata')), 1,
'Removed metadata element with only text child' )
class RemoveEmptyMetadataElement(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/empty-metadata.svg')
self.assertEquals(len(doc.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'metadata')), 0,
'Did not remove empty metadata element' )
class RemoveEmptyGElements(unittest.TestCase):
def runTest(self):
doc = scour.scourXmlFile('unittests/empty-g.svg')
self.assertEquals(len(doc.getElementsByTagNameNS('http://www.w3.org/2000/svg', 'g')), 1,
'Did not remove empty g element' )
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -7,6 +7,7 @@
xmlns:ir="http://ns.adobe.com/ImageReplacement/1.0/" xmlns:ir="http://ns.adobe.com/ImageReplacement/1.0/"
xmlns:custom="http://ns.adobe.com/GenericCustomNamespace/1.0/" xmlns:custom="http://ns.adobe.com/GenericCustomNamespace/1.0/"
xmlns:xpath="http://ns.adobe.com/XPath/1.0/" xmlns:xpath="http://ns.adobe.com/XPath/1.0/"
xmlns:ok="A.namespace.we.want.left.in"
i:viewOrigin="190.2959 599.1841" i:rulerOrigin="0 0" i:pageBounds="0 792 612 0"> i:viewOrigin="190.2959 599.1841" i:rulerOrigin="0 0" i:pageBounds="0 792 612 0">
<x:foo>bar</x:foo> <x:foo>bar</x:foo>
<i:foo>bar</i:foo> <i:foo>bar</i:foo>
@ -26,4 +27,18 @@
<slices/> <slices/>
<sliceSourceBounds y="191.664" x="190.296" width="225.72" height="407.52" bottomLeftOrigin="true"/> <sliceSourceBounds y="191.664" x="190.296" width="225.72" height="407.52" bottomLeftOrigin="true"/>
</sfw> </sfw>
<rect width="300" height="200" fill="green"
x:baz="1"
i:baz="1"
graph:baz="1"
a:baz="1"
f:baz="1"
ir:baz="1"
custom:baz='1'
xpath:baz="1"
xmlns:v="http://ns.adobe.Variables/1.0/"
v:baz="1"
xmlns:sfw="http://ns.adobe.com/SaveForWeb/1.0/"
sfw:baz="1"
ok:baz="1" />
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

6
unittests/empty-g.svg Normal file
View file

@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg">
<g/>
<g>
<rect width="300" height="200" fill="green" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 118 B

View file

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg">
<metadata></metadata>
</svg>

After

Width:  |  Height:  |  Size: 71 B

View file

@ -1,5 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" <svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="dihttp://www.inkscape.org/namespaces/inkscape"
inkscape:version="0.46" version="1.0"> inkscape:version="0.46" version="1.0">
<inkscape:perspective inkscape:vp_x="0 : 526.18109 : 1" inkscape:vp_y="0 : 1000 : 0" inkscape:vp_z="744.09448 : 526.18109 : 1" inkscape:persp3d-origin="372.04724 : 350.78739 : 1" id="perspective3104"/> <inkscape:perspective inkscape:vp_x="0 : 526.18109 : 1" inkscape:vp_y="0 : 1000 : 0" inkscape:vp_z="744.09448 : 526.18109 : 1" inkscape:persp3d-origin="372.04724 : 350.78739 : 1" id="perspective3104"/>
<rect width="300" height="200" fill="green" /> <rect width="300" height="200" fill="green" inkscape:collect="always"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 396 B

After

Width:  |  Height:  |  Size: 423 B

Before After
Before After

View file

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg">
<metadata>This is a metadata element with only text node children</metadata>
</svg>

After

Width:  |  Height:  |  Size: 126 B