diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 6c5bf02c33d..e2ffc19ad60 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -742,6 +742,13 @@ def test_methods(self):
'\n')
self.assertEqual(serialize(e, method="text"), '1 < 2\n')
+ def test_issue18347(self):
+ e = ET.XML('text')
+ self.assertEqual(serialize(e),
+ 'text')
+ self.assertEqual(serialize(e, method="html"),
+ 'text')
+
def test_entity(self):
# Test entity handling.
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index edf25818f13..56c91cce2e6 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -992,15 +992,15 @@ def _serialize_html(write, elem, qnames, namespaces, **kwargs):
# FIXME: handle boolean attributes
write(" %s=\"%s\"" % (qnames[k], v))
write(">")
- tag = tag.lower()
+ ltag = tag.lower()
if text:
- if tag == "script" or tag == "style":
+ if ltag == "script" or ltag == "style":
write(text)
else:
write(_escape_cdata(text))
for e in elem:
_serialize_html(write, e, qnames, None)
- if tag not in HTML_EMPTY:
+ if ltag not in HTML_EMPTY:
write("" + tag + ">")
if elem.tail:
write(_escape_cdata(elem.tail))
diff --git a/Misc/NEWS b/Misc/NEWS
index fc505376b07..f3a099dc506 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -135,6 +135,9 @@ Core and Builtins
Library
-------
+- Issue #18347: ElementTree's html serializer now preserves the case of
+ closing tags.
+
- Issue #17261: Ensure multiprocessing's proxies use proper address.
- Issue #18343: faulthandler.register() now keeps the previous signal handler