:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(elements): parse an elements chain in one place (#100986)
posthog[bot]b8f21c789061
diff --git a/posthog/models/element/element.py b/posthog/models/element/element.pyindex 94fb1cc5d063..2a269cfe4877 100644--- a/posthog/models/element/element.py+++ b/posthog/models/element/element.py@@ -58,42 +58,6 @@ def elements_to_string(elements: list[Element]) -> str:return ";".join(ret)-def chain_to_elements(chain: str) -> list[Element]:- """- Converts an elements chain string into a list of Element objects.- """- elements = []- for idx, el_string in enumerate(re.findall(split_chain_regex, chain)):- el_string_split = re.findall(split_class_attributes, el_string)[0]- attributes = re.finditer(parse_attributes_regex, el_string_split[2]) if len(el_string_split) > 2 else []-- element = Element(order=idx)-- if el_string_split[0]:- tag_and_class = el_string_split[0].split(".", 1)- element.tag_name = tag_and_class[0]- if len(tag_and_class) > 1:- element.attr_class = [cl for cl in tag_and_class[1].split(".") if cl != ""]-- for ii in attributes:- item = ii.groupdict()- if item["key"] == "href":- element.href = item["value"]- elif item["key"] == "nth-child":- element.nth_child = int(item["value"])- elif item["key"] == "nth-of-type":- element.nth_of_type = int(item["value"])- elif item["key"] == "text":- element.text = item["value"]- elif item["key"] == "attr_id":- element.attr_id = item["value"]- elif item["key"]:- element.attributes[item["key"]] = item["value"]-- elements.append(element)- return elements--_MAX_DATA_ATTRIBUTES = 50@@ -147,6 +111,52 @@ def build_attributes_filter(wanted_data_attributes: list[str]) -> Callable[[str]return matches+_PROMOTED_ATTRIBUTES: dict[str, tuple[str, Callable[[str], object]]] = {+ "text": ("text", str),+ "href": ("href", str),+ "attr_id": ("attr_id", str),+ "nth-child": ("nth_child", int),+ "nth-of-type": ("nth_of_type", int),+}+++def _parse_element(el_string: str, order: int, attributes_filter: Callable[[str], bool] | None) -> dict:+ match = split_class_attributes.search(el_string)+ tag_part = match.group(1) if match else ""+ attrs_part = match.group(3) if match else None++ element: dict = {+ "text": None,+ "tag_name": None,+ "attr_class": None,+ "href": None,+ "attr_id": None,+ "nth_child": None,+ "nth_of_type": None,+ "attributes": {},+ "order": order,+ }++ if tag_part:+ tag_and_class = tag_part.split(".", 1)+ element["tag_name"] = tag_and_class[0]+ if len(tag_and_class) > 1:+ element["attr_class"] = [cl for cl in tag_and_class[1].split(".") if cl != ""]++ if attrs_part:+ for attribute_match in parse_attributes_regex.finditer(attrs_part):+ key = attribute_match.group("key")+ value = attribute_match.group("value")+ promoted = _PROMOTED_ATTRIBUTES.get(key)+ if promoted:+ field, convert = promoted+ element[field] = convert(value)+ elif key and (attributes_filter is None or attributes_filter(key)):+ element["attributes"][key] = value++ return element++def chain_to_element_dicts(chain: str, attributes_filter: Callable[[str], bool] | None = None) -> list[dict]:"""Converts an elements chain string into serialized element dicts, shaped exactly like@@ -154,47 +164,14 @@ def chain_to_element_dicts(chain: str, attributes_filter: Callable[[str], bool]can serialize large pages cheaply. attributes_filter optionally restricts the attributesmap to matching keys (see build_attributes_filter)."""- element_dicts: list[dict] = []- for idx, el_string in enumerate(split_chain_regex.findall(chain)):- el_string_match = split_class_attributes.search(el_string)- tag_part = el_string_match.group(1) if el_string_match else ""- attrs_part = el_string_match.group(3) if el_string_match else None-- element: dict = {- "text": None,- "tag_name": None,- "attr_class": None,- "href": None,- "attr_id": None,- "nth_child": None,- "nth_of_type": None,- "attributes": {},- "order": idx,- }+ return [+ _parse_element(el_string, idx, attributes_filter)+ for idx, el_string in enumerate(split_chain_regex.findall(chain))+ ]- if tag_part:- tag_and_class = tag_part.split(".", 1)- element["tag_name"] = tag_and_class[0]- if len(tag_and_class) > 1:- element["attr_class"] = [cl for cl in tag_and_class[1].split(".") if cl != ""]-- if attrs_part:- for attribute_match in parse_attributes_regex.finditer(attrs_part):- key = attribute_match.group("key")- value = attribute_match.group("value")- if key == "href":- element["href"] = value- elif key == "nth-child":- element["nth_child"] = int(value)- elif key == "nth-of-type":- element["nth_of_type"] = int(value)- elif key == "text":- element["text"] = value- elif key == "attr_id":- element["attr_id"] = value- elif key:- if attributes_filter is None or attributes_filter(key):- element["attributes"][key] = value-- element_dicts.append(element)- return element_dicts++def chain_to_elements(chain: str) -> list[Element]:+ """+ Converts an elements chain string into a list of Element objects.+ """+ return [Element(**element) for element in chain_to_element_dicts(chain)]diff --git a/posthog/test/test_element_model.py b/posthog/test/test_element_model.pyindex 71b4cb478afc..66c1b0dd316d 100644--- a/posthog/test/test_element_model.py+++ b/posthog/test/test_element_model.py@@ -120,6 +120,11 @@ class TestElement(ClickhouseTestMixin, BaseTest):assert element_dicts[0]["href"] == "/a-url"assert element_dicts[0]["attr_class"] == ["small"]+ def test_chain_to_element_dicts_skips_empty_attribute_keys(self) -> None:+ element_dicts = chain_to_element_dicts('a:="x"nth-child="0"')+ assert element_dicts[0]["attributes"] == {}+ assert element_dicts[0]["nth_child"] == 0+def test_build_attributes_filter_caps_entry_count(self) -> None:many_attrs = [f"data-attr-{i}" for i in range(100)]matcher = build_attributes_filter(many_attrs)