(attr_filter, html, input_names={})
| 868 | |
| 869 | |
| 870 | def parse_html_form(attr_filter, html, input_names={}): |
| 871 | attr_str = "" if callable(attr_filter) else attr_filter |
| 872 | for form in re.finditer(r'(?P<TAG><form[^>]*%s.*?>)(?P<CONTENT>.*?)</?(form|body|html).*?>' % attr_str, html, re.I | re.S): |
| 873 | if callable(attr_filter) and not attr_filter(form.group('TAG')): |
| 874 | continue |
| 875 | |
| 876 | inputs = {} |
| 877 | action = parse_html_tag_attr_value("action", form.group('TAG')) |
| 878 | |
| 879 | for inputtag in re.finditer(r'(<(input|textarea).*?>)([^<]*(?=</\2)|)', |
| 880 | re.sub(re.compile(r'<!--.+?-->', re.I | re.S), "", form.group('CONTENT')), |
| 881 | re.I | re.S): |
| 882 | |
| 883 | name = parse_html_tag_attr_value("name", inputtag.group(1)) |
| 884 | if name: |
| 885 | value = parse_html_tag_attr_value("value", inputtag.group(1)) |
| 886 | if not value: |
| 887 | inputs[name] = inputtag.group(3) or "" |
| 888 | else: |
| 889 | inputs[name] = value |
| 890 | |
| 891 | if not input_names: |
| 892 | #: No attribute check |
| 893 | return action, inputs |
| 894 | else: |
| 895 | #: Check input attributes |
| 896 | for key, value in input_names.items(): |
| 897 | if key in inputs: |
| 898 | if isinstance(value, basestring) and inputs[key] == value: |
| 899 | continue |
| 900 | elif isinstance(value, tuple) and inputs[key] in value: |
| 901 | continue |
| 902 | elif hasattr(value, "search") and re.match(value, inputs[key]): |
| 903 | continue |
| 904 | else: |
| 905 | break #: Attibute value does not match |
| 906 | else: |
| 907 | break #: Attibute name does not match |
| 908 | else: |
| 909 | return action, inputs #: Passed attribute check |
| 910 | |
| 911 | return None, None #: No matching form found |
| 912 | |
| 913 | |
| 914 | def chunks(iterable, size): |
no test coverage detected