[Zope3-checkins] CVS: Zope3/src/zope/app/browser/component - interfacewidget.py:1.9

Steve Alexander steve@cat-box.net
Tue, 7 Jan 2003 12:56:05 -0500


Update of /cvs-repository/Zope3/src/zope/app/browser/component
In directory cvs.zope.org:/tmp/cvs-serv4045/src/zope/app/browser/component

Modified Files:
	interfacewidget.py 
Log Message:
Some bugs fixed.
We should be quoting attributes such as search_string.
There are some XXXs here for codepaths and things that need tests.
I'll write them eventually, if no-one else does.


=== Zope3/src/zope/app/browser/component/interfacewidget.py 1.8 => 1.9 ===
--- Zope3/src/zope/app/browser/component/interfacewidget.py:1.8	Tue Jan  7 12:21:57 2003
+++ Zope3/src/zope/app/browser/component/interfacewidget.py	Tue Jan  7 12:56:01 2003
@@ -16,14 +16,16 @@
 $Id$
 """
 
+import sys
 from zope.interface import Interface
 from zope.app.interfaces.browser.form import IBrowserWidget
-from zope.app.interfaces.forms import WidgetInputError
+from zope.app.interfaces.forms import WidgetInputError, ConversionError
 from zope.app.form.widget import Widget
 from zope.publisher.browser import BrowserView
 from zope.component import getService
 from zope.schema.interfaces import ValidationError
 from zope.component.exceptions import ComponentLookupError
+from xml.sax.saxutils import quoteattr
 
 class InterfaceWidget(Widget, BrowserView):
     __implements__ = IBrowserWidget
@@ -37,19 +39,19 @@
     def getData(self, optional=0):
         field = self.context
         value = self.request.form.get(self.name, self) # self used as marker
-        if value is self:
+        if value is self or value == '':
             # No user input
             if field.required and not optional:
                 raise MissingInputError(field.__name__, field.title,
                                         'the field is required')
             return field.default
-
         if value == 'None':
             value = None
         else:
             try:
                 value = nameToInterface(field, value)
             except ComponentLookupError:
+                # XXX this code path needs a test!
                 # Convert to conversion error
                 exc = ConversionError(sys.exc_info()[1])
                 raise ConversionError, exc, sys.exc_info()[2]
@@ -79,7 +81,10 @@
         interfaces = list(service.searchInterface(search_string, base=base))
         interfaces.sort()
         interfaces = map(interfaceToName, interfaces)
-        if include_none:
+        # Only include None if there is no search string, and include_none
+        # is True
+        # XXX need test for this
+        if include_none and not search_string:
             interfaces = ['None'] + interfaces
 
         if self._data is None:
@@ -166,6 +171,7 @@
             values = tuple([nameToInterface(field, value) for value in values])
         except ComponentLookupError:
             # Convert to conversion error
+            # XXX this code path needs to be tested!
             exc = ConversionError(sys.exc_info()[1])
             raise ConversionError, exc, sys.exc_info()[2]
 
@@ -259,7 +265,7 @@
             interfaces = list(service.searchInterface(search, base=base))
             interfaces.sort()
             interfaces = map(interfaceToName, interfaces)
-            if include_none:
+            if include_none and not search:
                 interfaces = ['None'] + interfaces
             search_name = '%s.search.i%s' % (name, count)
             rendered_selections.append(
@@ -335,9 +341,9 @@
                               interface == selected and ' selected' or '',
                               interface)
                            )
-
-    search_field = '<input type="text" name="%s" value="%s">' % (
-        search_name, search_string)
+    # XXX need unit test for use of quoteattr for search string
+    search_field = '<input type="text" name="%s" value=%s>' % (
+        search_name, quoteattr(search_string))
     select_field = '<select name="%s">%s</select>'  % (
         select_name, ''.join(options))