Skip to content

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

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:gh-81623-pretty-mixed-content
Sep 13, 2026
Merged

gh-81623: Do not add whitespace to significant content when pretty-printing#156660
serhiy-storchaka merged 4 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 #34534452 | 📁 Comparing fe41fd7 against main (d59d4e7)

  🔍 Preview build  

60 files changed · + 1 added · ± 58 modified · - 1 deleted

+ Added

± Modified

- Deleted

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
…ty-mixed-content

# Conflicts:
#	Lib/test/test_xml_etree.py
#	Lib/xml/etree/ElementTree.py
@serhiy-storchaka
serhiy-storchaka merged commit 3308360 into python:main Sep 13, 2026
54 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-81623-pretty-mixed-content branch September 13, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant