You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Minidom should not pretty-print inside text elements #81623
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2019-06-28.18:04:51.410>labels= ['expert-XML']
title='Minidom should not pretty-print inside text elements'updated_at=<Date2019-06-28.18:04:51.410>user='https://github.com/mitar'
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:
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: 0 on text.
PR #156660 stops pretty-printing from adding whitespace inside an element which is marked with xml:space="preserve" or which contains text, in both xml.dom.minidom.toprettyxml() and xml.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 tspan elements is significant, and xml:space="preserve" is the only way to express it. ElementTree has no such information at all.
Activity
mitar commentedon Jun 28, 2019
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.
Andrej730 commentedon Mar 16, 2023
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.pythongh-81623: Do not add whitespace to significant content in indent()
serhiy-storchaka commentedon Aug 30, 2026
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: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.Merge remote-tracking branch 'upstream/main' into pythongh-81623-pret…
gh-81623: Do not add whitespace to significant content when pretty-pr…
pythongh-81623: Do not add whitespace to significant content when pre…