@embrace-io/web-sdk
Version:
32 lines (31 loc) • 1.05 kB
JavaScript
//#region src/utils/getSelector.ts
/**
* This file was retrieved on 25 May 2026 from
* https://github.com/GoogleChrome/web-vitals/blob/dc31a21/src/lib/getSelector.ts
*
* We want the selectors from our own instrumentation to match those generated by
* web-vitals, however the web-vitals library does not expose this function. Copying
* the function verbatim was agreed as the best approach for selector parity.
*/
const getName = (node) => {
const name = node.nodeName;
return node.nodeType === 1 ? name.toLowerCase() : name.toUpperCase().replace(/^#/, "");
};
const MAX_LEN = 100;
const getSelector = (node) => {
let sel = "";
try {
while (node?.nodeType !== 9) {
const el = node;
const part = el.id ? `#${el.id}` : [getName(el), ...Array.from(el.classList).sort()].join(".");
if (sel.length + part.length > MAX_LEN - 1) return sel || part;
sel = sel ? `${part}>${sel}` : part;
if (el.id) break;
node = el.parentNode;
}
} catch {}
return sel;
};
//#endregion
export { getSelector };
//# sourceMappingURL=getSelector.js.map