Skip to content
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
Merged

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.

@@ -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:
Copy link
Member

@scoder scoder Dec 22, 2016

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

Copy link
Contributor Author

@plq plq Dec 23, 2016

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
2 participants