Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-50002: xml.dom.minidom now preserves whitespaces in attributes#107947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-50002: xml.dom.minidom now preserves whitespaces in attributes
Also double quotes (") are now only quoted in attributes.
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedAug 14, 2023
commitdd6c84cb4f74fd5773933acc71e65f6c0d215305
40 changes: 40 additions & 0 deletionsLib/test/test_minidom.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -505,6 +505,46 @@ def testWriteXML(self):
dom.unlink()
self.confirm(str == domstr)

def test_toxml_quote_text(self):
dom = Document()
elem = dom.appendChild(dom.createElement('elem'))
elem.appendChild(dom.createTextNode('&<>"'))
cr = elem.appendChild(dom.createElement('cr'))
cr.appendChild(dom.createTextNode('\r'))
crlf = elem.appendChild(dom.createElement('crlf'))
crlf.appendChild(dom.createTextNode('\r\n'))
lflf = elem.appendChild(dom.createElement('lflf'))
lflf.appendChild(dom.createTextNode('\n\n'))
ws = elem.appendChild(dom.createElement('ws'))
ws.appendChild(dom.createTextNode('\t\n\r '))
domstr = dom.toxml()
dom.unlink()
self.assertEqual(domstr, '<?xml version="1.0" ?>'
'<elem>&amp;&lt;&gt;"'
'<cr>\r</cr>'
'<crlf>\r\n</crlf>'
'<lflf>\n\n</lflf>'
'<ws>\t\n\r </ws></elem>')

def test_toxml_quote_attrib(self):
dom = Document()
elem = dom.appendChild(dom.createElement('elem'))
elem.setAttribute("a", '&<>"')
elem.setAttribute("cr", "\r")
elem.setAttribute("lf", "\n")
elem.setAttribute("crlf", "\r\n")
elem.setAttribute("lflf", "\n\n")
elem.setAttribute("ws", "\t\n\r ")
domstr = dom.toxml()
dom.unlink()
self.assertEqual(domstr, '<?xml version="1.0" ?>'
'<elem a="&amp;&lt;&gt;&quot;" '
'cr="&#13;" '
'lf="&#10;" '
'crlf="&#13;&#10;" '
'lflf="&#10;&#10;" '
'ws="&#09;&#10;&#13; "/>')

def testAltNewline(self):
str = '<?xml version="1.0" ?>\n<a b="c"/>\n'
dom = parseString(str)
Expand Down
29 changes: 22 additions & 7 deletionsLib/xml/dom/minidom.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -300,12 +300,27 @@ 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 text:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I usually prefer representing easy special cases in a short

Suggested change
iftext:
ifnottext:
return

rather than adding indentation to long chunks of code that only makes readers look down to see if something joined is still done at the end. Better make it clear right away that we're done handling this case.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Agree. I just kept the style of the old code.

# See comments in ElementTree.py about 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", "&#09;")
writer.write(text)

def _get_elements_by_tagName_helper(parent, name, rc):
for node in parent.childNodes:
Expand DownExpand Up@@ -883,7 +898,7 @@ def writexml(self, writer, indent="", addindent="", newl=""):

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(">")
Expand DownExpand Up@@ -1112,7 +1127,7 @@ def splitText(self, offset):
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)

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
:mod:`xml.dom.minidom` now preserves whitespaces in attributes.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
:mod:`xml.dom.minidom` now only quotes ``"`` in attributes.

[8]ページ先頭

©2009-2025 Movatter.jp