From 7e483ce92f344c7e5dcedf426abe2d9bb4f382ef Mon Sep 17 00:00:00 2001 From: JSCHILL1 Date: Thu, 24 Sep 2009 18:14:30 -0500 Subject: [PATCH] Another fix for xml:space=preserve --- scour.py | 15 +++++++++------ testscour.py | 8 ++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/scour.py b/scour.py index a4dbf7d..5062f33 100755 --- a/scour.py +++ b/scour.py @@ -2035,12 +2035,11 @@ def makeWellFormed(str): # - pretty printing # - somewhat judicious use of whitespace # - ensure id attributes are first -def serializeXML(element, options, ind = 0): +def serializeXML(element, options, ind = 0, preserveWhitespace = False): indent = ind I='' if options.indent_type == 'tab': I='\t' elif options.indent_type == 'space': I=' ' - preserveWhitespace = False outString = (I * ind) + '<' + element.nodeName @@ -2076,8 +2075,12 @@ def serializeXML(element, options, ind = 0): outString += 'xmlns:' outString += attr.nodeName + '=' + quot + attrValue + quot - if attr.nodeName == 'xml:space' and attrValue == 'preserve': - preserveWhitespace = True + # TODO: when to set preserveWhitespace to true, with a value of 'none'? + if attr.nodeName == 'xml:space': + if attrValue == 'preserve': + preserveWhitespace = True + elif attrValue == 'default': + preserveWhitespace = False # if no children, self-close children = element.childNodes @@ -2089,9 +2092,9 @@ def serializeXML(element, options, ind = 0): # element node if child.nodeType == 1: if preserveWhitespace: - outString += serializeXML(child, options, 0) + outString += serializeXML(child, options, 0, preserveWhitespace) else: - outString += '\n' + serializeXML(child, options, indent + 1) + outString += '\n' + serializeXML(child, options, indent + 1, preserveWhitespace) onNewLine = True # text node elif child.nodeType == 3: diff --git a/testscour.py b/testscour.py index c7017b7..4b5b390 100755 --- a/testscour.py +++ b/testscour.py @@ -936,6 +936,14 @@ class DoNotPrettyPrintWhenWhitespacePreserved(unittest.TestCase): ''', 'Whitespace not preserved') +class DoNotPrettyPrintWhenNestedWhitespacePreserved(unittest.TestCase): + def runTest(self): + self.assertEquals( scour.scourString(open('unittests/whitespace-nested.svg').read()), + ''' + + Use bold text +''', + 'Whitespace not preserved when nested') # TODO; write a test for embedding rasters # TODO: write a test for --disable-embed-rasters