| 8 | from .utils import get_xpath_top_down |
| 9 | |
| 10 | class HtmlParser(): |
| 11 | def __init__(self, ctx: str, args: dict[str]={}) -> None: |
| 12 | stt = time.time() |
| 13 | self.dom_tree = self.ctx2tree(ctx) |
| 14 | # tool related |
| 15 | self.bids2label = {} |
| 16 | self.bids2xpath = {} |
| 17 | self.used_labels = {} |
| 18 | |
| 19 | # parse args |
| 20 | self.parse_args(args) |
| 21 | self.init_time = time.time() - stt |
| 22 | |
| 23 | def parse_args(self, args: dict[str]={}) -> None: |
| 24 | def attr_check(attr, type_model='str'): |
| 25 | if attr is None: |
| 26 | return False |
| 27 | attr_type = type(attr) |
| 28 | if attr_type != type(type_model): |
| 29 | return False |
| 30 | if attr_type == type('str') and len(attr) == 0: |
| 31 | return False |
| 32 | return True |
| 33 | |
| 34 | args = {} if args is None else args |
| 35 | |
| 36 | # [Position] use_pos: False -> use full page, otherwise use window_size |
| 37 | use_position = args.get('use_position', False) |
| 38 | window_size = args.get('window_size', None) |
| 39 | rect = args.get('rect_dict', None) |
| 40 | if use_position: |
| 41 | if not attr_check(window_size, ()): |
| 42 | raise ValueError('window_size must be set when use_position is True') |
| 43 | if not attr_check(rect, {}): |
| 44 | raise ValueError('rect_dict must be set when use_position is True') |
| 45 | |
| 46 | if not attr_check(rect, {}): |
| 47 | rect = {} |
| 48 | |
| 49 | # [Label] for vimium is temp_clickable_label, otherwise keep all of it |
| 50 | label_attr = args.get('label_attr', '') |
| 51 | get_new_label = args.get('regenerate_label', False) |
| 52 | label_method = args.get('label_generator', None) |
| 53 | regen_label = not attr_check(label_method) |
| 54 | |
| 55 | # [id] for mind2web is backend_node_id, for normal website use our method |
| 56 | id_attr = args.get('id_attr', '') |
| 57 | regen_id = not attr_check(id_attr) |
| 58 | |
| 59 | if regen_id: |
| 60 | id_attr = 'temp_id' |
| 61 | |
| 62 | # [attributes] |
| 63 | keep_attrs = args.get('attr_list', []) |
| 64 | if not attr_check(keep_attrs, []): |
| 65 | keep_attrs = [] |
| 66 | |
| 67 | # [Tags] for clickable elem, keep: must keep, obs: keep if follow specific rule |