Fix XML serialization when xml:space=preserve
This commit is contained in:
parent
7652fbc76c
commit
e0aacf646d
3 changed files with 27 additions and 4 deletions
11
scour.py
11
scour.py
|
|
@ -38,6 +38,7 @@
|
||||||
# + prevent elements from being stripped if they are referenced in a <style> element
|
# + prevent elements from being stripped if they are referenced in a <style> element
|
||||||
# + only move common attributes and remove unused attributes after removing duplicate gradients
|
# + only move common attributes and remove unused attributes after removing duplicate gradients
|
||||||
# + only move common attributes to parent if the parent contains non-whitespace text nodes
|
# + only move common attributes to parent if the parent contains non-whitespace text nodes
|
||||||
|
# + do not pretty-print elements if whitespace is important (xml:space="preserve")
|
||||||
# - TODO: fix the removal of comment elements (between <?xml?> and <svg>)
|
# - TODO: fix the removal of comment elements (between <?xml?> and <svg>)
|
||||||
# (for instance, filter, marker, pattern) - need a crude CSS parser
|
# (for instance, filter, marker, pattern) - need a crude CSS parser
|
||||||
# - add an option to remove ids if they match the Inkscape-style of IDs
|
# - add an option to remove ids if they match the Inkscape-style of IDs
|
||||||
|
|
@ -2039,6 +2040,7 @@ def serializeXML(element, options, ind = 0):
|
||||||
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
|
||||||
|
|
||||||
|
|
@ -2074,6 +2076,9 @@ 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':
|
||||||
|
preserveWhitespace = True
|
||||||
|
|
||||||
# if no children, self-close
|
# if no children, self-close
|
||||||
children = element.childNodes
|
children = element.childNodes
|
||||||
if children.length > 0:
|
if children.length > 0:
|
||||||
|
|
@ -2083,13 +2088,17 @@ def serializeXML(element, options, ind = 0):
|
||||||
for child in element.childNodes:
|
for child in element.childNodes:
|
||||||
# element node
|
# element node
|
||||||
if child.nodeType == 1:
|
if child.nodeType == 1:
|
||||||
|
if preserveWhitespace:
|
||||||
|
outString += serializeXML(child, options, 0)
|
||||||
|
else:
|
||||||
outString += '\n' + serializeXML(child, options, indent + 1)
|
outString += '\n' + serializeXML(child, options, indent + 1)
|
||||||
onNewLine = True
|
onNewLine = True
|
||||||
# text node
|
# text node
|
||||||
elif child.nodeType == 3:
|
elif child.nodeType == 3:
|
||||||
# trim it only in the case of not being a child of an element
|
# trim it only in the case of not being a child of an element
|
||||||
# where whitespace might be important
|
# where whitespace might be important
|
||||||
if element.nodeName in ["text", "tspan", "textPath", "tref", "title", "desc", "textArea"]:
|
if element.nodeName in ["text", "tspan", "textPath", "tref", "title", "desc", "textArea",
|
||||||
|
"flowRoot", "flowDiv", "flowSpan", "flowPara", "flowRegion"]:
|
||||||
outString += makeWellFormed(child.nodeValue)
|
outString += makeWellFormed(child.nodeValue)
|
||||||
else:
|
else:
|
||||||
outString += makeWellFormed(child.nodeValue.strip())
|
outString += makeWellFormed(child.nodeValue.strip())
|
||||||
|
|
|
||||||
10
testscour.py
10
testscour.py
|
|
@ -927,6 +927,16 @@ class DoNotRemoveGradientsWhenReferencedInStyleCss(unittest.TestCase):
|
||||||
self.assertEquals( grads.length, 2,
|
self.assertEquals( grads.length, 2,
|
||||||
'Gradients removed when referenced in CSS')
|
'Gradients removed when referenced in CSS')
|
||||||
|
|
||||||
|
class DoNotPrettyPrintWhenWhitespacePreserved(unittest.TestCase):
|
||||||
|
def runTest(self):
|
||||||
|
self.assertEquals( scour.scourString(open('unittests/whitespace-important.svg').read()),
|
||||||
|
'''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<text xml:space="preserve">This is some <tspan font-style="italic">messed-up</tspan> markup</text>
|
||||||
|
</svg>''',
|
||||||
|
'Whitespace not preserved')
|
||||||
|
|
||||||
|
|
||||||
# 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
|
||||||
# TODO: write tests for --keep-editor-data
|
# TODO: write tests for --keep-editor-data
|
||||||
|
|
|
||||||
4
unittests/whitespace-important.svg
Normal file
4
unittests/whitespace-important.svg
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<text xml:space="preserve">This is some <tspan font-style="italic">messed-up</tspan> markup</text>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 202 B |
Loading…
Add table
Add a link
Reference in a new issue