Another fix for xml:space=preserve
This commit is contained in:
parent
e0aacf646d
commit
7e483ce92f
2 changed files with 17 additions and 6 deletions
13
scour.py
13
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':
|
||||
# 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:
|
||||
|
|
|
|||
|
|
@ -936,6 +936,14 @@ class DoNotPrettyPrintWhenWhitespacePreserved(unittest.TestCase):
|
|||
</svg>''',
|
||||
'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 --disable-embed-rasters
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue