diff options
-rw-r--r-- | src/lxml/serializer.pxi | 9 | ||||
-rw-r--r-- | src/lxml/tests/test_incremental_xmlfile.py | 14 |
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: |