From 40753af88a1570307a1ce08fb1f5a967c8545793 Mon Sep 17 00:00:00 2001 From: Patrick Storz Date: Sun, 17 May 2020 17:33:50 +0200 Subject: [PATCH] Fix whitespace handling for SVG 1.2 flowed text See 718748ff22ca4477a884035bfbf2175639043275 Fixes https://github.com/scour-project/scour/issues/235 --- scour/scour.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 64bf9bf..661dd9a 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -3354,6 +3354,10 @@ def chooseQuoteCharacter(str): return (quote, hasEmbeddedQuote) +TEXT_CONTENT_ELEMENTS = ['text', 'tspan', 'tref', 'textPath', 'altGlyph', + 'flowDiv', 'flowPara', 'flowSpan', 'flowTref', 'flowLine'] + + # hand-rolled serialization function that has the following benefits: # - pretty printing # - somewhat judicious use of whitespace @@ -3437,7 +3441,7 @@ def serializeXML(element, options, indent_depth=0, preserveWhitespace=False): # "text1\ntext2" and # "text1\n text2" # see https://www.w3.org/TR/SVG/text.html#WhiteSpace - if preserveWhitespace or element.nodeName in ['text', 'tspan', 'tref', 'textPath', 'altGlyph']: + if preserveWhitespace or element.nodeName in TEXT_CONTENT_ELEMENTS: outParts.append(serializeXML(child, options, 0, preserveWhitespace)) else: outParts.extend([newline, serializeXML(child, options, indent_depth + 1, preserveWhitespace)]) @@ -3448,7 +3452,7 @@ def serializeXML(element, options, indent_depth=0, preserveWhitespace=False): if not preserveWhitespace: # strip / consolidate whitespace according to spec, see # https://www.w3.org/TR/SVG/text.html#WhiteSpace - if element.nodeName in ['text', 'tspan', 'tref', 'textPath', 'altGlyph']: + if element.nodeName in TEXT_CONTENT_ELEMENTS: text_content = text_content.replace('\n', '') text_content = text_content.replace('\t', ' ') if child == element.firstChild: