From 2cc9c00ef71f29ebe4e16c2f1a50e9c90c7c6888 Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Wed, 15 Apr 2009 10:18:32 -0500 Subject: [PATCH] Prevent removal of metadata elements if they have only text nodes. Also added some unit tests. --- scour.py | 7 +++++-- testscour.py | 24 ++++++++++++++++++++++++ unittests/adobe.svg | 15 +++++++++++++++ unittests/empty-g.svg | 6 ++++++ unittests/empty-metadata.svg | 3 +++ unittests/inkscape.svg | 4 ++-- unittests/metadata-with-text.svg | 3 +++ 7 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 unittests/empty-g.svg create mode 100644 unittests/empty-metadata.svg create mode 100644 unittests/metadata-with-text.svg diff --git a/scour.py b/scour.py index dd0961a..2d88ee2 100755 --- a/scour.py +++ b/scour.py @@ -51,7 +51,10 @@ # when being run as main (for unit testing) # - Removed duplicate gradient stops # - 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 from __future__ import division @@ -597,7 +600,7 @@ def scourString(in_string): removeElem = not elem.hasChildNodes() if removeElem == False : for child in elem.childNodes : - if child.nodeType in [1, 4, 8] : + if child.nodeType in [1, 3, 4, 8] : break else: removeElem = True diff --git a/testscour.py b/testscour.py index 4e454fb..43116c2 100755 --- a/testscour.py +++ b/testscour.py @@ -95,6 +95,30 @@ class NoAdobeXPathElements(unittest.TestCase): lambda e: e.namespaceURI != 'http://ns.adobe.com/XPath/1.0/'), False, '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__': unittest.main() diff --git a/unittests/adobe.svg b/unittests/adobe.svg index f5dba16..0b3a39f 100644 --- a/unittests/adobe.svg +++ b/unittests/adobe.svg @@ -7,6 +7,7 @@ xmlns:ir="http://ns.adobe.com/ImageReplacement/1.0/" xmlns:custom="http://ns.adobe.com/GenericCustomNamespace/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"> bar bar @@ -26,4 +27,18 @@ + \ No newline at end of file diff --git a/unittests/empty-g.svg b/unittests/empty-g.svg new file mode 100644 index 0000000..fe8fc36 --- /dev/null +++ b/unittests/empty-g.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/unittests/empty-metadata.svg b/unittests/empty-metadata.svg new file mode 100644 index 0000000..1d373d3 --- /dev/null +++ b/unittests/empty-metadata.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/unittests/inkscape.svg b/unittests/inkscape.svg index e41b756..4b72559 100644 --- a/unittests/inkscape.svg +++ b/unittests/inkscape.svg @@ -1,5 +1,5 @@ - - + \ No newline at end of file diff --git a/unittests/metadata-with-text.svg b/unittests/metadata-with-text.svg new file mode 100644 index 0000000..47c8881 --- /dev/null +++ b/unittests/metadata-with-text.svg @@ -0,0 +1,3 @@ + + This is a metadata element with only text node children + \ No newline at end of file