plq · GitHub

Behold:

>>> from six import BytesIO
>>> from lxml import etree
>>> f = BytesIO()
>>> with etree.htmlfile(f) as xf:
...     elt = etree.Element('script')
...     elt.text = "if (a < b);"
...     xf.write(elt)
...
>>> f.getvalue()
'<script>if (a < b);</script>'

... which is correct.

However:

>>> f = BytesIO()
>>> with etree.htmlfile(f) as xf:
...     with xf.element('script'):
...         xf.write("if (a < b);")
...
>>> f.getvalue()
'<script>if (a &lt; b);</script>'

... which is not only incorrect, but also quite annoying :)

This patch is an attempt at fixing it.

Read the original on github.com ↗