| 1 | from __future__ import absolute_import |
| 2 | |
| 3 | import re |
| 4 | from collections import defaultdict |
| 5 | |
| 6 | from ._form_controls import HTMLForm, Label |
| 7 | from ._request import Request |
| 8 | from .polyglot import urljoin, is_string, as_unicode |
| 9 | |
| 10 | |
| 11 | class SkipControl(ValueError): |
| 12 | pass |
| 13 | |
| 14 | |
| 15 | def normalize_line_endings(text): |
| 16 | return re.sub(as_unicode(r"(?:(?<!\r)\n)|(?:\r(?!\n))"), u"\r\n", text) |
| 17 | |
| 18 | |
| 19 | def label_text(elem): |
| 20 | ans = [] |
| 21 | if elem.text: |
| 22 | ans.append(elem.text) |
| 23 | for child in elem: |
| 24 | if child.tail: |
| 25 | ans.append(child.tail) |
| 26 | return ''.join(ans) |
| 27 | |
| 28 | |
| 29 | def parse_control(elem, parent_of, default_type='text'): |
| 30 | attrs = dict(elem.attrib) |
| 31 | label_elem = parent_of(elem, 'label') |
| 32 | if label_elem is not None: |
| 33 | lt = label_text(label_elem) |
| 34 | if lt: |
| 35 | attrs["__label"] = lt |
| 36 | ctype = attrs.get('type') or default_type |
| 37 | return ctype, attrs.get('name'), attrs |
| 38 | |
| 39 | |
| 40 | def parse_input(elem, parent_of, *a): |
| 41 | return parse_control(elem, parent_of) |
| 42 | |
| 43 | |
| 44 | def parse_button(elem, parent_of, *a): |
| 45 | ctype, name, attrs = parse_control(elem, parent_of, default_type='submit') |
| 46 | ctype += 'button' |
| 47 | return ctype, name, attrs |
| 48 | |
| 49 | |
| 50 | def parse_option(elem, parent_of, attrs_map): |
| 51 | ctype, name, attrs = parse_control(elem, parent_of) |
| 52 | og = parent_of(elem, 'optgroup') |
| 53 | contents = (elem.text or '').strip() |
| 54 | attrs['contents'] = contents |
| 55 | attrs['value'] = attrs.get('value', contents) |
| 56 | attrs['label'] = attrs.get('label', contents) |
| 57 | if og is not None and og.get('disabled') is not None: |
| 58 | attrs['disabled'] = 'disabled' |
| 59 | sel = parent_of(elem, 'select') |
| 60 | if sel is None: |
| 61 | raise SkipControl() |
| 62 | attrs['__select'] = sel = attrs_map[sel]['__select'] |
| 63 | return 'select', sel.get('name'), attrs |
| 64 | |
| 65 | |
| 66 | def parse_textarea(elem, parent_of, *a): |
| 67 | ctype, name, attrs = parse_control(elem, parent_of) |
| 68 | ctype = 'textarea' |
| 69 | attrs['value'] = normalize_line_endings(elem.text or u'') |
| 70 | return ctype, name, attrs |
| 71 | |
| 72 | |
| 73 | def parse_select(elem, parent_of, *a): |
| 74 | ctype, name, attrs = parse_control(elem, parent_of) |
| 75 | ctype = 'select' |
| 76 | return ctype, name, {'__select': attrs} |
| 77 | |
| 78 | |
| 79 | def parse_forms(root, base_url, request_class=None, select_default=False, encoding=None): |
| 80 | if request_class is None: |
| 81 | request_class = Request |
| 82 | global_form = HTMLForm(base_url, encoding=encoding) |
| 83 | forms, labels = [], [] |
| 84 | form_elems = [] |
| 85 | form_id_map = {} |
| 86 | all_elems = tuple( |
| 87 | e for e in root.iter('*') if is_string(e.tag)) |
| 88 | parent_map = {c: p for p in all_elems for c in p} |
| 89 | id_to_labels = defaultdict(list) |
| 90 | for e in all_elems: |
| 91 | q = e.tag.lower() |
| 92 | if q == 'form': |
| 93 | form_elems.append(e) |
| 94 | fid = e.get('id') |
| 95 | if fid: |
| 96 | form_id_map[fid] = e |
| 97 | elif q == 'label': |
| 98 | for_id = e.get('for') |
| 99 | if for_id is not None: |
| 100 | label = Label(label_text(e), for_id) |
| 101 | labels.append(label) |
| 102 | id_to_labels[for_id].append(label) |
| 103 | elif q == 'base': |
| 104 | base_url = e.get('href') or base_url |
| 105 | |
| 106 | def parent_of(elem, parent_name): |
| 107 | q = elem |
| 108 | while True: |
| 109 | q = parent_map.get(q) |
| 110 | if q is None: |
| 111 | return |
| 112 | if q.tag.lower() == parent_name: |
| 113 | return q |
| 114 | |
| 115 | forms_map = {} |
| 116 | for form_elem in form_elems: |
| 117 | name = form_elem.get('name') or None |
| 118 | action = form_elem.get('action') or None |
| 119 | method = form_elem.get('method') or 'GET' |
| 120 | enctype = form_elem.get( |
| 121 | 'enctype') or "application/x-www-form-urlencoded" |
| 122 | if action: |
| 123 | action = urljoin(base_url, action) |
| 124 | else: |
| 125 | action = base_url |
| 126 | form = HTMLForm(action, method, enctype, name, form_elem.attrib, |
| 127 | request_class, forms, labels, id_to_labels, encoding=encoding) |
| 128 | forms_map[form_elem] = form |
| 129 | forms.append(form) |
| 130 | |
| 131 | attrs_map = {} |
| 132 | control_names = { |
| 133 | 'option': parse_option, |
| 134 | 'button': parse_button, |
| 135 | 'input': parse_input, |
| 136 | 'textarea': parse_textarea, |
| 137 | 'select': parse_select, |
| 138 | } |
| 139 | |
| 140 | for i, elem in enumerate(all_elems): |
| 141 | q = elem.tag.lower() |
| 142 | cfunc = control_names.get(q) |
| 143 | if cfunc is not None: |
| 144 | fid = elem.get('form') |
| 145 | if fid and fid in form_id_map: |
| 146 | form_elem = form_id_map[fid] |
| 147 | else: |
| 148 | form_elem = parent_of(elem, 'form') |
| 149 | form = forms_map.get(form_elem, global_form) |
| 150 | try: |
| 151 | control_type, control_name, attrs = cfunc(elem, parent_of, |
| 152 | attrs_map) |
| 153 | except SkipControl: |
| 154 | continue |
| 155 | attrs_map[elem] = attrs |
| 156 | form.new_control( |
| 157 | control_type, |
| 158 | control_name, |
| 159 | attrs, |
| 160 | index=i * 10, |
| 161 | select_default=select_default) |
| 162 | |
| 163 | for form in forms: |
| 164 | form.fixup() |
| 165 | global_form.fixup() |
| 166 | |
| 167 | return forms, global_form |