Skip to content

Conversation

@plq
Copy link
Contributor

@plq plq commented Dec 21, 2016

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.

raise LxmlSyntaxError("not in an element")
content = _utf8(content)
tree.xmlOutputBufferWriteEscape(self._c_out, _xcstr(content), NULL)
if c_method == OUTPUT_METHOD_XML:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reversed. Shouldn't your special casing apply only to HTML, instead of everything but XML?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@scoder scoder merged commit bc77035 into lxml:master Dec 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants