Precise XML Whitespace Handling in Python's XML Libraries
from: gh-156658: Only XML white space characters are treated as white space
The Concept
XML defines whitespace very specifically as the characters space, tab, carriage return, and newline. However, some string operations like Python's str.strip() remove additional Unicode whitespace characters that XML treats as content. Properly handling these distinctions is crucial in XML parsing and processing to avoid data loss or altering the document's canonical form.
How This PR Does It
This PR refines whitespace handling by ensuring only XML-defined whitespace characters (space, tab, carriage return, newline) are treated as whitespace. It corrects ElementTree.indent() so it no longer overwrites content containing non-XML whitespace characters, adjusts canonicalize(strip_text=True) to preserve these characters, and updates xml.dom parsing and Text.isWhitespaceInElementContent to respect the XML whitespace definition. The changes prevent unintentional stripping or removal of valid content characters, preserving the document's integrity.
Why It Matters
Understanding and correctly implementing XML whitespace rules prevents subtle bugs where meaningful content is lost or altered during parsing, serialization, or canonicalization. This is especially important for applications relying on exact XML document fidelity, such as digital signatures or configuration files.
Try It Yourself
Review the changes made to ElementTree.indent() in this PR. How would you modify a custom XML processing function that currently uses str.strip() to trim text nodes so that it respects XML whitespace rules like in this PR? Implement a small helper function that strips only XML whitespace characters and test it on strings containing non-XML whitespace such as U+00A0.