This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: embed, source, track, wbr HTML elements not considered empty
Type: enhancement Stage: resolved
Components: XML Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eli.bendersky, jnns, scoder
Priority: normal Keywords: patch

Created on 2022-02-18 10:23 by jnns, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31406 merged jnns, 2022-02-18 11:14
Messages (3)
msg413473 - (view) Author: jnns (jnns) * Date: 2022-02-18 10:23
[According to the WHATWG][1], the elements `area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `link`, `meta`, `param`, `source`, `track`, `wbr` are *void elements* that don't need and therefore shouldn't have a closing tag.

The source view of Firefox 96 shows a warning about an unexpected closing tag [1].

In Python 3.10.2 `xml.etree` seems to correctly recognize most of them as such and doesn't generate closing tags when using the `.tostring()` method. A few elements are serialized with a closing tag (`<embed></embed>` for example). 

```python
from xml.etree import ElementTree as etree

void_elements = [
    "area", "base","br", "col", "embed", "hr", "img", 
    "input", "link", "meta", "param", "source", "track", "wbr"
]
for el in void_elements:
    el = etree.Element(el)
    print(etree.tostring(el, method="html", encoding="unicode"))
```

```html
<area>
<base>
<br>
<col>
<embed></embed>
<hr>
<img>
<input>
<link>
<meta>
<param>
<source></source>
<track></track>
<wbr></wbr>
```

HTML_EMPTY in Lib/xml/etree/ElementTree.py only contains the following entries:

"area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "isindex", "link", "meta", "param"

I suppose "embed", "source", "track" and "wbr" should be added to that list.  

  [1]: https://html.spec.whatwg.org/multipage/syntax.html#void-elements
  [2]: https://i.stack.imgur.com/rBTHw.png
msg413707 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2022-02-22 13:38
Makes sense. That list hasn't been updated in 10 years.
msg414157 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2022-02-27 14:26
New changeset 345572a1a0263076081020524016eae867677cac by Jannis Vajen in branch 'main':
bpo-46786: Make ElementTree write the HTML tags embed, source, track, wbr as empty tags (GH-31406)
https://github.com/python/cpython/commit/345572a1a0263076081020524016eae867677cac
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90942
2022-02-27 14:26:44scodersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-02-27 14:26:21scodersetmessages: + msg414157
2022-02-22 13:38:34scodersetmessages: + msg413707
versions: - Python 3.7, Python 3.8, Python 3.9, Python 3.10
2022-02-21 20:57:12ned.deilysetnosy: + scoder, eli.bendersky
2022-02-18 11:14:06jnnssetkeywords: + patch
stage: patch review
pull_requests: + pull_request29546
2022-02-18 10:23:19jnnscreate