(self, root: html.HtmlElement, keep: list[str], obs: list[str], parent_chain: bool=False, get_new_label: bool=False)
| 156 | self.bids2xpath = i2xpath |
| 157 | |
| 158 | def parse(self, root: html.HtmlElement, keep: list[str], obs: list[str], parent_chain: bool=False, get_new_label: bool=False) -> dict[str]: |
| 159 | def get_text(str: str) -> str: |
| 160 | return '' if str is None else str.strip()[:500] |
| 161 | |
| 162 | def check_attr(attr: str, node: html.HtmlElement) -> bool: |
| 163 | tag = node.tag |
| 164 | if ( |
| 165 | ( attr == 'role' and node.attrib.get(attr, '') in ['presentation', 'none', 'link'] ) |
| 166 | or ( attr == 'type' and node.attrib.get(attr, '') == 'hidden' ) |
| 167 | or ( attr == 'value' and tag in ['option'] ) |
| 168 | ): |
| 169 | return False |
| 170 | return True |
| 171 | |
| 172 | def is_visible(bid: str) -> bool: |
| 173 | if not self.use_position: |
| 174 | return True |
| 175 | |
| 176 | rect = self.rect.get(bid, None) |
| 177 | if rect is None: |
| 178 | return False |
| 179 | |
| 180 | if self.window_size is None: |
| 181 | return True |
| 182 | |
| 183 | # get window size |
| 184 | wx, wy, ww, wh = self.window_size |
| 185 | x, y, w, h = rect |
| 186 | if x + w < wx or x > wx + ww or y + h < wy or y > wy + wh: |
| 187 | return False |
| 188 | |
| 189 | return True |
| 190 | |
| 191 | def _dfs(node: html.HtmlElement, keep: list[str]=[], obs: list[str]=[], |
| 192 | parent_chain: bool=False, get_new_label: bool=False, par_keep: bool=False) -> (str, dict[str]): |
| 193 | # basic information |
| 194 | bid = node.attrib.get(self.id_attr, '') |
| 195 | tag = node.tag |
| 196 | label = node.attrib.get(self.label_attr, '') |
| 197 | |
| 198 | # element which is keeped equivalent to visible |
| 199 | visible = is_visible(bid) |
| 200 | in_keep_list = bid in keep |
| 201 | in_obs_list = (bid in obs or len(label) > 0) and visible |
| 202 | keep_element = in_keep_list or in_obs_list or visible or par_keep |
| 203 | |
| 204 | # mark label |
| 205 | bids2label, labeled_elems = {}, [] |
| 206 | have_label = False |
| 207 | if in_keep_list or in_obs_list: |
| 208 | if label is None or len(label) == 0 or get_new_label: |
| 209 | label = self.identifier.generate() |
| 210 | node.attrib[self.label_attr] = label |
| 211 | bids2label[bid] = label |
| 212 | bids2label[label] = bid |
| 213 | have_label = True |
| 214 | |
| 215 | # get text or alt_text of current element |
no test coverage detected