summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurak Arslan <burak.arslan@arskom.com.tr>2016-12-23 09:26:42 +0300
committerBurak Arslan <burak.arslan@arskom.com.tr>2016-12-23 09:26:42 +0300
commitea1a743e24d264f102adf332248db98e759f4f93 (patch)
treec275740020e89d35c8666e45976bbc6d9455cd69
parent826ca604eccdf6f325c1c161e73132e765bb3855 (diff)
downloadpython-lxml-ea1a743e24d264f102adf332248db98e759f4f93.tar.gz
fix <script> text content serialization in incremental html mode
-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: