[Python-Dev] Escaping CDATA
Ka-Ping Yee
ping@lfw.org
Mon, 17 Apr 2000 18:08:53 -0500 (CDT)
Here's what i'm playing with, if you want to mess with it too:
import string
def replace(text, old, new, join=string.join, split=string.split):
return join(split(text, old), new)
la, ra = "@@", "@@]"
lb, rb = "]]>", "@@>"
la, ra = "<]", "<]>"
lb, rb = "]]>", "<]["
def escape(text):
cdata = replace(text, la, ra)
cdata = replace(cdata, lb, rb)
return cdata
def unescape(cdata):
text = replace(cdata, rb, lb)
text = replace(text, ra, la)
return text
chars = ""
for ch in la + ra + lb + rb:
if ch not in chars: chars = chars + ch
if __name__ == "__main__":
class Tester:
def __init__(self):
self.failed = []
self.count = 0
def test(self, s, find=string.find):
cdata = escape(s)
text = unescape(cdata)
print "%s -e-> %s -u-> %s" % (s, cdata, text)
if find(cdata, "]]>") >= 0:
print "EXPOSURE!"
self.failed.append(s)
elif s != text:
print "MISMATCH!"
self.failed.append(s)
self.count = self.count + 1
tester = Tester()
test = tester.test
for a in chars:
for b in chars:
for c in chars:
for d in chars:
for e in chars:
for f in chars:
for g in chars:
for h in chars:
test(a+b+c+d+e+f+g+h)
print
if tester.failed == []: print "All tests succeeded."
else:
print "Failed %d of %d tests." % (len(tester.failed), tester.count)
for t in tester.failed: tester.test(t)