summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscoder <stefan_ml@behnel.de>2016-12-23 18:57:21 +0100
committerGitHub <noreply@github.com>2016-12-23 18:57:21 +0100
commitbc7703563f5cd034f3f30e3b07ea9f3078b3db5c (patch)
treec275740020e89d35c8666e45976bbc6d9455cd69
parent826ca604eccdf6f325c1c161e73132e765bb3855 (diff)
parentea1a743e24d264f102adf332248db98e759f4f93 (diff)
downloadpython-lxml-bc7703563f5cd034f3f30e3b07ea9f3078b3db5c.tar.gz
Merge pull request #218 from plq/master
Fix <script> and <style> text content serialization
-rw-r--r--src/lxml/serializer.pxi9
-rw-r--r--src/lxml/tests/test_incremental_xmlfile.py14
2 files changed, 22 insertions, 1 deletions
diff --git a/src/lxml/serializer.pxi b/src/lxml/serializer.pxi
index a3d22365..489ed2de 100644
--- a/src/lxml/serializer.pxi
+++ b/src/lxml/serializer.pxi
@@ -954,7 +954,14 @@ 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)
+
+ ns, name, _, _ = self._element_stack[-1]
+ if c_method == OUTPUT_METHOD_HTML and \
+ ns in (None, 'http://www.w3.org/1999/xhtml') and name in ('script', 'style'):
+ tree.xmlOutputBufferWrite(self._c_out, len(content), content)
+ else:
+ tree.xmlOutputBufferWriteEscape(self._c_out, _xcstr(content), NULL)
+
elif iselement(content):
if self._status > WRITER_IN_ELEMENT:
raise LxmlSyntaxError("cannot append trailing element to complete XML document")
diff --git a/src/lxml/tests/test_incremental_xmlfile.py b/src/lxml/tests/test_incremental_xmlfile.py
index b6245618..414edd12 100644
--- a/src/lxml/tests/test_incremental_xmlfile.py
+++ b/src/lxml/tests/test_incremental_xmlfile.py
@@ -418,6 +418,20 @@ class HtmlFileTestCase(_XmlFileTestCaseBase):
'</root>')
self._file = BytesIO()
+ def test_unescaped_script(self):
+ with etree.htmlfile(self._file) as xf:
+ elt = etree.Element('script')
+ elt.text = "if (a < b);"
+ xf.write(elt)
+ self.assertXml('<script>if (a < b);</script>')
+
+ def test_unescaped_script_incremental(self):
+ with etree.htmlfile(self._file) as xf:
+ with xf.element('script'):
+ xf.write("if (a < b);")
+
+ self.assertXml('<script>if (a < b);</script>')
+
def test_write_declaration(self):
with etree.htmlfile(self._file) as xf:
try: