[Zope3-checkins] CVS: Zope3/src/zope/app/form/browser -
textwidgets.py:1.2
Stephan Richter
srichter at cosmos.phy.tufts.edu
Wed Mar 17 19:49:38 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/form/browser
In directory cvs.zope.org:/tmp/cvs-serv5052/src/zope/app/form/browser
Modified Files:
textwidgets.py
Log Message:
Added support for encoding and decoding HTML elements and entities, so that we
can edit HTML using widgets. Fixes issue 126.
Re-added doctests for browser text widgets.
=== Zope3/src/zope/app/form/browser/textwidgets.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/form/browser/textwidgets.py:1.1 Wed Mar 17 12:35:02 2004
+++ Zope3/src/zope/app/form/browser/textwidgets.py Wed Mar 17 19:49:07 2004
@@ -72,6 +72,23 @@
value="Barry"
/>
+ Check that HTML is correctly encoded and decoded:
+
+ >>> request = TestRequest(
+ ... form={'field.foo': u'<h1>&copy;</h1>'})
+ >>> widget = TextWidget(field, request)
+ >>> widget.getInputValue()
+ u'<h1>©</h1>'
+
+ >>> print normalize( widget() )
+ <input
+ class="textType"
+ id="field.foo"
+ name="field.foo"
+ size="20"
+ type="text"
+ value="<h1>&copy;</h1>"
+ />
"""
implements(IInputWidget)
@@ -112,6 +129,13 @@
size=self.displayWidth,
extra=self.extra)
+ def _convert(self, value):
+ value = super(TextWidget, self)._convert(value)
+ if value:
+ value = decode_html(value)
+ return value
+
+
class Bytes(BrowserWidget):
def _convert(self, value):
@@ -200,6 +224,22 @@
>Hey\r
dude!</textarea>
+ Check that HTML is correctly encoded and decoded:
+
+ >>> request = TestRequest(
+ ... form={'field.foo': u'<h1>&copy;</h1>'})
+ >>> widget = TextAreaWidget(field, request)
+ >>> widget.getInputValue()
+ u'<h1>©</h1>'
+
+ >>> print normalize( widget() )
+ <textarea
+ cols="60"
+ id="field.foo"
+ name="field.foo"
+ rows="15"
+ ><h1>&copy;</h1></textarea>
+
"""
implements(IInputWidget)
@@ -214,12 +254,14 @@
value = super(TextAreaWidget, self)._convert(value)
if value:
value = value.replace("\r\n", "\n")
+ value = decode_html(value)
return value
def _unconvert(self, value):
value = super(TextAreaWidget, self)._unconvert(value)
if value:
value = value.replace("\n", "\r\n")
+ value = encode_html(value)
return value
def __call__(self):
@@ -398,3 +440,17 @@
except (DateTimeError, ValueError, IndexError), v:
raise ConversionError("Invalid datetime data", v)
+
+def encode_html(text):
+ text = text.replace('&', '&')
+ text = text.replace('<', '<')
+ text = text.replace('>', '>')
+ text = text.replace('"', '"')
+ return text
+
+def decode_html(text):
+ text = text.replace('&', '&')
+ text = text.replace('<', '<')
+ text = text.replace('>', '>')
+ text = text.replace('"', '"')
+ return text
More information about the Zope3-Checkins
mailing list