Skip to content

Commit

Permalink
Remove check for ']]>' in CDATA class, add test (GH-421)
Browse files Browse the repository at this point in the history
CDATA containing CDEnd (']]>') is handled by libxml2 by splitting it into ']]' and '>'.
It is no longer necessary to disallow CDend.
  • Loading branch information
gertjanklein committed May 11, 2024
1 parent d226c07 commit 13df978
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/lxml/etree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3144,10 +3144,7 @@ cdef class CDATA:
"""
cdef bytes _utf8_data
def __cinit__(self, data):
_utf8_data = _utf8(data)
if b']]>' in _utf8_data:
raise ValueError, "']]>' not allowed inside CDATA"
self._utf8_data = _utf8_data
self._utf8_data = _utf8(data)


def Entity(name):
Expand Down
14 changes: 14 additions & 0 deletions src/tests/test_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,20 @@ def test_cdata_xpath(self):

self.assertEqual(['test'], root.xpath('//text()'))

def test_cdata_split_cdend(self):
# Tests that existing ']]>' in CDATA is split to 'escape' it
CDATA = self.etree.CDATA
Element = self.etree.Element
tostring = self.etree.tostring

root = Element("root")
root.text = CDATA('test]]>')

self.assertEqual('test]]>',
root.text)
self.assertEqual(b'<root><![CDATA[test]]]]><![CDATA[>]]></root>',
tostring(root))

# TypeError in etree, AssertionError in ElementTree;
def test_setitem_assert(self):
Element = self.etree.Element
Expand Down

0 comments on commit 13df978

Please sign in to comment.