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

jjlee at codespeak.net jjlee at codespeak.net
Sat Oct 8 16:11:58 CEST 2005


Author: jjlee
Date: Sat Oct  8 16:11:57 2005
New Revision: 18286

Modified:
   wwwsearch/ClientForm/trunk/ClientForm.py
   wwwsearch/ClientForm/trunk/test.py
Log:
Ugly hack to make label matching work properly in backwards-compatible mode; Clarify _getLabel(); Fix testDisabledCheckbox

Modified: wwwsearch/ClientForm/trunk/ClientForm.py
==============================================================================
--- wwwsearch/ClientForm/trunk/ClientForm.py	(original)
+++ wwwsearch/ClientForm/trunk/ClientForm.py	Sat Oct  8 16:11:57 2005
@@ -927,18 +927,35 @@
 class Label:
     def __init__(self, attrs):
         self.id = attrs.get("for")
-        self.text = compress_text(attrs.get("__text"))
+        self._text = attrs.get("__text").strip()
+        self._ctext = compress_text(self._text)
         self.attrs = attrs
+        self._backwards_compat = False  # maintaned by HTMLForm
+
+    def __getattr__(self, name):
+        if name == "text":
+            if self._backwards_compat:
+                return self._text
+            else:
+                return self._ctext
+        return getattr(Label, name)
+
+    def __setattr__(self, name, value):
+        if name == "text":
+            # don't see any need for this
+            raise AttributeError("text attribute is read-only")
+        self.__dict__[name] = value
 
     def __str__(self):
         return '<Label(id=%r, text=%r)>' % (self.id, self.text)
 
 
 def _getLabel(attrs):
-    label = attrs.get("__label")
-    if label is not None:
-        label = Label(label)
-    return label
+    text = attrs.get("__label")
+    if text is not None:
+        return Label(text)
+    else:
+        return None
 
 class Control:
     """An HTML form control.
@@ -2477,10 +2494,34 @@
             self.attrs = {}
         self.controls = []
         self._request_class = request_class
+
+        # these attributes are used by zope.testbrowser
         self._forms = forms  # this is a semi-public API!
         self._labels = labels  # this is a semi-public API!
         self._id_to_labels = id_to_labels  # this is a semi-public API!
-        self.backwards_compat = backwards_compat
+
+        self.backwards_compat = backwards_compat  # note __setattr__
+
+    def __getattr__(self, name):
+        if name == "backwards_compat":
+            return self._backwards_compat
+        return getattr(HTMLForm, name)
+
+    def __setattr__(self, name, value):
+        # yuck
+        if name == "backwards_compat":
+            name = "_backwards_compat"
+            value = bool(value)
+            for cc in self.controls:
+                try:
+                    items = cc.items 
+                except AttributeError:
+                    continue
+                else:
+                    for ii in items:
+                        for ll in ii.get_labels():
+                            ll._backwards_compat = value
+        self.__dict__[name] = value
 
     def new_control(self, type, name, attrs,
                     ignore_unknown=False, select_default=False):
@@ -2530,6 +2571,7 @@
         """
         for control in self.controls:
             control.fixup()
+        self.backwards_compat = self._backwards_compat
 
 #---------------------------------------------------
     def __str__(self):

Modified: wwwsearch/ClientForm/trunk/test.py
==============================================================================
--- wwwsearch/ClientForm/trunk/test.py	(original)
+++ wwwsearch/ClientForm/trunk/test.py	Sat Oct  8 16:11:57 2005
@@ -822,7 +822,7 @@
 
     def testDisabledCheckbox(self):
         for compat in False, True:
-            self._testDisabledCheckbox(self)
+            self._testDisabledCheckbox(compat)
     def _testDisabledCheckbox(self, compat):
         file = StringIO(
 """<form action="abc" name="myform">
@@ -1769,7 +1769,7 @@
         c1.value = ['value_value']
         self.failUnless(c1.items[0].selected)
         self.assertEqual(c1.value, ['value_value'])
-        
+
         # id labels
         form._id_to_labels['name_value_1'] = [
             ClientForm.Label({'for': 'name_value_1', '__text':'First Option'})]
@@ -2713,6 +2713,29 @@
         # nr too high again
         self.assertRaises(ItemNotFoundError, ctl.get, label="a", nr=3)
 
+    def test_label_whitespace(self):
+        for compat in [False, True]:
+            f = StringIO("""\
+<form>
+    <select multiple name="eg">
+        <option value="p"> a b  c  </option>
+        <option value="q">b</option>
+    </select>
+</form>
+""")
+            if compat:
+                hide_deprecations()
+            form = ClientForm.ParseFile(f, "http://example.com/",
+                                        backwards_compat=compat)[0]
+            ctl = form.find_control("eg")
+            p = ctl.get("p")
+            q = ctl.get("q")
+            self.assertEqual(p.get_labels()[0].text,
+                             (compat and "a b  c" or "a b c"))
+            self.assertEqual(q.get_labels()[0].text, "b")
+            if compat:
+                reset_deprecations()
+
 
 def startswith(string, initial):
     if len(initial) > len(string): return False


More information about the wwwsearch-commits mailing list