Another fix for xml:space=preserve

This commit is contained in:
JSCHILL1 2009-09-24 18:14:30 -05:00
parent e0aacf646d
commit 7e483ce92f
2 changed files with 17 additions and 6 deletions

View file

@ -2035,12 +2035,11 @@ def makeWellFormed(str):
# - pretty printing # - pretty printing
# - somewhat judicious use of whitespace # - somewhat judicious use of whitespace
# - ensure id attributes are first # - ensure id attributes are first
def serializeXML(element, options, ind = 0): def serializeXML(element, options, ind = 0, preserveWhitespace = False):
indent = ind indent = ind
I='' I=''
if options.indent_type == 'tab': I='\t' if options.indent_type == 'tab': I='\t'
elif options.indent_type == 'space': I=' ' elif options.indent_type == 'space': I=' '
preserveWhitespace = False
outString = (I * ind) + '<' + element.nodeName outString = (I * ind) + '<' + element.nodeName
@ -2076,8 +2075,12 @@ def serializeXML(element, options, ind = 0):
outString += 'xmlns:' outString += 'xmlns:'
outString += attr.nodeName + '=' + quot + attrValue + quot outString += attr.nodeName + '=' + quot + attrValue + quot
if attr.nodeName == 'xml:space' and attrValue == 'preserve': # TODO: when to set preserveWhitespace to true, with a value of 'none'?
if attr.nodeName == 'xml:space':
if attrValue == 'preserve':
preserveWhitespace = True preserveWhitespace = True
elif attrValue == 'default':
preserveWhitespace = False
# if no children, self-close # if no children, self-close
children = element.childNodes children = element.childNodes
@ -2089,9 +2092,9 @@ def serializeXML(element, options, ind = 0):
# element node # element node
if child.nodeType == 1: if child.nodeType == 1:
if preserveWhitespace: if preserveWhitespace:
outString += serializeXML(child, options, 0) outString += serializeXML(child, options, 0, preserveWhitespace)
else: else:
outString += '\n' + serializeXML(child, options, indent + 1) outString += '\n' + serializeXML(child, options, indent + 1, preserveWhitespace)
onNewLine = True onNewLine = True
# text node # text node
elif child.nodeType == 3: elif child.nodeType == 3:

View file

@ -936,6 +936,14 @@ class DoNotPrettyPrintWhenWhitespacePreserved(unittest.TestCase):
</svg>''', </svg>''',
'Whitespace not preserved') 'Whitespace not preserved')
class DoNotPrettyPrintWhenNestedWhitespacePreserved(unittest.TestCase):
def runTest(self):
self.assertEquals( scour.scourString(open('unittests/whitespace-nested.svg').read()),
'''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg">
<text xml:space="preserve"><tspan font-style="italic">Use <tspan font-style="bold">bold</tspan> text</tspan></text>
</svg>''',
'Whitespace not preserved when nested')
# TODO; write a test for embedding rasters # TODO; write a test for embedding rasters
# TODO: write a test for --disable-embed-rasters # TODO: write a test for --disable-embed-rasters