mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-50002: xml.dom.minidom now preserves whitespaces in attributes (GH-107947)
Also double quotes (") are now only quoted in attributes.
This commit is contained in:
parent
29bc6165ab
commit
154477be72
4 changed files with 65 additions and 7 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue