
Burak Arslan schrieb am 16.09.2014 um 10:12:
Void elements are html tags that don't need a closing tag. <br> is a well known example:
from lxml.builder import E html.tostring(E.br())
'<br>'
etree.tostring(E.br())
'<br/>'
<p> is not a void element, so:
html.tostring(E.p())
'<p></p>'
etree.tostring(E.p())
'<p/>'
see the full list:
http://www.w3.org/TR/html5/syntax.html#elements-0
While working on etree.htmlfile, I noticed that the following tags are not treated as void:
embed, keygen, source, track, wbr
see: https://github.com/lxml/lxml/pull/142#issuecomment-55588559
because of this, lxml can produce invalid html.
It's libxml2 serialising them, though. lxml has no word in this.
OTOH, it might be a good idea to make xf.element() aware of self-closing tags in HTML mode and let it a) write them out without closing tag and b) raise an exception when users attempt to write content into them.
You'd then write
with xf.element('meta', ...): pass
which isn't really worse than
xf.write(etree.Element('meta', ...))
That's another inconsistency, because the latter would still serialise some self-closing elements with closing tags. But I still consider it an improvement to provide a way for users to get it right at all.
Stefan