[wwwsearch-commits] r28914 - wwwsearch/ClientForm/trunk
jjlee at codespeak.net
jjlee at codespeak.net
Sat Jun 17 14:34:21 CEST 2006
Author: jjlee
Date: Sat Jun 17 14:34:19 2006
New Revision: 28914
Modified:
wwwsearch/ClientForm/trunk/ClientForm.py
wwwsearch/ClientForm/trunk/test.py
Log:
Fix list control (they used to get inappropriately .merge_control()ed with other controls, or parsing would raise AmbiguityError); Add Missing class to allow explicitly finding a nameless control using .find_control() (not yet documented)
Modified: wwwsearch/ClientForm/trunk/ClientForm.py
==============================================================================
--- wwwsearch/ClientForm/trunk/ClientForm.py (original)
+++ wwwsearch/ClientForm/trunk/ClientForm.py Sat Jun 17 14:34:19 2006
@@ -41,11 +41,6 @@
# Does file upload work when name is missing? Sourceforge tracker form
# doesn't like it. Check standards, and test with Apache. Test
# binary upload with Apache.
-# Controls can have name=None (e.g. forms constructed partly with
-# JavaScript), but find_control can't be told to find a control
-# with that name, because None there means 'unspecified'. Can still
-# get at by nr, but would be nice to be able to specify something
-# equivalent to name=None, too.
# mailto submission & enctype text/plain
# I'm not going to fix this unless somebody tells me what real servers
# that want this encoding actually expect: If enctype is
@@ -127,6 +122,8 @@
DEFAULT_ENCODING = "latin-1"
+class Missing: pass
+
_compress_re = re.compile(r"\s+")
def compress_text(text): return _compress_re.sub(" ", text.strip())
@@ -1853,12 +1850,16 @@
assert self._form is None or form == self._form, (
"can't add control to more than one form")
self._form = form
- try:
- control = form.find_control(self.name, self.type)
- except ControlNotFoundError:
+ if self.name is None:
+ # always count nameless elements as separate controls
Control.add_to_form(self, form)
else:
- control.merge_control(self)
+ try:
+ control = form.find_control(self.name, self.type)
+ except (ControlNotFoundError, AmbiguityError):
+ Control.add_to_form(self, form)
+ else:
+ control.merge_control(self)
def merge_control(self, control):
assert bool(control.multiple) == bool(self.multiple)
@@ -3050,7 +3051,8 @@
is_listcontrol, nr)
def _find_control(self, name, type, kind, id, label, predicate, nr):
- if (name is not None) and not isstringlike(name):
+ if ((name is not None) and (name is not Missing) and
+ not isstringlike(name)):
raise TypeError("control name must be string-like")
if (type is not None) and not isstringlike(type):
raise TypeError("control type must be string-like")
@@ -3072,7 +3074,8 @@
nr = 0
for control in self.controls:
- if name is not None and name != control.name:
+ if ((name is not None and name != control.name) and
+ (name is not Missing or control.name is not None)):
continue
if type is not None and type != control.type:
continue
@@ -3102,7 +3105,7 @@
return found
description = []
- if name is not None: description.append("name '%s'" % name)
+ if name is not None: description.append("name %s" % repr(name))
if type is not None: description.append("type '%s'" % type)
if kind is not None: description.append("kind '%s'" % kind)
if id is not None: description.append("id '%s'" % id)
Modified: wwwsearch/ClientForm/trunk/test.py
==============================================================================
--- wwwsearch/ClientForm/trunk/test.py (original)
+++ wwwsearch/ClientForm/trunk/test.py Sat Jun 17 14:34:19 2006
@@ -499,7 +499,7 @@
form = forms[0]
self.assert_(form.controls[0].name is None)
- def testNamelessListControls(self):
+ def testNamelessListItems(self):
# XXX SELECT
# these controls have no item names
file = StringIO("""<form action="./weird.html">
@@ -2012,6 +2012,22 @@
else:
self.assertRaises(AmbiguityError, fc, label="Book")
+ def test_find_nameless_control(self):
+ data = """\
+<form>
+ <input type="checkbox"/>
+ <input type="checkbox" id="a" onclick="blah()"/>
+</form>
+"""
+ f = StringIO(data)
+ form = ClientForm.ParseFile(f, "http://example.com/",
+ backwards_compat=False)[0]
+ self.assertRaises(
+ AmbiguityError,
+ form.find_control, type="checkbox", name=ClientForm.Missing)
+ ctl = form.find_control(type="checkbox", name=ClientForm.Missing, nr=1)
+ self.assertEqual(ctl.id, "a")
+
def test_deselect_disabled(self):
def get_new_form(f, compat):
f.seek(0)
@@ -2974,6 +2990,40 @@
if compat:
reset_deprecations()
+ def test_nameless_list_control(self):
+ # ListControls are built up from elements that match by name and type
+ # attributes. Nameless controls cause some tricky cases. We should
+ # get a new control for nameless controls.
+ for data in [
+ """\
+<form>
+ <input type="checkbox" name="foo"/>
+ <input type="checkbox" name="bar"/>
+ <input type="checkbox" id="a" onclick="bar()"/>
+</form>
+""",
+"""\
+<form>
+ <input type="checkbox" name="foo"/>
+ <input type="checkbox" id="a" onclick="bar()"/>
+</form>
+""",
+"""\
+<form>
+ <input type="checkbox"/>
+ <input type="checkbox"/>
+ <input type="checkbox" id="a" onclick="bar()"/>
+</form>
+""",
+ ]:
+ f = StringIO(data)
+ form = ClientForm.ParseFile(f, "http://example.com/",
+ backwards_compat=False)[0]
+ bar = form.find_control(type="checkbox", id="a")
+ # should have value "on", but not be successful
+ self.assertEqual([item.name for item in bar.items], ["on"])
+ self.assertEqual(bar.value, [])
+
class ContentTypeTests(TestCase):
def test_content_type(self):
More information about the wwwsearch-commits
mailing list