ElementTree._escape_cdata isn't reliable across Python versions.
It also does not work as desired. Revert "Merge pull request #254 from barreverte/develop" This reverts commit23750357e2
, reversing changes made to07284f380f
.
This commit is contained in:
parent
19b24b276d
commit
ad7a57103d
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,4 +12,3 @@ sleekxmpp.egg-info/
|
|||||||
*~
|
*~
|
||||||
.baboon/
|
.baboon/
|
||||||
.DS_STORE
|
.DS_STORE
|
||||||
*.iml
|
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from xml.etree.ElementTree import _escape_cdata, _escape_attrib
|
|
||||||
|
|
||||||
if sys.version_info < (3, 0):
|
if sys.version_info < (3, 0):
|
||||||
import types
|
import types
|
||||||
@ -141,12 +140,33 @@ def tostring(xml=None, xmlns='', stream=None, outbuffer='',
|
|||||||
|
|
||||||
|
|
||||||
def escape(text, use_cdata=False):
|
def escape(text, use_cdata=False):
|
||||||
encoding = 'utf-8'
|
"""Convert special characters in XML to escape sequences.
|
||||||
|
|
||||||
if use_cdata:
|
:param string text: The XML text to convert.
|
||||||
return _escape_cdata(text, encoding)
|
:rtype: Unicode string
|
||||||
|
"""
|
||||||
|
if sys.version_info < (3, 0):
|
||||||
|
if type(text) != types.UnicodeType:
|
||||||
|
text = unicode(text, 'utf-8', 'ignore')
|
||||||
|
|
||||||
text = _escape_attrib(text, encoding)
|
escapes = {'&': '&',
|
||||||
if "'" in text:
|
'<': '<',
|
||||||
text = text.replace("'", "'")
|
'>': '>',
|
||||||
return text
|
"'": ''',
|
||||||
|
'"': '"'}
|
||||||
|
|
||||||
|
if not use_cdata:
|
||||||
|
text = list(text)
|
||||||
|
for i, c in enumerate(text):
|
||||||
|
text[i] = escapes.get(c, c)
|
||||||
|
return ''.join(text)
|
||||||
|
else:
|
||||||
|
escape_needed = False
|
||||||
|
for c in text:
|
||||||
|
if c in escapes:
|
||||||
|
escape_needed = True
|
||||||
|
break
|
||||||
|
if escape_needed:
|
||||||
|
escaped = map(lambda x : "<![CDATA[%s]]>" % x, text.split("]]>"))
|
||||||
|
return "<![CDATA[]]]><![CDATA[]>]]>".join(escaped)
|
||||||
|
return text
|
||||||
|
@ -34,7 +34,8 @@ class TestToString(SleekTest):
|
|||||||
desired = """<foo bar="baz">'Hi"""
|
desired = """<foo bar="baz">'Hi"""
|
||||||
desired += """ & welcome!'</foo>"""
|
desired += """ & welcome!'</foo>"""
|
||||||
|
|
||||||
self.assertEqual(escaped, desired)
|
self.failUnless(escaped == desired,
|
||||||
|
"XML escaping did not work: %s." % escaped)
|
||||||
|
|
||||||
def testEmptyElement(self):
|
def testEmptyElement(self):
|
||||||
"""Test converting an empty element to a string."""
|
"""Test converting an empty element to a string."""
|
||||||
|
Loading…
Reference in New Issue
Block a user