[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/textwidgets.py Use saxutils instead of home-brewed encode() and unencode() method.

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Jul 13 13:18:10 EDT 2004


Log message for revision 26492:
  Use saxutils instead of home-brewed encode() and unencode() method.
  


Changed:
  U   Zope3/trunk/src/zope/app/form/browser/textwidgets.py


-=-
Modified: Zope3/trunk/src/zope/app/form/browser/textwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-07-13 17:16:12 UTC (rev 26491)
+++ Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-07-13 17:18:09 UTC (rev 26492)
@@ -15,6 +15,7 @@
 
 $Id$
 """
+from xml.sax import saxutils
 from zope.interface import implements
 
 from zope.app.form.interfaces import IInputWidget, ConversionError
@@ -24,6 +25,17 @@
 from zope.app.datetimeutils import DateTimeError
 
 
+def escape(str):
+    if str is not None:
+        str = saxutils.escape(str)
+    return str
+
+def unescape(str):
+    if str is not None:
+        str = saxutils.unescape(str)
+    return str
+
+
 class TextWidget(SimpleInputWidget):
     """Text widget.
 
@@ -141,7 +153,7 @@
                 value = unicode(input)
             except ValueError, v:
                 raise ConversionError("Invalid text data", v)
-        return decode_html(value)
+        return unescape(value)
 
 
 class Bytes(SimpleInputWidget):
@@ -262,7 +274,7 @@
             except ValueError, v:
                 raise ConversionError("Invalid unicode data", v)
             else:
-                value = decode_html(value)
+                value = unescape(value)
                 value = value.replace("\r\n", "\n")                
         return value
 
@@ -270,7 +282,7 @@
         value = super(TextAreaWidget, self)._toFormValue(value)
         if value:
             value = value.replace("\n", "\r\n")
-            value = encode_html(value)
+            value = escape(value)
         return value
 
     def __call__(self):
@@ -430,21 +442,3 @@
                 return parseDatetimetz(input).date()
             except (DateTimeError, ValueError, IndexError), v:
                 raise ConversionError("Invalid datetime data", v)
-
-
-def encode_html(text):
-    if text:
-        text = text.replace('&', '&')
-        text = text.replace('<', '&lt;')
-        text = text.replace('>', '&gt;')
-        text = text.replace('"', '&quot;')
-    return text
-
-
-def decode_html(text):
-    if text:
-        text = text.replace('&amp;', '&')
-        text = text.replace('&lt;', '<')
-        text = text.replace('&gt;', '>')
-        text = text.replace('&quot;', '"')
-    return text



More information about the Zope3-Checkins mailing list