getUniqueSelector function exported ✓ 84.2%
Last updated: 2026-02-24T21:07:57.586Z
Metrics
LOC: 32
Complexity: 7
Params: 1
Coverage: 84.2% (16/19 lines, 66x executed)
Signature
getUniqueSelector(element: Element): : string
Summary
Builds a unique, stable CSS selector for a DOM element. Traverses parent chain using tag + :nth-of-type, shortcuts on id.
Tags
#@param element - The element to build a selector for#@returns A CSS selector string that uniquely identifies the element
Source Code
export function getUniqueSelector(element: Element): string {
if (element.id) return `#${CSS.escape(element.id)}`;
const parts: string[] = [];
let current: Element | null = element;
while (current && current !== document.body) {
let selector = current.tagName.toLowerCase();
if (current.id) {
selector = `#${CSS.escape(current.id)}`;
parts.unshift(selector);
break;
}
const parent: Element | null = current.parentElement;
if (parent) {
const siblings = Array.from(parent.children).filter(
(c: Element) => c.tagName === current!.tagName,
);
if (siblings.length > 1) {
const index = siblings.indexOf(current) + 1;
selector += `:nth-of-type(${index})`;
}
}
parts.unshift(selector);
current = parent;
}
return parts.join(" > ");
}
Dependencies (Outgoing)
| Target | Type |
|---|---|
| Extractor | uses |
Impact (Incoming)
| Source | Type |
|---|---|
| handleRuleButtonClick | uses |
| isFillableField | uses |
| initFieldIcon | uses |
| NativeElement | uses |
| matches | uses |
| matches | uses |
| matches | uses |
| matches | uses |
| findAntLabel | uses |
| matches | uses |