[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/formparser. make sure we do not lose fields that are repeated

Fred L. Drake, Jr. fdrake at gmail.com
Thu Jun 30 18:45:23 EDT 2005


Log message for revision 30964:
  make sure we do not lose fields that are repeated

Changed:
  U   zope.testing/trunk/src/zope/testing/formparser.py
  U   zope.testing/trunk/src/zope/testing/formparser.txt

-=-
Modified: zope.testing/trunk/src/zope/testing/formparser.py
===================================================================
--- zope.testing/trunk/src/zope/testing/formparser.py	2005-06-30 21:30:52 UTC (rev 30963)
+++ zope.testing/trunk/src/zope/testing/formparser.py	2005-06-30 22:45:23 UTC (rev 30964)
@@ -33,7 +33,7 @@
         self._parser.handle_starttag = self._handle_starttag
         self._parser.handle_startendtag = self._handle_starttag
         self._buffer = []
-        self.current = None
+        self.current = None # current form
         self.forms = FormCollection()
 
     def parse(self):
@@ -92,9 +92,9 @@
             value = d.get("value")
             size = intattr(d, "size")
             maxlength = intattr(d, "maxlength")
-            self.current[name] = Input(name, id, type, value,
-                                       checked, disabled, readonly,
-                                       src, size, maxlength)
+            self._add_field(
+                Input(name, id, type, value, checked,
+                      disabled, readonly, src, size, maxlength))
         elif tag == "button":
             pass
         elif tag == "textarea":
@@ -105,7 +105,7 @@
                                   None, None, None)
             self.textarea.rows = intattr(d, "rows")
             self.textarea.cols = intattr(d, "cols")
-            self.current[name] = self.textarea
+            self._add_field(self.textarea)
             # The value will be set when the </textarea> is seen.
         elif tag == "base":
             href = d.get("href", "").strip()
@@ -117,7 +117,7 @@
             multiple = "multiple" in d
             size = intattr(d, "size")
             self.select = Select(name, id, disabled, multiple, size)
-            self.current[name] = self.select
+            self._add_field(self.select)
         elif tag == "option":
             disabled = "disabled" in d
             selected = "selected" in d
@@ -126,7 +126,19 @@
             option = Option(id, value, selected, label, disabled)
             self.select.options.append(option)
 
+    # Helpers:
 
+    def _add_field(self, field):
+        if field.name in self.current:
+            ob = self.current[field.name]
+            if isinstance(ob, list):
+                ob.append(field)
+            else:
+                self.current[field.name] = [ob, field]
+        else:
+            self.current[field.name] = field
+
+
 def kwattr(d, name, default=None):
     """Return attribute, converted to lowercase."""
     v = d.get(name, default)

Modified: zope.testing/trunk/src/zope/testing/formparser.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/formparser.txt	2005-06-30 21:30:52 UTC (rev 30963)
+++ zope.testing/trunk/src/zope/testing/formparser.txt	2005-06-30 22:45:23 UTC (rev 30964)
@@ -35,6 +35,8 @@
   ...     <textarea name="sometext" rows="5">Some text.</textarea>
   ...     <input type="Image" name="action" value="Do something."
   ...            src="else.png" />
+  ...     <input type="text" value="" name="multi" size="2" />
+  ...     <input type="text" value="" name="multi" size="3" />
   ...   </form>
   ... </body></html>
   ... '''
@@ -99,6 +101,17 @@
   >>> forms[1]["action"].src
   'http://www.example.com/base/else.png'
 
+Fields which are repeated are reported as lists of objects that
+represent each instance of the field::
+
+  >>> field = forms[1]["multi"]
+  >>> type(field)
+  <type 'list'>
+  >>> [o.value for o in field]
+  ['', '']
+  >>> [o.size for o in field]
+  [2, 3]
+
 The ``<textarea>`` element provides some additional attributes:
 
   >>> ta = forms[1]["sometext"]



More information about the Zope3-Checkins mailing list