summaryrefslogtreecommitdiffstats
path: root/Lib/xml
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-08-23 12:23:41 (GMT)
committerGitHub <noreply@github.com>2023-08-23 12:23:41 (GMT)
commit154477be722ae5c4e18d22d0860e284006b09c4f (patch)
tree04df1403a74ed9d464574298db337f81b370b074 /Lib/xml
parent29bc6165ab8aa434145a34676b8b7e48e7c6e308 (diff)
downloadcpython-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.py30
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("&", "&amp;").replace("<", "&lt;"). \
- replace("\"", "&quot;").replace(">", "&gt;")
- writer.write(data)
+ if not text:
+ return
+ # See the comments in ElementTree.py for behavior and
+ # implementation details.
+ if "&" in text:
+ text = text.replace("&", "&amp;")
+ if "<" in text:
+ text = text.replace("<", "&lt;")
+ if ">" in text:
+ text = text.replace(">", "&gt;")
+ if attr:
+ if '"' in text:
+ text = text.replace('"', "&quot;")
+ if "\r" in text:
+ text = text.replace("\r", "&#13;")
+ if "\n" in text:
+ text = text.replace("\n", "&#10;")
+ if "\t" in text:
+ text = text.replace("\t", "&#9;")
+ 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)