New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <script> and <style> text content serialization #218

Merged
merged 1 commit into from Dec 23, 2016

Conversation

Projects
None yet
2 participants
@plq
Copy link
Contributor

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.

@plq plq force-pushed the plq:master branch from 1633f86 to b383b32 Dec 21, 2016

@@ -954,7 +954,15 @@ cdef class _IncrementalFileWriter:
if self._status > WRITER_IN_ELEMENT or content.strip():
raise LxmlSyntaxError("not in an element")
content = _utf8(content)
tree.xmlOutputBufferWriteEscape(self._c_out, _xcstr(content), NULL)
if c_method == OUTPUT_METHOD_XML:

This comment has been minimized.

@scoder

scoder Dec 22, 2016

Member

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

This comment has been minimized.

@plq

plq Dec 23, 2016

Author Contributor

Fixed.

@plq plq force-pushed the plq:master branch from b383b32 to ea1a743 Dec 23, 2016

@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