Spaces:
Runtime error
Runtime error
Download src/utils/selectors.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/utils/selectors.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/src/utils/selectors.py
-
curl -L -o selectors.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/utils/selectors.py
2.54 kB
| """Selector normalization helpers.""" | |
| import re | |
| from typing import Any, Dict, Optional | |
| _ATTR_RE_CACHE: Dict[str, re.Pattern] = {} | |
| def _attr_pattern(name: str) -> re.Pattern: | |
| if name not in _ATTR_RE_CACHE: | |
| _ATTR_RE_CACHE[name] = re.compile( | |
| rf"{re.escape(name)}\s*=\s*([\"\'])(.*?)\1", | |
| flags=re.IGNORECASE, | |
| ) | |
| return _ATTR_RE_CACHE[name] | |
| def _extract_attr(attrs: str, name: str) -> Optional[str]: | |
| match = _attr_pattern(name).search(attrs) | |
| if not match: | |
| return None | |
| return match.group(2) | |
| def _css_attr_value(value: str) -> Optional[str]: | |
| if value is None: | |
| return None | |
| if "'" in value and '"' in value: | |
| return None | |
| if "'" in value: | |
| return f'"{value}"' | |
| return f"'{value}'" | |
| def normalize_selector_value(value: Optional[str]) -> Optional[str]: | |
| """ | |
| Normalize a selector string. If an HTML element snippet is provided, try | |
| to convert it into a CSS selector. | |
| """ | |
| if value is None: | |
| return None | |
| raw = value.strip() | |
| if not raw: | |
| return raw | |
| if not raw.lstrip().startswith("<"): | |
| return raw | |
| match = re.search(r"<\s*([a-zA-Z0-9:_-]+)([^>]*)>", raw) | |
| if not match: | |
| return raw | |
| tag = match.group(1) | |
| attrs = match.group(2) | |
| attr_id = _extract_attr(attrs, "id") | |
| if attr_id: | |
| return f"{tag}#{attr_id}" | |
| data_testid = _extract_attr(attrs, "data-testid") | |
| if data_testid: | |
| q = _css_attr_value(data_testid) | |
| if q: | |
| return f"{tag}[data-testid={q}]" | |
| name = _extract_attr(attrs, "name") | |
| if name: | |
| q = _css_attr_value(name) | |
| if q: | |
| return f"{tag}[name={q}]" | |
| aria = _extract_attr(attrs, "aria-label") | |
| if aria: | |
| q = _css_attr_value(aria) | |
| if q: | |
| return f"{tag}[aria-label={q}]" | |
| placeholder = _extract_attr(attrs, "placeholder") | |
| if placeholder: | |
| q = _css_attr_value(placeholder) | |
| if q: | |
| return f"{tag}[placeholder*={q}]" | |
| class_attr = _extract_attr(attrs, "class") | |
| if class_attr: | |
| classes = [c for c in re.split(r"\s+", class_attr.strip()) if c] | |
| if classes: | |
| return tag + "".join(f".{c}" for c in classes[:3]) | |
| return tag | |
| def normalize_selectors_dict(data: Dict[str, Any]) -> Dict[str, Any]: | |
| normalized = dict(data) | |
| for key, value in normalized.items(): | |
| if isinstance(value, str): | |
| normalized[key] = normalize_selector_value(value) | |
| return normalized | |