Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/html/form_inputs.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@
<button name="action" type="submit" value="activate">Activate</button>
</form>

<input name="foo" type="text" value="early" form="outer_inputs_form">
<form method="POST" id="outer_inputs_form">
<input name="bar" type="text" value="bar">
</form>
<input name="button" type="submit" value="text" form="outer_inputs_form">

</body>
</html>
26 changes: 18 additions & 8 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def test_button_submit_by_value_and_index(self):
form.submit, "action", value="activate",
index=0)

def test_outer_inputs(self):
form = self.callFUT(formid='outer_inputs_form')
self.assertEqual(('foo', 'bar', 'button'), tuple(form.fields))

class TestResponseFormAttribute(unittest.TestCase):

Expand Down Expand Up @@ -285,26 +288,33 @@ def test_textarea_emptyfirstline(self):
class TestFormLint(unittest.TestCase):

def test_form_lint(self):
form = webtest.Form(None, '''<form>
def _build_response(html):
return webtest.TestResponse('<body>{}</body>'.format(html))

html = '''<form>
<input type="text" name="field"/>
</form>''')
</form>'''
form = webtest.Form(_build_response(html), html)
self.assertRaises(AttributeError, form.lint)

form = webtest.Form(None, '''<form>
html = '''<form>
<input type="text" id="myfield" name="field"/>
</form>''')
</form>'''
form = webtest.Form(_build_response(html), html)
self.assertRaises(AttributeError, form.lint)

form = webtest.Form(None, '''<form>
html = '''<form>
<label for="myfield">my field</label>
<input type="text" id="myfield" name="field"/>
</form>''')
</form>'''
form = webtest.Form(_build_response(html), html)
form.lint()

form = webtest.Form(None, '''<form>
html = '''<form>
<label class="field" for="myfield" role="r">my field</label>
<input type="text" id="myfield" name="field"/>
</form>''')
</form>'''
form = webtest.Form(_build_response(html), html)
form.lint()


Expand Down
7 changes: 6 additions & 1 deletion webtest/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ def _parse_fields(self):
fields = OrderedDict()
field_order = []
tags = ('input', 'select', 'textarea', 'button')
for pos, node in enumerate(self.html.find_all(tags)):
inner_elts = self.html.find_all(tags)
def _form_elt_filter(tag):
return tag in inner_elts or (
tag.attrs.get('form') == self.id and tag.name in tags)
elements = self.response.html.find_all(_form_elt_filter)
for pos, node in enumerate(elements):
attrs = dict(node.attrs)
tag = node.name
name = None
Expand Down