Repository navigation
Minidom should not pretty-print inside text elements #81623
Description
Activity
Inside text elements both in HTML and SVG white-space is significant and introduces differences in how things are rendered. By default in general all white-space is collapsed into one space and then this is rendered, adding additional text content.
I observed this while working with SVG which can have content like:
<text><tspan>foo</tspan><tspan>bar</tspan></text>
After pretty-printing it with minidom, and white-space collapsing, the following is what is input to SVG rendering:
<text> <tspan>foo</tspan> <tspan>bar</tspan> </text>
And space between "foo" and "bar" is now visible and while before it was one word to the user, now it is shown as two.
Related issue: mozman/svgwrite#58
I think pretty-printing not add whitespace inside text elements.
Still an issue for me with svgwrite because of that you basically either avoid pretty-printing or find some hack to remove the text inside tag - e.g. if you know font-size for children tags you can do:
font-size: 0on text.PR #156660 stops pretty-printing from adding whitespace inside an element which is marked with
xml:space="preserve"or which contains text, in bothxml.dom.minidom.toprettyxml()andxml.etree.ElementTree.indent().toprettyxml()also takes into account the content model declared in the DTD: only white space in element content is ignorable (XML 1.0, 3.2.1). With such declaration the reported case is no longer changed:>>> src = ("<!DOCTYPE text [<!ELEMENT text (#PCDATA|tspan)*><!ELEMENT tspan (#PCDATA)>]>" ... "<text><tspan>foo</tspan><tspan>bar</tspan></text>") >>> parseString(src).documentElement.toprettyxml(indent=" ") '<text><tspan>foo</tspan><tspan>bar</tspan></text>\n'
Without a DTD nothing in the document says that the whitespace between the
tspanelements is significant, andxml:space="preserve"is the only way to express it.ElementTreehas no such information at all.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs