[wwwsearch-commits] r43567 - wwwsearch/ClientForm/trunk

jjlee at codespeak.net jjlee at codespeak.net
Tue May 22 23:09:13 CEST 2007


Author: jjlee
Date: Tue May 22 23:09:12 2007
New Revision: 43567

Modified:
   wwwsearch/ClientForm/trunk/ClientForm.py
   wwwsearch/ClientForm/trunk/test.py
Log:
Don't merge multiple SELECT controls with the same name

Modified: wwwsearch/ClientForm/trunk/ClientForm.py
==============================================================================
--- wwwsearch/ClientForm/trunk/ClientForm.py	(original)
+++ wwwsearch/ClientForm/trunk/ClientForm.py	Tue May 22 23:09:12 2007
@@ -26,6 +26,10 @@
 
 """
 
+def hexid(x):
+    import struct
+    return hex(struct.unpack('I', struct.pack('i', id(x)))[0]).lower().strip("l")
+
 # XXX
 # SELECT outside of FORM loses last OPTION
 # add an __all__
@@ -1120,8 +1124,8 @@
         for ii in range(len(controls)):
             type, name, attrs = controls[ii]
             # index=ii*10 allows ImageControl to return multiple ordered pairs
-            form.new_control(type, name, attrs, select_default=select_default,
-                             index=ii*10)
+            form.new_control(
+                type, name, attrs, select_default=select_default, index=ii*10)
         forms.append(form)
     for form in forms:
         form.fixup()
@@ -1760,6 +1764,7 @@
         self.disabled = False
         self.readonly = False
         self.id = attrs.get("id")
+        self._closed = False
 
         # As Controls are merged in with .merge_control(), self.attrs will
         # refer to each Control in turn -- always the most recently merged
@@ -1988,6 +1993,9 @@
             "control.get(...).attrs")
         return self._get(name, by_label, nr).attrs
 
+    def close_control(self):
+        self._closed = True
+
     def add_to_form(self, form):
         assert self._form is None or form == self._form, (
             "can't add control to more than one form")
@@ -1996,12 +2004,16 @@
             # always count nameless elements as separate controls
             Control.add_to_form(self, form)
         else:
-            try:
-                control = form.find_control(self.name, self.type)
-            except (ControlNotFoundError, AmbiguityError):
-                Control.add_to_form(self, form)
+            for ii in range(len(form.controls)-1, -1, -1):
+                control = form.controls[ii]
+                if control.name == self.name and control.type == self.type:
+                    if control._closed:
+                        Control.add_to_form(self, form)
+                    else:
+                        control.merge_control(self)
+                    break
             else:
-                control.merge_control(self)
+                Control.add_to_form(self, form)
 
     def merge_control(self, control):
         assert bool(control.multiple) == bool(self.multiple)
@@ -2820,6 +2832,14 @@
             control = klass(type, name, a, select_default, index)
         else:
             control = klass(type, name, a, index)
+
+        if type == "select" and len(attrs) == 1:
+            for ii in range(len(self.controls)-1, -1, -1):
+                ctl = self.controls[ii]
+                if ctl.type == "select":
+                    ctl.close_control()
+                    break
+
         control.add_to_form(self)
         control._urlparse = self._urlparse
         control._urlunparse = self._urlunparse

Modified: wwwsearch/ClientForm/trunk/test.py
==============================================================================
--- wwwsearch/ClientForm/trunk/test.py	(original)
+++ wwwsearch/ClientForm/trunk/test.py	Tue May 22 23:09:12 2007
@@ -758,6 +758,36 @@
         ctl = forms[0].find_control(type="textarea")
         self.assertEqual(ctl.value, "\r\nblah\r\n")
 
+## # XXXX missing form elem --> complains about nested selects! (global form trouble?)
+
+    def test_double_select(self):
+        # More than one SELECT control of the same name in a form never
+        # represent a single control (unlike RADIO and CHECKBOX elements), so
+        # don't merge them.
+        forms = ClientForm.ParseFile(
+            StringIO("""\
+<form>
+    <select name="a">
+        <option>b</option>
+        <option>c</option>
+    </select>
+    <select name="a">
+        <option>d</option>
+        <option>e</option>
+    </select>
+</form>
+"""),
+            "http://example.com/",
+            backwards_compat=False,
+            )
+        form = forms[0]
+        self.assertEquals(len(form.controls), 2)
+        ctl = form.find_control(name="a", nr=0)
+        self.assertEqual([item.name for item in ctl.items], ["b", "c"])
+        ctl = form.find_control(name="a", nr=1)
+        self.assertEqual([item.name for item in ctl.items], ["d", "e"])
+
+
 
 class DisabledTests(TestCase):
     def testOptgroup(self):


More information about the wwwsearch-commits mailing list