gh-81623: Do not add whitespace to significant content when pretty-printing - #156660
Open
serhiy-storchaka wants to merge 3 commits into
Open
gh-81623: Do not add whitespace to significant content when pretty-printing#156660serhiy-storchaka wants to merge 3 commits into
serhiy-storchaka wants to merge 3 commits into
Conversation
… 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.
Documentation build overview
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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.