Skip to content

gh-81623: Do not add whitespace to significant content when pretty-printing - #156660

Open
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-81623-pretty-mixed-content
Open

gh-81623: Do not add whitespace to significant content when pretty-printing#156660
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-81623-pretty-mixed-content

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 30, 2026

Copy link
Copy Markdown
Member

Based on #156659.

Pretty-printing added whitespace inside an element which contains text, which changed its content.

xml.dom.minidom.Node.toprettyxml() indented every child of an element:

>>> parseString("<p>hello <b>world</b>!</p>").documentElement.toprettyxml()
'<p>\n\thello \n\t<b>world</b>\n\t!\n</p>\n'

xml.etree.ElementTree.indent() only avoided overwriting text and tails which are not white space, but the white space between them is significant too:

>>> tree = ET.fromstring("<p>hello <b>x</b> <i>y</i></p>")
>>> ET.indent(tree, space="  ")
>>> ET.tostring(tree, encoding="unicode")
'<p>hello <b>x</b>\n  <i>y</i>\n</p>'

Now whitespace is not added inside an element which is marked with xml:space="preserve" or which contains text. toprettyxml() also takes into account the content model declared in the DTD (only white space in element content is ignorable, see XML 1.0, 3.2.1), which covers the case in the issue.

This extends gh-48397, which exempted only an element whose single child is a text node.

… space

XML defines white space as " \t\r\n" (see XML 1.0, 2.3), but str.strip()
also strips other characters, such as U+00A0.  Such characters could be
lost in ElementTree.indent(), in canonicalize(strip_text=True), and when
parsing with the whitespace-in-element-content feature turned off.
…ttyxml()

Whitespace is no longer added inside an element which is marked with
xml:space="preserve", which is declared in the DTD as not having element
content, or, in absence of such declaration, which contains text.  Previously
such indentation changed the content of the element.
@read-the-docs-community

read-the-docs-community Bot commented Aug 30, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34299378 | 📁 Comparing 159e84a against main (d59d4e7)

  🔍 Preview build  

4 files changed
± library/xml.dom.minidom.html
± library/xml.etree.elementtree.html
± whatsnew/3.16.html
± whatsnew/changelog.html

ElementTree.indent() only avoided overwriting text and tails which are not
white space, but in an element which contains text the white space between
them is significant too.  xml:space="preserve" is now honored as well.
@serhiy-storchaka serhiy-storchaka changed the title gh-81623: Do not add whitespace to significant content in toprettyxml() gh-81623: Do not add whitespace to significant content when pretty-printing Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant