diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-08-23 12:23:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-23 12:23:41 (GMT) |
commit | 154477be722ae5c4e18d22d0860e284006b09c4f (patch) | |
tree | 04df1403a74ed9d464574298db337f81b370b074 /Lib/xml | |
parent | 29bc6165ab8aa434145a34676b8b7e48e7c6e308 (diff) | |
download | cpython-154477be722ae5c4e18d22d0860e284006b09c4f.zip cpython-154477be722ae5c4e18d22d0860e284006b09c4f.tar.gz cpython-154477be722ae5c4e18d22d0860e284006b09c4f.tar.bz2 |
gh-50002: xml.dom.minidom now preserves whitespaces in attributes (GH-107947)
Also double quotes (") are now only quoted in attributes.
Diffstat (limited to 'Lib/xml')
-rw-r--r-- | Lib/xml/dom/minidom.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py index ef8a159..db51f35 100644 --- a/Lib/xml/dom/minidom.py +++ b/Lib/xml/dom/minidom.py @@ -300,12 +300,28 @@ def _in_document(node): node = node.parentNode return False -def _write_data(writer, data): +def _write_data(writer, text, attr): "Writes datachars to writer." - if data: - data = data.replace("&", "&").replace("<", "<"). \ - replace("\"", """).replace(">", ">") - writer.write(data) + if not text: + return + # See the comments in ElementTree.py for behavior and + # implementation details. + if "&" in text: + text = text.replace("&", "&") + if "<" in text: + text = text.replace("<", "<") + if ">" in text: + text = text.replace(">", ">") + if attr: + if '"' in text: + text = text.replace('"', """) + if "\r" in text: + text = text.replace("\r", " ") + if "\n" in text: + text = text.replace("\n", " ") + if "\t" in text: + text = text.replace("\t", "	") + writer.write(text) def _get_elements_by_tagName_helper(parent, name, rc): for node in parent.childNodes: @@ -883,7 +899,7 @@ class Element(Node): for a_name in attrs.keys(): writer.write(" %s=\"" % a_name) - _write_data(writer, attrs[a_name].value) + _write_data(writer, attrs[a_name].value, True) writer.write("\"") if self.childNodes: writer.write(">") @@ -1112,7 +1128,7 @@ class Text(CharacterData): return newText def writexml(self, writer, indent="", addindent="", newl=""): - _write_data(writer, "%s%s%s" % (indent, self.data, newl)) + _write_data(writer, "%s%s%s" % (indent, self.data, newl), False) # DOM Level 3 (WD 9 April 2002) |