Skip to content

Commit 345572a

Browse files
authored
bpo-46786: Make ElementTree write the HTML tags embed, source, track, wbr as empty tags (GH-31406)
See https://html.spec.whatwg.org/multipage/syntax.html#void-elements for reference.
1 parent 5a1c637 commit 345572a

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

Lib/test/test_xml_etree.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,9 @@ def test_processinginstruction(self):
13501350
def test_html_empty_elems_serialization(self):
13511351
# issue 15970
13521352
# from http://www.w3.org/TR/html401/index/elements.html
1353-
for element in ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'FRAME', 'HR',
1354-
'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM']:
1353+
for element in ['AREA', 'BASE', 'BASEFONT', 'BR', 'COL', 'EMBED', 'FRAME',
1354+
'HR', 'IMG', 'INPUT', 'ISINDEX', 'LINK', 'META', 'PARAM',
1355+
'SOURCE', 'TRACK', 'WBR']:
13551356
for elem in [element, element.lower()]:
13561357
expected = '<%s>' % elem
13571358
serialized = serialize(ET.XML('<%s />' % elem), method='html')

Lib/xml/etree/ElementTree.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -918,13 +918,9 @@ def _serialize_xml(write, elem, qnames, namespaces,
918918
if elem.tail:
919919
write(_escape_cdata(elem.tail))
920920

921-
HTML_EMPTY = ("area", "base", "basefont", "br", "col", "frame", "hr",
922-
"img", "input", "isindex", "link", "meta", "param")
923-
924-
try:
925-
HTML_EMPTY = set(HTML_EMPTY)
926-
except NameError:
927-
pass
921+
HTML_EMPTY = {"area", "base", "basefont", "br", "col", "embed", "frame", "hr",
922+
"img", "input", "isindex", "link", "meta", "param", "source",
923+
"track", "wbr"}
928924

929925
def _serialize_html(write, elem, qnames, namespaces, **kwargs):
930926
tag = elem.tag
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The HTML serialisation in xml.etree.ElementTree now writes ``embed``,
2+
``source``, ``track`` and ``wbr`` as empty tags, as defined in HTML 5.

0 commit comments

Comments
 (0)