From 8f50f0d50996b57a3dc65bd7c66ef89a61a8c269 Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Tue, 28 Jul 2009 08:33:30 -0500 Subject: [PATCH] Fix Bug 405744: Keep namespace declarations when --keep-editor-data. Add tests. --- scour.py | 22 +++++++++++----------- scra.py | 3 ++- testscour.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/scour.py b/scour.py index e3fcdc2..aab3244 100755 --- a/scour.py +++ b/scour.py @@ -1517,17 +1517,17 @@ def scourString(in_string, options=None): pass while removeNamespacedAttributes( doc.documentElement, unwanted_ns ) > 0 : pass - - # remove the xmlns: declarations now - xmlnsDeclsToRemove = [] - attrList = doc.documentElement.attributes - for num in range(attrList.length) : - if attrList.item(num).nodeValue in unwanted_ns : - xmlnsDeclsToRemove.append(attrList.item(num).nodeName) - - for attr in xmlnsDeclsToRemove : - doc.documentElement.removeAttribute(attr) - numAttrsRemoved += 1 + + # remove the xmlns: declarations now + xmlnsDeclsToRemove = [] + attrList = doc.documentElement.attributes + for num in range(attrList.length) : + if attrList.item(num).nodeValue in unwanted_ns : + xmlnsDeclsToRemove.append(attrList.item(num).nodeName) + + for attr in xmlnsDeclsToRemove : + doc.documentElement.removeAttribute(attr) + numAttrsRemoved += 1 # repair style (remove unnecessary style properties and change them into XML attributes) numStylePropsFixed = repairStyle(doc.documentElement, options) diff --git a/scra.py b/scra.py index 91c5d69..f09a778 100644 --- a/scra.py +++ b/scra.py @@ -83,6 +83,7 @@ class ScourOptions: group_collapse = True strip_ids = False digits = 5 + embed_rasters = False keep_editor_data = False # params are the form elements (if a checkbox is unchecked it will not be present) @@ -104,6 +105,6 @@ def fetch(req, indoc,**params): fileitem = fs['upload'] if fileitem.filename: - req.write(scourString(fileitem.file.read())) + req.write(scourString(fileitem.file.read(), options)) else: req.write(scourString(indoc,options)) diff --git a/testscour.py b/testscour.py index ce9459d..3af701f 100755 --- a/testscour.py +++ b/testscour.py @@ -44,6 +44,7 @@ class ScourOptions: strip_ids = False digits = 5 embed_rasters = True + keep_editor_data = False class NoInkscapeElements(unittest.TestCase): def runTest(self): @@ -257,6 +258,34 @@ class NoInkscapeAttributes(unittest.TestCase): findInkscapeAttr), False, 'Found Inkscape attributes' ) +class KeepInkscapeNamespaceDeclarationsWhenKeepEditorData(unittest.TestCase): + def runTest(self): + options = ScourOptions + options.keep_editor_data = True + attrs = scour.scourXmlFile('unittests/inkscape.svg', options).documentElement.attributes + FoundNamespace = False + for i in range(len(attrs)): + if attrs.item(i).nodeValue == 'http://www.inkscape.org/namespaces/inkscape': + FoundNamespace = True + break + self.assertEquals(True, FoundNamespace, + "Did not find Inkscape namespace declaration when using --keep-editor-data") + return False + +class KeepSodipodiNamespaceDeclarationsWhenKeepEditorData(unittest.TestCase): + def runTest(self): + options = ScourOptions + options.keep_editor_data = True + attrs = scour.scourXmlFile('unittests/sodipodi.svg', options).documentElement.attributes + FoundNamespace = False + for i in range(len(attrs)): + if attrs.item(i).nodeValue == 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd': + FoundNamespace = True + break + self.assertEquals(True, FoundNamespace, + "Did not find Sodipodi namespace declaration when using --keep-editor-data") + return False + class KeepReferencedFonts(unittest.TestCase): def runTest(self): doc = scour.scourXmlFile('unittests/referenced-font.svg') @@ -604,13 +633,14 @@ class DoNotRemoveGroupsWithIDsInDefs(unittest.TestCase): class AlwaysKeepClosePathSegments(unittest.TestCase): def runTest(self): - p= scour.scourXmlFile('unittests/path-with-closepath.svg').getElementsByTagNameNS(SVGNS, 'path')[0] + p = scour.scourXmlFile('unittests/path-with-closepath.svg').getElementsByTagNameNS(SVGNS, 'path')[0] self.assertEquals(p.getAttribute('d'), 'M10,10h100v100h-100z', 'Path with closepath not preserved') # TODO: write tests for --set-precision for path data, for polygon data, for attributes # TODO; write a test for embedding rasters # TODO: write a test for --disable-embed-rasters +# TODO: write tests for --keep-editor-data if __name__ == '__main__': unittest.main()