Skip to content

Commit b1a6902

Browse files
committed
gh-156812: Reject a trailing '-' when serializing an xml.dom.minidom comment
1 parent 3daa7f8 commit b1a6902

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/test/test_minidom.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,12 @@ def testSerializeCommentNodeWithDoubleHyphen(self):
17211721
doc.appendChild(doc.createComment("foo--bar"))
17221722
self.assertRaises(ValueError, doc.toxml)
17231723

1724+
def testSerializeCommentNodeWithTrailingHyphen(self):
1725+
# A trailing '-' would fuse with the closing '-->' into '--->'.
1726+
doc = create_doc_without_doctype()
1727+
doc.appendChild(doc.createComment("foo-"))
1728+
self.assertRaises(ValueError, doc.toxml)
1729+
17241730

17251731
def testEmptyXMLNSValue(self):
17261732
doc = parseString("<element xmlns=''>\n"

Lib/xml/dom/minidom.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,8 +1253,10 @@ def __init__(self, data):
12531253
self._data = data
12541254

12551255
def writexml(self, writer, indent="", addindent="", newl=""):
1256-
if "--" in self.data:
1257-
raise ValueError("'--' is not allowed in a comment node")
1256+
if "--" in self.data or self.data.endswith("-"):
1257+
raise ValueError(
1258+
"'--' is not allowed in a comment node, and a comment "
1259+
"node cannot end with '-'")
12581260
writer.write("%s<!--%s-->%s" % (indent, self.data, newl))
12591261

12601262

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Serializing an :mod:`xml.dom.minidom` comment node whose data ends with ``-``
2+
now raises :exc:`ValueError` instead of producing non-well-formed XML,
3+
completing the existing check for ``--``.

0 commit comments

Comments
 (0)