[Zope3-checkins] SVN: Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/ Sync HTML quoting behaviour with Zope 2 DTML:

Philipp von Weitershausen philikon at philikon.de
Tue Jan 10 12:05:08 EST 2006


Log message for revision 41248:
  Sync HTML quoting behaviour with Zope 2 DTML:
  - simply use cgi.escape for the HTML escaping
  - use the ugly, but working 'ustr' module (a straight copy from Zope 2)
  

Changed:
  U   Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/dt_util.py
  A   Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/tests/testustr.py
  A   Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/ustr.py

-=-
Modified: Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/dt_util.py
===================================================================
--- Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/dt_util.py	2006-01-10 12:45:35 UTC (rev 41247)
+++ Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/dt_util.py	2006-01-10 17:05:08 UTC (rev 41248)
@@ -18,13 +18,14 @@
 import re
 
 from types import ListType, StringType, TupleType
+from cgi import escape
 
 # These imports are for the use of clients of this module, as this
 # module is the canonical place to get them. 
 from zope.documenttemplate.pdocumenttemplate import TemplateDict, InstanceDict
 from zope.documenttemplate.pdocumenttemplate import render_blocks
+from zope.documenttemplate.ustr import ustr
 
-
 class ParseError(Exception):
     '''Document Template Parse Error'''
 
@@ -32,17 +33,9 @@
     '''Unauthorized'''
 
 
-def html_quote(v, name='(Unknown name)', md={},
-               character_entities=(
-                       (('&'),    '&'),
-                       (('<'),    '&lt;' ),
-                       (('>'),    '&gt;' ),
-                       (('"'),    '&quot;'))): #"
-    text = str(v)
-    for re, name in character_entities:
-        text = text.replace(re, name)
-    return text
 
+def html_quote(v, name='(Unknown name)', md={}):
+    return escape(ustr(v), 1)
 
 def int_param(params, md, name, default=0):
     try:

Copied: Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/tests/testustr.py (from rev 41247, Zope/trunk/lib/python/DocumentTemplate/tests/testustr.py)
===================================================================
--- Zope/trunk/lib/python/DocumentTemplate/tests/testustr.py	2006-01-10 12:45:35 UTC (rev 41247)
+++ Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/tests/testustr.py	2006-01-10 17:05:08 UTC (rev 41248)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""ustr unit tests.
+
+$Id$
+"""
+
+import unittest
+
+from zope.documenttemplate.ustr import ustr
+
+
+class force_str:
+    # A class whose string representation is not always a plain string:
+    def __init__(self,s):
+        self.s = s
+    def __str__(self):
+        return self.s
+
+
+class Foo(str):
+
+    pass
+
+
+class Bar(unicode):
+
+    pass
+
+
+class UnicodeTests(unittest.TestCase):
+
+    def testPlain(self):
+        a = ustr('hello')
+        assert a=='hello', `a`
+        a = ustr(force_str('hello'))
+        assert a=='hello', `a`
+        a = ustr(chr(200))
+        assert a==chr(200), `a`
+        a = ustr(force_str(chr(200)))
+        assert a==chr(200), `a`
+        a = ustr(22)
+        assert a=='22', `a`
+        a = ustr([1,2,3])
+        assert a=='[1, 2, 3]', `a`
+
+    def testUnicode(self):
+        a = ustr(u'hello')
+        assert a=='hello', `a`
+        a = ustr(force_str(u'hello'))
+        assert a=='hello', `a`
+        a = ustr(unichr(200))
+        assert a==unichr(200), `a`
+        a = ustr(force_str(unichr(200)))
+        assert a==unichr(200), `a`
+
+    def testExceptions(self):
+        a = ustr(ValueError(unichr(200)))
+        assert a==unichr(200), `a`
+
+    def testCustomStrings(self):
+        a = ustr(Foo('foo'))
+        self.failUnlessEqual(type(a), Foo)
+        a = ustr(Bar('bar'))
+        self.failUnlessEqual(type(a), Bar)
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest( unittest.makeSuite( UnicodeTests ) )
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Copied: Zope3/branches/philikon-dtml-sync/src/zope/documenttemplate/ustr.py (from rev 41247, Zope/trunk/lib/python/DocumentTemplate/ustr.py)



More information about the Zope3-Checkins mailing list