@embrace-io/web-sdk
Version:
1 lines • 2.42 kB
Source Map (JSON)
{"version":3,"file":"getSelector.cjs","names":[],"sources":["../../src/utils/getSelector.ts"],"sourcesContent":["/**\n * This file was retrieved on 25 May 2026 from\n * https://github.com/GoogleChrome/web-vitals/blob/dc31a21/src/lib/getSelector.ts\n *\n * We want the selectors from our own instrumentation to match those generated by\n * web-vitals, however the web-vitals library does not expose this function. Copying\n * the function verbatim was agreed as the best approach for selector parity.\n */\n\n/*\n * Copyright 2022 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst getName = (node: Node) => {\n const name = node.nodeName;\n return node.nodeType === 1\n ? name.toLowerCase()\n : name.toUpperCase().replace(/^#/, '');\n};\n\nconst MAX_LEN = 100;\n\nexport const getSelector = (node: Node | null) => {\n let sel = '';\n\n try {\n while (node?.nodeType !== 9) {\n const el: Element = node as Element;\n const part = el.id\n ? `#${el.id}`\n : [getName(el), ...Array.from(el.classList).sort()].join('.');\n if (sel.length + part.length > MAX_LEN - 1) {\n return sel || part;\n }\n sel = sel ? `${part}>${sel}` : part;\n if (el.id) {\n break;\n }\n node = el.parentNode;\n }\n } catch {\n // Do nothing...\n }\n return sel;\n};\n"],"mappings":";;;;;;;;;;AAyBA,MAAM,WAAW,SAAe;CAC9B,MAAM,OAAO,KAAK;CAClB,OAAO,KAAK,aAAa,IACrB,KAAK,YAAY,IACjB,KAAK,YAAY,CAAC,CAAC,QAAQ,MAAM,EAAE;AACzC;AAEA,MAAM,UAAU;AAEhB,MAAa,eAAe,SAAsB;CAChD,IAAI,MAAM;CAEV,IAAI;EACF,OAAO,MAAM,aAAa,GAAG;GAC3B,MAAM,KAAc;GACpB,MAAM,OAAO,GAAG,KACZ,IAAI,GAAG,OACP,CAAC,QAAQ,EAAE,GAAG,GAAG,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG;GAC9D,IAAI,IAAI,SAAS,KAAK,SAAS,UAAU,GACvC,OAAO,OAAO;GAEhB,MAAM,MAAM,GAAG,KAAK,GAAG,QAAQ;GAC/B,IAAI,GAAG,IACL;GAEF,OAAO,GAAG;EACZ;CACF,QAAQ,CAER;CACA,OAAO;AACT"}