[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ add a URL-specific display widget that generates a hyperlink

Fred L. Drake, Jr. fdrake at gmail.com
Wed Nov 24 13:57:40 EST 2004


Log message for revision 28500:
  add a URL-specific display widget that generates a hyperlink

Changed:
  U   Zope3/trunk/src/zope/app/form/browser/configure.zcml
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
  U   Zope3/trunk/src/zope/app/form/browser/textwidgets.py

-=-
Modified: Zope3/trunk/src/zope/app/form/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/configure.zcml	2004-11-24 18:16:35 UTC (rev 28499)
+++ Zope3/trunk/src/zope/app/form/browser/configure.zcml	2004-11-24 18:57:40 UTC (rev 28500)
@@ -70,6 +70,14 @@
       permission="zope.Public"
       />
 
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IURI"
+      provides="zope.app.form.interfaces.IDisplayWidget"
+      factory=".textwidgets.URIDisplayWidget"
+      permission="zope.Public"
+      />
+
   <!-- Core edit widgets -->
 
   <view

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2004-11-24 18:16:35 UTC (rev 28499)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_textwidget.py	2004-11-24 18:57:40 UTC (rev 28500)
@@ -23,7 +23,9 @@
 
 from zope.app.form.interfaces import IInputWidget
 from zope.app.form.browser import TextWidget
+from zope.app.form.browser.textwidgets import URIDisplayWidget
 from zope.app.tests.placelesssetup import setUp, tearDown
+from zope.app.form.browser.tests.test_browserwidget import BrowserWidgetTest
 from zope.app.form.browser.tests.test_browserwidget import SimpleInputWidgetTest
 
 
@@ -116,9 +118,28 @@
         self._widget.extra = 'style="color: red"'
         self.verifyResult(self._widget.hidden(), check_list)
 
+
+class URIDisplayWidgetTest(BrowserWidgetTest):
+
+    _WidgetFactory = URIDisplayWidget
+
+    def testProperties(self):
+        # check the default linkTarget
+        self.failIf(self._widget.linkTarget)
+
+    def testRender(self):
+        value = "uri:fake"
+        self._widget.setRenderedValue(value)
+        self.verifyResult(self._widget(), ["<a", 'href="uri:fake"'])
+        self._widget.linkTarget = "there"
+        self.verifyResult(self._widget(), ["<a", 'href="uri:fake"',
+                                           'target="there"'])
+
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(TextWidgetTest),
+        unittest.makeSuite(URIDisplayWidgetTest),
         doctest.DocTestSuite(),
         ))
 

Modified: Zope3/trunk/src/zope/app/form/browser/textwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-11-24 18:16:35 UTC (rev 28499)
+++ Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2004-11-24 18:57:40 UTC (rev 28500)
@@ -207,6 +207,30 @@
     Single-line data (string) input
     """
 
+
+class URIDisplayWidget(DisplayWidget):
+    """URI display widget.
+
+    :ivar linkTarget:
+      The value of the ``target`` attribute for the generated hyperlink.
+      If this is not set, no ``target`` attribute is generated.
+
+    """
+
+    linkTarget = None
+
+    def __call__(self):
+        if self._renderedValueSet():
+            content = self._data
+        else:
+            content = self.context.default
+        content = escape(content)
+        kw = dict(contents=content, href=content)
+        if self.linkTarget:
+            kw["target"] = self.linkTarget
+        return renderElement("a", **kw)
+
+
 class TextAreaWidget(SimpleInputWidget):
     """TextArea widget.
 



More information about the Zope3-Checkins mailing list