Skip to content

Commit fae253d

Browse files
authored
Merge pull request #267 from JohaJung/html5_form_attr
support HTML5 `form` attribute
2 parents 599371f + 12c147c commit fae253d

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

tests/html/form_inputs.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@
5555
<button name="action" type="submit" value="activate">Activate</button>
5656
</form>
5757

58+
<input name="foo" type="text" value="early" form="outer_inputs_form">
59+
<form method="POST" id="outer_inputs_form">
60+
<input name="bar" type="text" value="bar">
61+
</form>
62+
<input name="button" type="submit" value="text" form="outer_inputs_form">
63+
5864
</body>
5965
</html>

tests/test_forms.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ def test_button_submit_by_value_and_index(self):
134134
form.submit, "action", value="activate",
135135
index=0)
136136

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

138141
class TestResponseFormAttribute(unittest.TestCase):
139142

@@ -285,26 +288,33 @@ def test_textarea_emptyfirstline(self):
285288
class TestFormLint(unittest.TestCase):
286289

287290
def test_form_lint(self):
288-
form = webtest.Form(None, '''<form>
291+
def _build_response(html):
292+
return webtest.TestResponse('<body>{}</body>'.format(html))
293+
294+
html = '''<form>
289295
<input type="text" name="field"/>
290-
</form>''')
296+
</form>'''
297+
form = webtest.Form(_build_response(html), html)
291298
self.assertRaises(AttributeError, form.lint)
292299

293-
form = webtest.Form(None, '''<form>
300+
html = '''<form>
294301
<input type="text" id="myfield" name="field"/>
295-
</form>''')
302+
</form>'''
303+
form = webtest.Form(_build_response(html), html)
296304
self.assertRaises(AttributeError, form.lint)
297305

298-
form = webtest.Form(None, '''<form>
306+
html = '''<form>
299307
<label for="myfield">my field</label>
300308
<input type="text" id="myfield" name="field"/>
301-
</form>''')
309+
</form>'''
310+
form = webtest.Form(_build_response(html), html)
302311
form.lint()
303312

304-
form = webtest.Form(None, '''<form>
313+
html = '''<form>
305314
<label class="field" for="myfield" role="r">my field</label>
306315
<input type="text" id="myfield" name="field"/>
307-
</form>''')
316+
</form>'''
317+
form = webtest.Form(_build_response(html), html)
308318
form.lint()
309319

310320

webtest/forms.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,12 @@ def _parse_fields(self):
432432
fields = OrderedDict()
433433
field_order = []
434434
tags = ('input', 'select', 'textarea', 'button')
435-
for pos, node in enumerate(self.html.find_all(tags)):
435+
inner_elts = self.html.find_all(tags)
436+
def _form_elt_filter(tag):
437+
return tag in inner_elts or (
438+
tag.attrs.get('form') == self.id and tag.name in tags)
439+
elements = self.response.html.find_all(_form_elt_filter)
440+
for pos, node in enumerate(elements):
436441
attrs = dict(node.attrs)
437442
tag = node.name
438443
name = None

0 commit comments

Comments
 (0)