ff-dom 2.0.9 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,22 @@
1
1
  export declare const reWhiteSpace: RegExp;
2
2
  export declare let cspEnabled: boolean;
3
+ export declare const timeLog: (label: string, start: number) => void;
4
+ type EvalResult = {
5
+ first: Node | null;
6
+ second: Node | null;
7
+ };
8
+ export declare const clearXPathEvalCache: (node?: Node | null) => void;
9
+ export declare const evaluateXPathOnce: (xpath: string, docmt: Node) => EvalResult;
3
10
  export declare let modifiedElementAttributes: {
4
11
  url: string | null;
5
12
  attributeName: string | null;
6
13
  element: HTMLElement | Element;
7
14
  doc: Document;
8
15
  }[];
16
+ export declare const hasNumericAttributeValue: (value: string | null | undefined) => boolean;
17
+ export declare const getClassTokenConditions: (element: HTMLElement | Element, classValue: string | null | undefined, docmt: Document | ShadowRoot, allowNumericToken?: boolean) => string[];
18
+ export declare const getClassTokenCondition: (element: HTMLElement | Element, classValue: string | null | undefined, docmt: Document | ShadowRoot, allowNumericToken?: boolean) => string | null;
19
+ export declare const sanitizeAttributeValue: (attributeName: string, attributeValue: string | null | undefined) => string;
9
20
  export declare const createObserver: (addedNodeCallBack: Function) => void;
10
21
  export declare const startObserver: (target: Node, options: MutationObserverInit | undefined) => void;
11
22
  export declare const stopObserver: () => void;
@@ -18,7 +29,19 @@ export declare const escapeCharacters: (text: string) => string;
18
29
  export declare const removeParenthesis: (xpath: string) => string;
19
30
  export declare const findXpathWithIndex: (val: string, node: HTMLElement | Element, docmt: Node, count: number) => string | undefined;
20
31
  export declare const replaceWhiteSpaces: (str: string) => string;
32
+ export declare const getContainerTextCondition: (element: HTMLElement | Element, text: string | null | undefined) => string;
33
+ export declare const getStableTargetTextCandidates: (text: string | null | undefined) => {
34
+ prefix: string;
35
+ fragment: string;
36
+ };
37
+ export declare const getStableTargetText: (text: string | null | undefined) => string;
21
38
  export declare const getShadowRoot: (el: HTMLElement | Element) => Element | null;
39
+ export declare const createShadowEvaluationContext: (root: ShadowRoot, element?: HTMLElement | Element | (HTMLElement | Element)[]) => {
40
+ owner: Document;
41
+ contextNode: HTMLDivElement;
42
+ cloneElement: Element | null;
43
+ cloneElements: (Element | null)[];
44
+ };
22
45
  export declare const checkBlockedAttributes: (attribute: {
23
46
  name: string;
24
47
  value: string;
@@ -30,14 +53,31 @@ export declare const getPropertyXPath: (element: HTMLElement | Element, docmt: D
30
53
  export declare const getAbsoluteXPath: (domNode: HTMLElement | Element | null, docmt: Document) => string;
31
54
  export declare const getRelativeXPath: (domNode: HTMLElement | Element, docmt: Document, isIndex: boolean, isTarget: boolean | undefined, attributesArray: Attr[]) => any;
32
55
  export declare const getCombinationXpath: (attribute: Attr, domNode: HTMLElement | Element) => string | undefined;
33
- export declare const getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
56
+ export declare const getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document | ShadowRoot, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
34
57
  export declare const intermediateXpathStep: (targetElemt: HTMLElement | Element, attr: {
35
58
  name: string;
36
59
  value: string;
37
60
  }, isTarget: boolean) => string;
38
- export declare const getFilteredTextXPath: (node: HTMLElement | Element, docmt: Document) => string;
61
+ export declare const intermediateXpathSteps: (targetElemt: HTMLElement | Element, attr: {
62
+ name: string;
63
+ value: string;
64
+ }, isTarget: boolean) => string[];
65
+ export declare const getFilteredTextXPath: (node: HTMLElement | Element, docmt: Document | ShadowRoot) => string;
66
+ export declare const getNormalizedPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
67
+ export declare const getStartsWithPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
68
+ export declare const getContainsPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
69
+ export declare const getOrAttributesXPath: (element: HTMLElement | Element, attributes: Attr[]) => string;
70
+ export declare const getTagOnlyXPath: (element: HTMLElement | Element, docmt?: Document | ShadowRoot) => string;
71
+ export declare const getFirstMatchedNode: (xpath: string, docmt: Document | ShadowRoot) => HTMLElement | Element | null;
72
+ export declare const isExactUniqueXpath: (xpath: string, element: HTMLElement | Element, docmt: Document | ShadowRoot) => boolean;
73
+ export declare const getAxisNodeTest: (element: HTMLElement | Element) => string;
74
+ export declare const getUniqueNodeAnchorXpaths: (node: HTMLElement | Element, docmt: Document | ShadowRoot, isTarget: boolean) => {
75
+ key: string;
76
+ value: string;
77
+ }[];
39
78
  export declare const getTextXpathFunction: (domNode: HTMLElement | Element) => string | undefined;
40
79
  export declare const getXpathString: (node: HTMLElement | Element, attrName: string, attrValue: string) => string;
80
+ export declare const getXpathStrings: (node: HTMLElement | Element, attrName: string, attrValue: string) => string[];
41
81
  export declare const replaceActualAttributes: (str: string, element: {
42
82
  attributes: any;
43
83
  }) => string;
@@ -67,7 +107,7 @@ export declare const xpathUtils: {
67
107
  getAbsoluteXPath: (domNode: HTMLElement | Element | null, docmt: Document) => string;
68
108
  getRelativeXPath: (domNode: HTMLElement | Element, docmt: Document, isIndex: boolean, isTarget: boolean | undefined, attributesArray: Attr[]) => any;
69
109
  getCombinationXpath: (attribute: Attr, domNode: HTMLElement | Element) => string | undefined;
70
- getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
110
+ getAttributeCombinationXpath: (domNode: HTMLElement | Element, docmt: Document | ShadowRoot, uniqueAttributes: Attr[], isTarget: boolean) => string | undefined;
71
111
  getElementFromXpath: (xpath: string, docmt: Node, multi?: boolean) => Node | XPathResult | null;
72
112
  isSvg: (element: HTMLElement | Element) => element is SVGElement;
73
113
  findXpathWithIndex: (val: string, node: HTMLElement | Element, docmt: Node, count: number) => string | undefined;
@@ -97,6 +137,28 @@ export declare const xpathUtils: {
97
137
  }[];
98
138
  cspEnabled: boolean;
99
139
  getXPathPattern: typeof getXPathPattern;
140
+ getNormalizedPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
141
+ getStartsWithPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
142
+ getContainsPropertyXPath: (element: HTMLElement | Element, prop: string, value: string) => string;
143
+ getOrAttributesXPath: (element: HTMLElement | Element, attributes: Attr[]) => string;
144
+ getTagOnlyXPath: (element: HTMLElement | Element, docmt?: Document | ShadowRoot) => string;
145
+ getFirstMatchedNode: (xpath: string, docmt: Document | ShadowRoot) => HTMLElement | Element | null;
146
+ isExactUniqueXpath: (xpath: string, element: HTMLElement | Element, docmt: Document | ShadowRoot) => boolean;
147
+ getAxisNodeTest: (element: HTMLElement | Element) => string;
148
+ getUniqueNodeAnchorXpaths: (node: HTMLElement | Element, docmt: Document | ShadowRoot, isTarget: boolean) => {
149
+ key: string;
150
+ value: string;
151
+ }[];
100
152
  escapeAttrValue: typeof escapeAttrValue;
101
153
  isUniqueInDOM: typeof isUniqueInDOM;
154
+ sanitizeAttributeValue: (attributeName: string, attributeValue: string | null | undefined) => string;
155
+ getClassTokenCondition: (element: HTMLElement | Element, classValue: string | null | undefined, docmt: Document | ShadowRoot, allowNumericToken?: boolean) => string | null;
156
+ getClassTokenConditions: (element: HTMLElement | Element, classValue: string | null | undefined, docmt: Document | ShadowRoot, allowNumericToken?: boolean) => string[];
157
+ getXpathStrings: (node: HTMLElement | Element, attrName: string, attrValue: string) => string[];
158
+ intermediateXpathSteps: (targetElemt: HTMLElement | Element, attr: {
159
+ name: string;
160
+ value: string;
161
+ }, isTarget: boolean) => string[];
162
+ getContainerTextCondition: (element: HTMLElement | Element, text: string | null | undefined) => string;
102
163
  };
164
+ export {};
package/dist/xpath.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{VirtualConsole as e,JSDOM as t}from"jsdom";const n=/^[\S]+( [\S]+)*$/i,a={};let l=[];const r=e=>/\d/.test(e),o=e=>e?.childNodes[0]?.nodeValue||"",s=(e,t,n,l=!1)=>{try{let r;if(void 0!==a[e])r=a[e];else{r=(9===n.nodeType?n:n.ownerDocument).evaluate(`count(${e})`,n,null,XPathResult.NUMBER_TYPE,null).numberValue,a[e]=r}if(l&&Array.isArray(t)){if(r>1){const a=d(e,n,!0);let l,r=0;if(a instanceof XPathResult)for(;(l=a?.iterateNext())&&(!t.includes(l)||(r+=1,2!==r)););if(2!==r)return 0}}else if(1===r){return d(e,n)===t?r:0}return r}catch(t){return console.error(`Error evaluating XPath: ${e}`,t),0}},i=e=>{if(e){if(-1!==e.indexOf('"'))return`'${e}'`;if(-1!==e.indexOf("'"))return`"${e}"`}return`'${e}'`},c=(e,t,n,a)=>{try{const l=9===n.nodeType?n:n.ownerDocument;let r=0;if(a){if(1===s(`${e}[${a}]`,t,n))return`${e}[${a}]`;if(1===s(`(${e})[${a}]`,t,n))return`(${e})[${a}]`}const o=l.evaluate(e,n,null,XPathResult.ANY_TYPE,null);let i;for(;i=o.iterateNext();)if(r++,i.isSameNode(t))return 1===s(`${e}[${r}]`,t,n)?`${e}[${r}]`:1===s(`(${e})[${r}]`,t,n)?`(${e})[${r}]`:void 0}catch(e){console.log(e)}},u=e=>e?e.replace(/\s\s+/g," ").trim():e,f=(e,t,n)=>{if(!e?.value||"boolean"==typeof e?.value)return!1;if(["true","false","on","off","flntooltip","flutter-highlight-overlay"].some(t=>t===e.value))return!1;if(["type","style","locator-data-tooltip","value"].some(t=>t===e.name))return!1;const a=l?.find(n=>n.doc===t.ownerDocument&&n.element===t&&n.attributeName===e.name);return!a&&(!(0===e?.name?.indexOf("on")&&e?.name?.length>3)&&("function"!=typeof e.value&&(!n||!r(e.value))))},m=e=>e instanceof SVGElement,d=(e,t,n=!1)=>{let a=t;t.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&(a=t.host??t);const l=a.nodeType===Node.DOCUMENT_NODE?a:a.ownerDocument;return n?l.evaluate(e,a,null,XPathResult.ANY_TYPE,null):l.evaluate(e,a,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue},h=(e,t,a,l,o,c)=>{if(l){const{tagName:f}=e;let d,h="";const p=[];let g;if(l&&(!c||!r(l))&&(g=n.test(l)?`//${f}[${a}=${i(l)}]`:((e,t,n)=>t?`//*[local-name()='${n}' and ${e}]`:`//${n}[${e}]`)(`normalize-space(${a})=${i(u(l)).trim()}`,m(e),e.tagName.toLowerCase()),d=s(g,e,t),1===d&&!o))return g;if(l&&c){const r=l.split(" ");if(r?.length)if(1===r.length){const c=[...new Set(r[0].match(/([^0-9]+)/g))];if(c?.length>=1&&c[0]&&u(c[0].trim())?.length>1&&l.startsWith(c[0])&&(h=n.test(c[0])?`starts-with(${a},${i(c[0]).trim()})`:`starts-with(${a},${i(u(c[0])).trim()})`),c?.length>1&&c[c.length-1]&&u(c[c.length-1].trim())?.length>1&&l.endsWith(c[c.length-1])&&(h=n.test(c[c.length-1])?`ends-with(${a},${i(c[c.length-1]).trim()})`:`ends-with(${a},${i(u(c[c.length-1])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g}else{const c=r.length%2==0?r.length/2:r.length%2,p=r.slice(0,c).join(" ");let $=[...new Set(p.match(/([^0-9]+)/g))];if($?.length&&$[0]&&u($[0].trim())?.length&&l.startsWith($[0])&&(h=n.test($[0])?`starts-with(${a},${i($[0]).trim()})`:`starts-with(${a},${i(u($[0])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g;const y=r.slice(c,r.length-1).join(" ");if($=[...new Set(y.match(/([^0-9]+)/g))],$?.length&&$[0]&&u($[0].trim())?.length>3&&l.endsWith($[0])&&(h=n.test($[0])?`ends-with(${a},${i($[0]).trim()})`:`ends-with(${a},${i(u($[0])).trim()})`),h?.length&&(g=m(e)?`//*[local-name()='${f}' and ${h}]`:`//${f}[${h}]`,d=s(g,e,t),1===d&&!o))return g}}if(l&&c&&r(l)){const r=[...new Set(l.match(/([^0-9]+)/g))];if(r?.length)for(let e=0;e<r?.length;e++)r[e]&&u(r[e].trim())?.length>1&&(n.test(r[e])?p.push(`contains(${a},${i(r[e].trim()).trim()})`):p.push(`contains(${a},${i(u(r[e])).trim()})`));if(p?.length&&(g=m(e)?`//*[local-name()='${f}' and ${p.join(" and ")}]`:`//${f}[${p.join(" and ")}]`,d=s(g,e,t),1===d&&!o))return g}if(g=m(e)?`//*[local-name()='${f}' and text()]`:`//${f}[text()]`,d=s(g,e,t),1===d&&!o)return g}},p=(e,t,a)=>{let l=m(e),r="";if(f(t,e,a)){let a=t.value;a=a.replace("removePointers","");const o=t.name;r=n.test(a)?l?`*[local-name()='${e.tagName}' and @${o}=${i(a)}]`:`${e.tagName||"*"}[@${o}=${i(a)}]`:l?`*[local-name()='${e.tagName}' and normalize-space(@${o})=${i(a)}]`:`${e.tagName||"*"}[normalize-space(@${o})=${i(a)}]`}return r},g=(e,t)=>{if(!e.textContent)return"";const a=o(e);let l;return l=n.test(a)?m(e)?`//*[local-name()='${e.tagName}' and .=${i(a)}]`:`//${e.tagName||"*"}[.=${i(a)}]`:m(e)?`//*[local-name()='${e.tagName}' and normalize-space(.)=${i(a)}]`:`//${e.tagName||"*"}[normalize-space(.)=${i(a)}]`,l},$=(e,t,n)=>{const a=new RegExp(/^[\S]+( [\S]+)*$/gi);let l="";return n&&(l=a.test(n)?"class"===t?m(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${i(n)})]`:`//${e.tagName||"*"}[contains(@${t},${i(n)})]`:m(e)?`//*[local-name()='${e.tagName}' and @${t}=${i(n)}]`:`//${e.tagName||"*"}[@${t}=${i(n)}]`:m(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${i(n)})]`:`//${e.tagName||"*"}[contains(@${t},${i(n)})]`),l},y=e=>e=(e=e.replace(/text\(\)\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1")).replace(/\.\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1");function v(e){const t=function(e){return e.toLowerCase().replace(/'[^']*'/g,"?").replace(/"[^"]*"/g,"?").replace(/\[\d+\]/g,"[?]").replace(/\/\/[a-z0-9_-]+/g,"//node").replace(/\/[a-z0-9_-]+/g,"/node")}(e),n=function(e){const t=e.toLowerCase(),n=t.match(/(ancestor-or-self|ancestor|descendant-or-self|descendant|following-sibling|preceding-sibling|following|preceding|parent|child|self)::/),a=n?.[1]??"none",l=t.match(/@([a-z0-9:-]+)/);return{axis:a,attribute:l?.[1]??"none",usesNormalize:t.includes("normalize-space")}}(e);return["XPATH",`axis:${n.axis}`,`attr:${n.attribute}`,`normalize:${n.usesNormalize}`,`shape:${t}`].join("|")}function N(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/ /g,"\\ ")}function E(e,t,n,a){const l=a?.getRootNode?.()??e,r=e=>{try{return Array.from(l.querySelectorAll(e))}catch{return[]}};try{switch(t){case"id":return 1===r(`#${N(n)}`).length;case"name":return 1===r(`[name="${N(n)}"]`).length;case"className":return 1===r(`.${n}`).length;case"tagName":case"cssSelector":return 1===r(n).length;case"linkText":return 1===r("a").filter(e=>e.textContent?.trim()===n).length;case"partialLinkText":return 1===r("a").filter(e=>e.textContent?.includes(n)).length;default:return!1}}catch{return!1}}let x=[],b=[],S=new Map,w=new Map;const R=new Map,k=(e,t,n,a,l,r)=>{if(e){let o;o="parent"===r?[`${e}/descendant-or-self::${t}`,`${e}/following::${t}`]:[`${e}/ancestor-or-self::${t}`,`${e}/preceding::${t}`];for(const e of o){S?.get(e)||S.set(e,s(e,n,a));const t=S?.get(e);if(1===t)return e;if(t>1&&(b.length?t<b[0].count&&(b.pop(),b.push({key:"relative xpath by unique parent "+(l?"index":""),value:e,count:t})):b.push({key:"relative xpath by unique parent "+(l?"index":""),value:e,count:t})),t>1&&l&&!x.length){const r=c(e,n,a,t);r&&1===s(r,n,a)&&x.push({key:"relative xpath by unique parent "+(l?"index":""),value:r})}}}return null},C=(e,t,a,l,c,d)=>{try{if(R.has(e))return R.get(e);const h=[];let p=e;for(;p&&1===p.nodeType;){const e=!1;for(const e of Array.from(p.attributes))if(f(e,p,l)){let n=e.nodeValue;n=n?.replace("removePointers","")??"";const l=e.name,r=$(p,l,n);let o;try{o=k(r,c,a,t,d,"parent")}catch(e){continue}if(o)return o}if(p.textContent&&!p.textContent)if(!l||l&&!r(p.textContent)){let e;e=n.test(p.textContent)?m(p)?`//*[local-name()='${p.tagName}' and .=${i(o(p))}]`:`//${p.tagName||"*"}[.=${i(o(p))}]`:m(p)?`//*[local-name()='${p.tagName}' and normalize-space(.)=${i(o(p))}]`:`//${p.tagName||"*"}[normalize-space(.)=${i(o(p))}]`;const l=k(e,c,a,t,d,"parent");if(l)return l}else{const e=[],n=[...new Set(o(p).match(/([^0-9]+)/g))],l=new RegExp(/^[\S]+( [\S]+)*$/gi);if(n?.length)for(let t=0;t<n?.length;t++)n[t]&&u(n[t].trim())&&(l.test(n[t])?e.push(`contains(.,${i(n[t].trim()).trim()})`):e.push(`contains(.,${i(u(n[t])).trim()})`));if(e?.length){const n=m(p)?`//*[local-name()='${p.tagName}' and ${e.join(" and ")}]`:`//${p.tagName||"*"}[${e.join(" and ")}]`,l=k(n,c,a,t,d,"parent");if(l)return l}}if(!e){const e=m(p)?`/*[local-name()='${p.tagName}']`:`/${p.tagName}`;h.unshift(e)}p=p.parentElement}const g=h.join("")+c;if(1===s(g,e,t))return R.set(e,g),g}catch(e){return console.error(e),null}},T=(e,t,n,a)=>{const l=new Map;if(l.has(e))return l.get(e);const r=[];let o=e;try{for(;o&&1===o.nodeType;){if(!o.tagName)return"";let e=!1;for(const n of Array.from(o.attributes))if(f(n,o,a)){let a=n.nodeValue;a=a?.replace("removePointers","")??"";const l=n.name,i=$(o,l,a);let c;try{c=s(i,o,t)}catch(e){continue}if(1===c){r.unshift(i),e=!0;break}}if(!e&&o.textContent&&!n.textContent){const n=g(o);if(n){if(1===s(n,o,t)){e=!0,r.unshift(n);break}}}if(e)break;{const e=m(o)?`/*[local-name()='${o.tagName}']`:`/${o.tagName}`;r.unshift(e)}o=o.parentElement}const i=r.join("");return l.set(e,i),i}catch(e){return console.log(e),null}},P=(e,t,n)=>{const a=[];let l;const r=[];null!=e.firstElementChild&&e.firstElementChild.classList.contains("flntooltip")?r.unshift(e.firstElementChild):null!=e.nextElementSibling&&r.unshift(e.nextElementSibling);for(let t=e.parentElement;null!=t&&1===t.nodeType;t=t.parentElement)t.nextElementSibling&&r.unshift(t.nextElementSibling);try{do{let e=!1;for(l=r.pop();null!==l;){for(const n of Array.from(l.attributes))if(f(n,l,!0)){let r=n.nodeValue;r=r?.replace("removePointers","")??"";const o=n.name,i=$(l,o,r);let c;try{c=s(i,l,t)}catch(e){continue}if(1===c){e=!0,a.push(i);break}}if(!e&&l.textContent&&!n.textContent){const n=g(l);if(n){if(1===s(n,l,t)){e=!0,a.push(n);break}}}if(e)break;{const e=m(l)?`/*[local-name()='${l.tagName}']`:`/${l.tagName}`;a.push(e),null!=l.firstElementChild?(r.push(l.nextElementSibling),l=l.firstElementChild):l=l.nextElementSibling}}}while(r.length>0);const o=a.join("");return w.set(e,o),o}catch(e){return console.log(e),null}},A=(e,t,n,a)=>{try{const l=document.querySelector(".flntooltip");for(let r=e.nextElementSibling;null!==r&&r!==l;r=r.nextElementSibling)D(r,e,t,a,"preceding-sibling",n);for(let r=e.previousElementSibling;null!==r&&r!==l;r=r.previousElementSibling)D(r,e,t,a,"following-sibling",n)}catch(e){return console.error("sibling error",e),null}},D=(e,t,n,a,l,r)=>{try{if(e.hasAttributes())for(const t of Array.from(e.attributes)){let o=p(e,{name:t.name,value:t.value},r);if(o){o+=`/${l}::${a}`;const t=s(o,e,n);if(1===t)return void x.push({key:`xpath by ${l}`,value:o});t>1&&(b.length?t<b[0].count&&(b.pop(),b.push({key:`relative xpath by ${l}`,value:o,count:t})):b.push({key:`relative xpath by ${l}`,value:o,count:t}))}}if(!r){let t;if(t=p(e,{name:"text",value:e.textContent},r),t){const a=s(t,e,n);if(1===a)return void x.push({key:`xpath by ${l}`,value:t});a>1&&(b.length?a<b[0].count&&(b.pop(),b.push({key:`relative xpath by ${l}`,value:t,count:a})):b.push({key:`relative xpath by ${l}`,value:t,count:a}))}}}catch(e){console.log(`${l} xpath-error`,e)}};const Y=(e,t,n,a)=>{if(""!=(e.textContent??"").trim()){const l=(e=>{const t=e?.textContent;return t})(e);if(l)return h(e,t,".",l,n,a)}},L=(e,t,a,l,r)=>{const o=e;try{o.map(e=>{if("className"!==e.name&&"class"!==e.name&&f(e,t,r)){const n=((e,t,n,a,l)=>{let r;return r=e.name,h(t,n,`@${r}`,e.value,a,l)})(e,t,a,l,r);n&&x.push({key:`xpath by ${e.name}${l?" index":""}`,value:n})}});const i=Y(t,a,l,r);if(i&&x.push({key:"xpath by text"+(l?" index":""),value:i}),o.find(e=>"className"===e.name)&&f(o?.find(e=>"className"===e.name),t,r)){let e=((e,t,n,a)=>{let l=e.className;if("string"!=typeof l&&(l=""),l=l?.replace("flndisabled","disabled"),l=l?.replace("removePointers",""),l=l?.trim(),l)return h(e,t,"@class",l,n,a)})(t,a,l,r);e&&x.push({key:"xpath by class",value:e})}if(!x.length){const n=function(e,t,n,a){const{tagName:l}=t,r=t.textContent.trim();for(const o of e)if(f(o,t,a)){let e=o.nodeValue;e=e?.replace("removePointers","")??"";const a=`//${l}[@${o.name}='${e}' and text()='${r}']`;if(a&&1==s(a,t,n))return a}}(e,t,a,r);n&&x.push({key:"xpath by textAttribute",value:n})}if(!x.length&&o.length>1){const e=((e,t,a,l)=>{try{const r=[];if(a.length>1)for(const o of a)if(f(o,e,l)){const a=o.nodeValue;n.test(a)?"class"===o.nodeName?r.push(`contains(@${o.nodeName},"${a}")`):r.push(`@${o.nodeName}="${a}"`):r.push(`normalize-space(@${o.nodeName})="${a}"`);const l=m(e)?`//*[local-name()='${e.tagName}' and ${r.join(" and ")}]`:`//${e.tagName}[${r.join(" and ")}]`;let i;try{i=s(l,e,t)}catch(e){continue}if(1===i&&!l.includes("and"))return"";if(1===i&&l.includes("and"))return l}}catch(e){console.log(`'${JSON.stringify(e,null,2)}'`)}})(t,a,o,r);e&&x.push({key:"xpath by combination",value:e})}}catch(e){console.log(e)}},_=(e,t,n,a,l=[])=>{x=[],console.log(e);const r=e,o=r?.ownerDocument||t,i=r.tagName,{attributes:u}=r,f=l.length>0?l:Array.from(u);if(L(f,r,o,n,a),x.length){const t=x.length;for(let n=0;n<t;n++){let t=x[n].value;t="//*"+t.substring(t.indexOf("//")+2+i.length);1===s(t,e,o)&&x.push({key:`${x[n].key} regex`,value:t})}}return x.length||((e,t,n,a,l)=>{try{let r,o,i=[];if(x=[],console.log(l),l)for(const t of l){let n=p(e,{name:t.name,value:t.value},a);console.log(n),n&&i.push(n)}if(e.textContent){let t=p(e,{name:"text",value:e.textContent},a);console.log(t),t&&i.push(t)}if(i.push(e.tagName),i?.length)for(let l=0;l<i.length;l++)if(!x.length){if(A(e,t,a,i[l]),!x.length&&(r||(r=T(e.parentElement,t,e,a)),console.log(r),r&&(r.includes("@")||r.includes("text()")||r.includes(".="))&&r.match(/\//g)?.length-2<5)){const a=r+`/${i[l]}`,o=s(a,e,t);if(1===o)x.push({key:"relative xpath by relative parent",value:a});else if(o>1&&n){const l=c(a,e,t,o);l&&1===s(l,e,t)&&x.push({key:"relative xpath by relative parent "+(n?"index":""),value:l})}}if(!x.length&&(o||(o=P(e,t,e)),o&&(o.includes("@")||o.includes("text()")||o.includes(".=")))){const a=`/${i[l]+o.substring(1)}`,r=s(a,e,t);if(1===r)x.push({key:"relative xpath by relative child",value:a});else if(r>1&&n){const l=c(a,e,t,r);l&&1===s(l,e,t)&&x.push({key:"relative xpath by relative parent "+(n?"index":""),value:l})}}if(1===x?.length&&x?.[0]?.value?.match(/\[([0-9]+)\]/gm)?.length>3&&e.textContent){const a=Y(e,t,n,!1);a&&x.unshift({key:"xpath by text"+(n?"index":""),value:a})}if(!x.length){let r=C(e.parentElement,t,e,a,i[l],n);r&&x.push({key:"xpath by unique parent",value:r})}}return x}catch(e){console.log(e)}})(r,o,n,a,Array.from(r.attributes)),x};let O=[];const j=(e,t,n)=>{try{const a=n.querySelectorAll(e);return 1===a.length&&a[0]===t}catch{return!1}},V=(e,t="single")=>{const n=[],a=e.getRootNode();try{const l=q(e);if(l&&j(l,e,a)&&(n.push({key:"cssSelector by id",value:l}),"single"===t))return n;const r=I(e);r&&j(r,e,a)&&n.push({key:"cssSelector by class",value:r});const o=H(e);o&&n.push({key:"cssSelector by name",value:o});const s=M(e);s&&j(s,e,a)&&n.push({key:"Absolute cssSelector",value:s})}catch(e){console.error(e)}return n},q=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if(e.id&&!r(e.id)){t+=`#${CSS.escape(e.id)}`,a.unshift(t);break}{let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}a.unshift(t),e=e.parentElement}return a.join(" > ")},z=new Set(["id","class","style"]);function X(e){return Array.from(e.attributes).filter(e=>!z.has(e?.name?.toLowerCase())&&e.value&&!r(e.value)).map(e=>{return`[${e.name}="${t=e.value,String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"]`;var t})}const H=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.getRootNode(),a=e.tagName.toLowerCase();if("style"===a||"script"===a)return;const l=X(e);for(const t of l){const l=`${a}${t}`;if(j(l,e,n))return l}},I=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const a=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if("string"!=typeof e.className||!e.className||r(e.className)||O?.find(t=>t.element===e&&"class"===t.attributeName)){let n=e,a=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&a++;1!==a&&(t+=`:nth-of-type(${a})`),1===a&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${a})`)}else if(e.classList.remove("marked-element-temp"),e.classList.remove("removePointers"),e.className){t+=`.${e.className.trim().replace(/\s+/g,".")}`,a.unshift(t);break}a.unshift(t),e=e.parentElement}return a.join(" > ")},M=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=[];for(;e&&e.nodeType===Node.ELEMENT_NODE;){const t=e.tagName.toLowerCase();if("style"===t||"script"===t)return;let a=t;const l=e.parentNode;if(l){const t=Array.from(l.children).filter(t=>t.tagName===e.tagName);t.length>1&&(a+=`:nth-of-type(${t.indexOf(e)+1})`)}n.unshift(a),e=e.parentElement}return n.join(" > ")},W=(e,t,n)=>{const a=y(e);if(te(t,a)===n)return!0;if(function(e){return/\[(?:\s*\.|\s*contains\s*\(\s*\.|\s*normalize-space\s*\(\s*\.)/.test(e)}(a)){const e=t.evaluate(a,t,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);for(let t=0;t<e.snapshotLength;t++)if(e.snapshotItem(t)===n)return!0}return!1},F=(e,t)=>{try{const n=e.querySelector(t);if(n)return n}catch(e){return console.error("Invalid CSS selector:",t,e),null}return B(e.body,t)},G=(e,t,n)=>F(t,e)===n,U=e=>e.name?.toLowerCase().includes("cssselector")??!1,B=(e,t)=>{const n=Array.from(e.querySelectorAll("*"));try{for(let e=0;e<n.length;e++)if(n[e].shadowRoot){const{shadowRoot:a}=n[e];if(a){const e=B(a,t);if(e)return e;if(a&&!t.includes("dynamic"))return a.querySelector(t)}}}catch(e){console.log(e)}return null},J=e=>e?.id||null,Z=e=>e.className||null,K=e=>e.textContent?.trim()||null,Q=e=>{const t=e;if(t.hasAttribute("name")){return`${t.getAttribute("name")}`||null}return null},ee=["/preceding-sibling","/following-sibling","/parent","/descendant","/ancestor","/self","/ancestor-or-self","/child","/preceding","/following"];function te(e,t){const n=e.defaultView;if(!n)return null;return(new n.XPathEvaluator).evaluate(t,e,null,n.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}function ne(e,t,n){if(e.includes(t)){const a=e.split(t)[0].trim(),l=n.defaultView;if(!l)return null;if(!e.includes("dynamic")){const t=new l.XPathEvaluator;if(t.evaluate(a,n,null,l.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue){let a;return t.evaluate(e,n,null,l.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue?(a=e,a):(console.error("Complete Locator is Invalid:",e),a=e,a)}console.error("Source Locator Not Found:",a)}}return null}const ae=(e,t)=>{const n=t.querySelectorAll("script, style, link[rel='stylesheet'], meta, noscript, embed, object, param, source, svg");n&&n.forEach(e=>{e.remove()});const a=new Set;let l=[];function o(e,t={}){const n=e?.value,r=t.value??e?.value,o={name:t.name??e?.name,type:t.type??e?.type,value:t.value??e?.value,reference:t.reference??e?.reference,status:t.status??e?.status,isRecorded:t.isRecorded??e?.isRecorded},s="Y"===e?.isSelfHealed;o.isSelfHealed=s?"Y":t.hasOwnProperty("isSelfHealed")?t.isSelfHealed:((e,t,n)=>t&&n&&t===n?null:"Y")(o.name,n,r),function(e){const t=`${e.name}:${e.value}`;a.has(t)||(a.add(t),l.push(e))}(o)}function s(e,t,n){if(U(t))return F(e,n);if(t.name.includes("id")||n.startsWith("#"))return e.querySelector("#"+N(n));if(t.name.includes("className")||n.startsWith("."))return e.querySelector("."+n);if("name"===t.name){const t=N(n);return e.querySelector(`[name="${t}"]`)}if("tagName"===t.name)return e.querySelector(n);if("linkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.trim()===n)||null;if("partialLinkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.includes(n))||null;if(!t.name.includes("xpath")&&!n.startsWith("//")||t.type.match("dynamic"))return e.querySelector(n);{const a=te(e,y(n));return a&&o(t,{value:n,isRecorded:String(t.isRecorded).includes("N")?"N":"Y"}),a}}function i(e,t,n){const a=e.querySelectorAll("iframe");for(const e of a)try{const a=e.contentDocument||e.contentWindow?.document;if(!a)continue;const l=s(a,t,n);if(l)return l}catch{continue}return null}const c=(e,t,n)=>{if(!e)return null;let a=e.trim();return a&&"null"!==a.toLowerCase()?(a=a.replace(/\\"/g,'"').replace(/\\'/g,"'"),a=a.replace(/^['"](.+?)['"]$/,"$1"),a=a.replace(/''/g,"'"),a=a.replace(/\[@(id|name)=['"]{2}(.+?)['"]{2}\]/g,"[@$1='$2']"),"id"!==t&&"name"!==t||(a=a.replace(/['"]/g,"").trim()),"xpath"!==t||"Y"!==n||e.startsWith("//")?!a||/^['"]{2}$/.test(a)?null:a:null):null};e:for(const n of e.locators)try{String(n.isRecorded||"");const u=e.locators.filter(e=>"N"===e.isRecorded);if(u.length>0)for(const e of u)o(e);const f=String(n.value||n.type||"");if(f.includes("dynamic")||f.match("dynamic")||f.includes("{")||f.includes("}")){o(n);continue}if(e.isShared.includes("Y"))break e;try{let u=null;const f=n.value.split(">>>");for(const e of f){if(!t){console.error("Element not found at:",e);break}const a=e.trim();if(u=s(t,n,a),u||(u=i(t,n,a)),u||(u=B(t.body,a)),!u){console.error("Element not found at:",a);break}}const m=(e,t)=>{const n=`${e}:${t}`;return a.has(n)};if(u){const n=e.locators.filter(e=>"xpath"===e.name&&e.value),a=e.locators.filter(e=>U(e)&&e.value);for(const e of n)W(e.value,t,u)&&o(e,{isSelfHealed:null});for(const e of a)G(e.value,t,u)&&o(e,{isSelfHealed:null});const s=l.filter(e=>"xpath"===e.name&&e.value),i=new Set(s.map(e=>v(e.value))),f=[],d=J(u);if(d&&!m("id",d)&&!r(d)){const n=e.locators.find(e=>"id"===e.name);E(t,"id",d,u)&&(f.push("id"),o(n,{name:"id",type:"static",isRecorded:"Y",value:d}))}const h=u.tagName;if(h&&!m("tagName",h)){const n=e.locators.find(e=>"tagName"===e.name);E(t,"tagName",h,u)&&(f.push("tagName"),o(n,{name:"tagName",type:"static",isRecorded:"Y",value:h}))}const p=K(u);if(p&&!r(p)){const n=e.locators.find(e=>"linkText"===e.name);E(t,"linkText",p,u)&&(f.push("linkText"),o(n,{name:"linkText",type:"static",isRecorded:"Y",value:p}))}const g=Q(u);if(g&&!m("name",g)&&!r(g)){const n=e.locators.find(e=>"name"===e.name);E(t,"name",g,u)&&(f.push("name"),o(n,{name:"name",type:"static",isRecorded:"Y",value:g}))}const $=Z(u);if($&&""!==$.trim()&&!$.includes(" ")&&!m("className",$)&&!r($)){const n=e.locators.find(e=>"className"===e.name);E(t,"className",$,u)&&(f.push("className"),o(n,{name:"className",type:"static",isRecorded:"Y",value:$}))}V(u,"single").forEach(e=>{e.value&&!m("cssSelector",e.value)&&o(void 0,{name:"cssSelector",value:e.value,type:"static",isRecorded:"Y"})});const y=Array.from(u.attributes).filter(e=>!f.includes(e.name));let N=[];try{N=_(u,t,!1,!0,y)??[]}catch(e){console.error("Error generating XPath candidates:",e)}if(0!==N?.length){const e=n.filter(e=>!W(e.value,t,u));let a=0;for(const t of e){if(a>=e.length)break;const n=v(t.value);if(i.has(n))continue;const l=N.find(e=>e.value&&v(e.value)===n);l?.value&&(o(t,{name:"xpath",value:l.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),i.add(n),a++)}if(a<e.length)for(const t of N){if(a>=e.length)break;if(!t.value)continue;const n=v(t.value);i.has(n)||(o(t,{name:"xpath",value:t.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),i.add(n),a++)}}for(const n of e.locators)try{for(const a of e.locators)if(a.value)for(const e of ee)if(a.value.includes(e)){const l=ne(a.value,e,t);if(l){o(a,{name:"xpath",value:l,isRecorded:""!==n.isRecorded&&null!==n.isRecorded?n.isRecorded:"Y"});break}}}catch(e){console.error("Error processing locator:",n,e)}if(l.length<5){const e=[{name:"id",value:J(u)},{name:"name",value:Q(u)},{name:"className",value:Z(u)},{name:"tagName",value:u.tagName},{name:"linkText",value:K(u)}];for(const n of e){if(l.length>4)break;const{name:e,value:a}=n;a&&(r(a)||m(e,a)||"className"===e&&a.includes(" ")||E(t,e,a,u)&&o(void 0,{name:e,type:"static",value:a,isRecorded:"Y",isSelfHealed:"Y"}))}l.length<5&&V(u,"multiple").forEach(e=>{l.length>4||e.value&&(m("cssSelector",e.value)||E(t,"cssSelector",e.value,u)&&o(void 0,{name:"cssSelector",type:"static",value:e.value,isRecorded:"Y",isSelfHealed:"Y"}))})}const x=l.map(e=>({...e,value:c(e.value,e.name,e.isRecorded)})),b=[{name:`${e.name}`,desc:`${e.desc}`,type:`${e.type}`,locators:x.filter(e=>null!=e?.value&&""!==e.value),isShared:`${e.isShared}`,projectId:`${e.projectId}`,projectType:`${e.projectType}`,isRecorded:`${e.isRecorded}`,folder:`${e.folder}`,parentId:`${e.parentId}`,parentName:`${e.parentName}`,platform:`${e.platform}`,licenseId:`${e.licenseId}`,licenseType:`${e.licenseType}`,userId:`${e.userId}`}];return b}}catch(e){console.error("Error processing locator:",n,e);continue}}catch(e){console.error("Error processing locator:",n,e);continue}return null};async function le(){return{fromHTML:async n=>{const a=new e,l=new t(n.replace(/\\"/g,'"').replace(/\\r/g,"\r").replace(/\\n/g,"\n").replace(/\\t/g,"\t"),{pretendToBeVisual:!0,runScripts:"dangerously",resources:"usable",virtualConsole:a}),{window:r}=l;global.document=r.document,global.Node=r.Node,global.Element=r.Element,global.HTMLElement=r.HTMLElement,global.SVGElement=r.SVGElement;const o=r.CSS??{};return o.escape||(o.escape=e=>String(e).replace(/[^a-zA-Z0-9_-]/g,"\\$&")),global.CSS=o,global.XPathResult=r.XPathResult,global.XPathEvaluator=r.XPathEvaluator,{autoHeal:async e=>ae(e,r.document)}}}}export{le as default};
1
+ import{VirtualConsole as e,JSDOM as t}from"jsdom";const n=/^[\S]+( [\S]+)*$/i;let r=new WeakMap;const a=(e,t)=>{const{owner:n,contextNode:a}=(e=>{const t=9===e.nodeType?e:e.ownerDocument||document;return{owner:t,contextNode:1===e.nodeType||9===e.nodeType||11===e.nodeType?e:t}})(t);let o=r.get(a);o||(o=new Map,r.set(a,o));const l=o.get(e);if(l)return l;const s=n.evaluate(e,a,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null),i={first:s.iterateNext(),second:s.iterateNext()};return o.set(e,i),i};let o=[];const l=new Set(["marked-element-temp","removePointers"]),s=e=>(e||"").split(/\s+/).map(e=>e.trim()).filter(e=>e&&!l.has(e)).sort(),i=e=>/\d/.test(e||""),c=(e,t,n,r=!1)=>{const a=s(t),o=a.filter(e=>!!r||!i(e));return o.length?1===a.length&&1===o.length?[`@class=${p(o[0])}`]:o.map(t=>{const r=d(`contains(@class,${p(t)})`,S(e),e.tagName.toLowerCase());return{token:t,count:A(r,e,n)}}).sort((e,t)=>e.token.length!==t.token.length?e.token.length-t.token.length:e.count-t.count).map(({token:e})=>`contains(@class,${p(e)})`):[]},u=new Set(["data-testid","data-test-id","data-test","data-cy","testid"]),f=(e,t)=>t?"class"===e||"className"===e?s(t).join(" "):t.replace("removePointers","").trim():"",m=e=>/\d/.test(e),d=(e,t,n)=>t?`//*[local-name()='${n}' and ${e}]`:`//${n}[${e}]`,h=e=>{const t=e?.textContent;return t},g=(e,t,n,r=!1)=>{try{const{first:o,second:l}=a(e,n);if(!o)return 0;if(!l)return o===t?1:0;if(r&&Array.isArray(t)){let r=0;if(t.includes(o)&&r++,t.includes(l)&&r++,2===r)return 2;const a=(9===n.nodeType?n:n.ownerDocument).evaluate(e,n,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);let s;for(;s=a.iterateNext();)if(t.includes(s)&&(r++,2===r))return 2;return 0}return 2}catch(t){return console.error(`Error evaluating XPath: ${e}`,t),0}},p=e=>{if(e){if(-1!==e.indexOf('"'))return`'${e}'`;if(-1!==e.indexOf("'"))return`"${e}"`}return`'${e}'`},$=e=>e?e.replace(/\s\s+/g," ").trim():e,y=(e,t)=>{const n=$((t||"").trim());if(!e.children.length||n.length<=50)return"";const r=n.split(/\s+/).slice(0,3).join(" ");return r.length>2?`contains(normalize-space(.),${p(r)})`:""},v=e=>{const t=$((e||"").trim());if(!t||!i(t))return{prefix:t,fragment:t};const n=t.split(/[0-9]/)[0].replace(/[₹$€£¥₫₽₩₦₱₲₴₵₡₭₮₺฿៛]+/g,"").trim(),r=n.split(/[^a-zA-Z]+/).map(e=>$(e).trim()).filter(e=>e.length>2&&/[a-zA-Z]/.test(e)).join(" ").trim();return{prefix:n.length>2&&/[a-zA-Z]/.test(n)?n:"",fragment:r.length>2?r:""}},N=e=>v(e).fragment,x=(e,t)=>{const n=e.ownerDocument.implementation.createHTMLDocument("shadow-xpath"),r=n.createElement("div");r.innerHTML=e.innerHTML,n.body.appendChild(r);const a=t=>{if(!(t&&t instanceof e.ownerDocument.defaultView.Element))return null;const n=((e,t)=>{const n=[];let r=t;for(;r&&r.parentElement;){const e=r.parentElement;n.unshift(Array.from(e.children).indexOf(r)),r=e}return r&&r.parentNode===e?(n.unshift(Array.from(e.children).indexOf(r)),n):null})(e,t);return n?((e,t)=>{let n=e;for(const e of t){const t=n.children.item(e);if(!t)return null;n=t}return n})(r,n):null},o=Array.isArray(t)?t.map(e=>a(e)).filter(Boolean):[],l=Array.isArray(t)?null:a(null);return{owner:n,contextNode:r,cloneElement:l,cloneElements:o}},E=(e,t,n)=>{const r=f(e.name,e.value);if(!r||"boolean"==typeof e?.value)return!1;if(["true","false","on","off","flntooltip","flutter-highlight-overlay"].some(e=>e===r))return!1;if(["style","locator-data-tooltip","value"].some(t=>t===e.name))return!1;const a=o?.find(n=>n.doc===t.ownerDocument&&n.element===t&&n.attributeName===e.name);return!a&&(!(0===e?.name?.indexOf("on")&&e?.name?.length>3)&&("function"!=typeof e.value&&(l=e.name,s=r,(!u.has(l.toLowerCase())||!(e=>/(?:^|[-_:])\d{4,}(?:$|[-_:])/.test(e))(s))&&("class"===e.name||!i(r)))));var l,s},S=e=>e instanceof SVGElement,w=(e,t,r,a,o,l)=>{if(a){const{tagName:s}=e;let u,f="";const h=[];let v;const x=r.startsWith("@"),E="."===r||"text()"===r,w=E?".":r,b=E&&/\s/.test(a.trim()),k=E?N(a):a,R=E&&i(a)?k:a,T=E?y(e,R):"";if("@class"===r){for(const n of c(e,a,t))if(v=d(n,S(e),e.tagName.toLowerCase()),u=g(v,e,t),1===u&&!o)return v;return}if(E&&i(a)&&!k)return;if(a&&(!x||!m(a))&&(v=T?d(T,S(e),e.tagName.toLowerCase()):b?d(`normalize-space(${w})=${p($(R)).trim()}`,S(e),e.tagName.toLowerCase()):n.test(a)?`//${s}[${r}=${p(R)}]`:d(`normalize-space(${r})=${p($(R)).trim()}`,S(e),e.tagName.toLowerCase()),u=g(v,e,t),1===u&&!o))return v;if(x&&i(a))return;if(a&&l){const l=a.split(" ");if(l?.length)if(1===l.length){const i=[...new Set(l[0].match(/([^0-9]+)/g))];if(i?.length>=1&&i[0]&&$(i[0].trim())?.length>1&&a.startsWith(i[0])&&(f=n.test(i[0])?`starts-with(${r},${p(i[0]).trim()})`:`starts-with(${r},${p($(i[0])).trim()})`),i?.length>1&&i[i.length-1]&&$(i[i.length-1].trim())?.length>1&&a.endsWith(i[i.length-1])&&(f=n.test(i[i.length-1])?`ends-with(${r},${p(i[i.length-1]).trim()})`:`ends-with(${r},${p($(i[i.length-1])).trim()})`),f?.length&&(v=S(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v}else{const i=l.length%2==0?l.length/2:l.length%2,c=l.slice(0,i).join(" ");let m=[...new Set(c.match(/([^0-9]+)/g))];if(m?.length&&m[0]&&$(m[0].trim())?.length&&a.startsWith(m[0])&&(f=n.test(m[0])?`starts-with(${r},${p(m[0]).trim()})`:`starts-with(${r},${p($(m[0])).trim()})`),f?.length&&(v=S(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v;const d=l.slice(i,l.length-1).join(" ");if(m=[...new Set(d.match(/([^0-9]+)/g))],m?.length&&m[0]&&$(m[0].trim())?.length>3&&a.endsWith(m[0])&&(f=n.test(m[0])?`ends-with(${r},${p(m[0]).trim()})`:`ends-with(${r},${p($(m[0])).trim()})`),f?.length&&(v=S(e)?`//*[local-name()='${s}' and ${f}]`:`//${s}[${f}]`,u=g(v,e,t),1===u&&!o))return v}}if(a&&l&&m(a)){const l=[...new Set(a.match(/([^0-9]+)/g))];if(l?.length)for(let e=0;e<l?.length;e++)l[e]&&$(l[e].trim())?.length>1&&(n.test(l[e])?h.push(`contains(${r},${p(l[e].trim()).trim()})`):h.push(`contains(${r},${p($(l[e])).trim()})`));if(h?.length&&(v=S(e)?`//*[local-name()='${s}' and ${h.join(" and ")}]`:`//${s}[${h.join(" and ")}]`,u=g(v,e,t),1===u&&!o))return v}if(v=S(e)?`//*[local-name()='${s}' and text()]`:`//${s}[text()]`,u=g(v,e,t),1===u&&!o)return v}},b=(e,t,r,a)=>{try{const a=r.filter(t=>E(t,e)),o=r=>{const a=f(r.name,r.nodeValue);return a?"class"===r.name?c(e,a,t):i(a)?[]:n.test(a)?[`@${r.name}="${a}"`]:[`normalize-space(@${r.name})="${a}"`]:[]},l=()=>{const t=$(h(e)?.trim()||""),r=N(t);if(i(t))return r?[`contains(normalize-space(.),${p(r)})`]:[];if(!t||t.length>80||/^\d+$/.test(t)){const n=y(e,t);return n?[n]:[]}const a=new Set,o=y(e,t);return o?(a.add(o),Array.from(a)):(n.test(t)&&t.length<=40&&!t.includes(" ")&&a.add(`text()=${p(t)}`),t.includes(" ")&&a.add(`normalize-space(.)=${p($(t))}`),a.add(`contains(text(),${p(t)})`),Array.from(a))},s=(e,t)=>{const n=[],r=(a,o)=>{if(o.length!==t)for(let t=a;t<e.length;t++)o.push(e[t]),r(t+1,o),o.pop();else n.push([...o])};return r(0,[]),n},u=t=>S(e)?`//*[local-name()='${e.tagName}' and ${t.join(" and ")}]`:`//${e.tagName}[${t.join(" and ")}]`,m=(()=>{if(!(a.length<2))for(let n=2;n<=a.length;n++){const r=s(a,n);for(const n of r){let r=[];for(const e of n){const t=o(e);t.length&&r.push(...t)}if(r.length<2)continue;const a=u(r);let l;try{l=g(a,e,t)}catch{continue}if(1===l)return a}}})();if(m)return m;const d=l();if(!d.length||!a.length)return;for(let n=1;n<=a.length;n++){const r=s(a,n);for(const n of r){let r=[];for(const e of n)r.push(...o(e));if(r.length)for(const n of d){const a=[...r,n],o=u(a);try{if(1===g(o,e,t))return o}catch{continue}}}}}catch(e){console.log(`XPath generation error: ${JSON.stringify(e,null,2)}`)}},k=(e,t)=>{if(!e.textContent)return"";const r=(a=e,a?.childNodes[0]?.nodeValue||"");var a;const o=N(r);if(i(r)&&!o)return"";const l=i(r)?o:r,s=y(e,l);let c;return c=s?d(s,S(e),e.tagName||"*"):i(r)?d(`contains(normalize-space(.),${p(l)})`,S(e),e.tagName||"*"):!n.test(r)||/\s/.test(r.trim())?d(`normalize-space(.)=${p(l)}`,S(e),e.tagName||"*"):S(e)?`//*[local-name()='${e.tagName}' and .=${p(l)}]`:`//${e.tagName||"*"}[.=${p(l)}]`,c},R=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{fragment:t}=v(n);return t?d(`contains(normalize-space(.),${p(t)})`,S(e),e.tagName.toLowerCase()):""}return d(`normalize-space(${t})=${p($(n)).trim()}`,S(e),e.tagName.toLowerCase())},T=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{prefix:t,fragment:r}=v(n),a=t||r;return a?d(`starts-with(normalize-space(.),${p(a)})`,S(e),e.tagName.toLowerCase()):""}return d(`starts-with(${t},${p($(n)).trim()})`,S(e),e.tagName.toLowerCase())},C=(e,t,n)=>{if(("."===t||"text()"===t)&&i(n)){const{fragment:t}=v(n);return t?d(`contains(normalize-space(.),${p(t)})`,S(e),e.tagName.toLowerCase()):""}return d(`contains(${t},${p($(n)).trim()})`,S(e),e.tagName.toLowerCase())},L=(e,t)=>{const r=t.flatMap(t=>(t=>{const r=f(t.name,t.value);if(!r)return[];if("class"===t.name){const t=e.getRootNode?.()??e.ownerDocument;return c(e,r,t)}return i(r)?[]:n.test(r)?[`@${t.name}=${p(r)}`]:[`normalize-space(@${t.name})=${p(r)}`]})(t)).filter(Boolean);if(r.length>=2)return d(`${r[0]} or ${r[1]}`,S(e),e.tagName.toLowerCase());const a=(()=>{const t=$(h(e)?.trim()||""),r=N(t);if(i(t))return r?`contains(normalize-space(.),${p(r)})`:null;const a=y(e,t);return a||(!t||t.length>80||/^\d+$/.test(t)?null:n.test(t)&&t.length<=40&&!t.includes(" ")?`text()=${p(t)}`:t.includes(" ")?`normalize-space(.)=${p(t)}`:`contains(text(),${p(t)})`)})();return 1===r.length&&a?d(`${r[0]} or ${a}`,S(e),e.tagName.toLowerCase()):""},A=(e,t,n)=>{try{return g(e,t,n)}catch(e){return 0}},D=(e,t)=>{const n=[],r=new Set,a=Array.from(e.attributes||[]),o=["id","data-testid","data-test","data-qa","name","aria-label","role"],l=[...o.map(e=>a.find(t=>t.name===e)).filter(Boolean),...a.filter(e=>!o.includes(e.name))],s=a=>{a&&!r.has(a)&&_(a,e,t)&&(r.add(a),n.push(a))};l.forEach(t=>{if(E(t,e))for(const n of M(e,t.name,t.value))s(n)});const i=e.textContent?.trim();if(i&&i.length<40&&0===e.children.length){const t=k(e);t&&s(t)}if(a.length>1){const n=b(e,t,a);n&&s(n)}return n},P=(e,t)=>{const n=[];let r=t;for(;r&&r!==e;)n.unshift(Y(r)),r=r.parentElement;return r===e?n.join("/"):""},z=(e,t)=>{const n=t??(e.getRootNode?.()||e.ownerDocument),r=Y(e),a=S(e)?`//*[local-name()='${e.tagName.toLowerCase()}']`:`//${e.tagName.toLowerCase()}`;for(let t=e.parentElement;t;t=t.parentElement){const a=D(t,n);for(const o of a){console.log(`Trying ancestor XPath: ${o}`);const a=`${o}/descendant::${r}`;console.log(`Trying descendant XPath: ${a}`);const l=A(a,e,n);if(console.log(`Match count: ${l}`),1===l&&O(a,n)===e)return console.log(`Selected XPath: ${a}`),a;const s=P(t,e);if(s){const t=`${o}/${s}`;console.log(`Trying descendant XPath: ${t}`);const r=A(t,e,n);if(console.log(`Match count: ${r}`),1===r&&O(t,n)===e)return console.log(`Selected XPath: ${t}`),t}}}console.log(`Trying descendant XPath: ${a}`);const o=A(a,e,n);return console.log(`Match count: ${o}`),1===o&&O(a,n)===e?(console.log(`Selected XPath: ${a}`),a):(console.log("Selected XPath: "),"")},O=(e,t)=>{try{if(n=t,n?.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&"host"in n){const{owner:n,contextNode:r,cloneElement:a}=x(t);return n.evaluate(e,r,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue||a}return t.evaluate(e,t,null,t.defaultView.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}catch(e){return null}var n},_=(e,t,n)=>{try{const{first:r,second:o}=a(e,n);return!!r&&!o&&r===t}catch{return!1}},Y=e=>S(e)?`*[local-name()='${e.tagName.toLowerCase()}']`:e.tagName.toLowerCase(),X=(e,t,n)=>{if(!(e instanceof Element))return[];const r=new Set,a=[],o=Array.from(e.attributes||[]).filter(t=>{return t?.value&&(n=t.value,!/\d/.test(n))&&E(t,e);var n}),l=(e,t)=>{t&&!r.has(t)&&(r.add(t),a.push({key:e,value:t}))},s=["id","data-testid","data-test","data-qa","name","aria-label","role"];s.forEach(n=>{const r=o.find(e=>e.name===n);if(r)for(const n of M(e,r.name,r.value))n&&_(n,e,t)&&l(`anchor by ${r.name}`,n)}),o.forEach(n=>{if(!s.includes(n.name))for(const r of M(e,n.name,n.value))r&&_(r,e,t)&&l(`anchor by ${n.name}`,r)});const i=e.textContent?.trim();if(i&&i.length<40&&0===e.children.length){const n=k(e);n&&_(n,e,t)&&l("anchor by text",n)}if(o.length>1){const n=b(e,t,o);n&&_(n,e,t)&&l("anchor by combination",n)}const c=z(e,t);return c&&_(c,e,t)&&l("anchor by tag",c),a},M=(e,t,n)=>{const r=new RegExp(/^[\S]+( [\S]+)*$/gi);let a="";if(n=f(t,n)){if("class"===t){const t=e.getRootNode?.()??e.ownerDocument;return c(e,n,t).map(t=>S(e)?`//*[local-name()='${e.tagName}' and ${t}]`:`//${e.tagName||"*"}[${t}]`)}if(i(n))return[];a=r.test(n)?S(e)?`//*[local-name()='${e.tagName}' and @${t}=${p(n)}]`:`//${e.tagName||"*"}[@${t}=${p(n)}]`:S(e)?`//*[local-name()='${e.tagName}' and contains(@${t},${p(n)})]`:`//${e.tagName||"*"}[contains(@${t},${p(n)})]`}return a?[a]:[]},j=e=>e=(e=e.replace(/text\(\)\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1")).replace(/\.\s*=\s*(['"])(.*?)\1/g,"normalize-space(.)=$1$2$1");function H(e){const t=function(e){return e.toLowerCase().replace(/'[^']*'/g,"?").replace(/"[^"]*"/g,"?").replace(/\[\d+\]/g,"[?]").replace(/\/\/[a-z0-9_-]+/g,"//node").replace(/\/[a-z0-9_-]+/g,"/node")}(e),n=function(e){const t=e.toLowerCase(),n=t.match(/(ancestor-or-self|ancestor|descendant-or-self|descendant|following-sibling|preceding-sibling|following|preceding|parent|child|self)::/),r=n?.[1]??"none",a=t.match(/@([a-z0-9:-]+)/);return{axis:r,attribute:a?.[1]??"none",usesNormalize:t.includes("normalize-space")}}(e);return["XPATH",`axis:${n.axis}`,`attr:${n.attribute}`,`normalize:${n.usesNormalize}`,`shape:${t}`].join("|")}function V(e){return e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/ /g,"\\ ")}function q(e,t,n,r){const a=r?.getRootNode?.()??e,o=e=>{try{return Array.from(a.querySelectorAll(e))}catch{return[]}};try{switch(t){case"id":return 1===o(`#${V(n)}`).length;case"name":return 1===o(`[name="${V(n)}"]`).length;case"className":return 1===o(`.${n}`).length;case"tagName":case"cssSelector":return 1===o(n).length;case"linkText":return 1===o("a").filter(e=>e.textContent?.trim()===n).length;case"partialLinkText":return 1===o("a").filter(e=>e.textContent?.includes(n)).length;default:return!1}}catch{return!1}}let I=[];const W={andConditions:"and",orConditions:"or",contains:"contains",startsWith:"startsWith",normalizeSpace:"normalizeSpace",axes:"axes",index:"index",hardCodedText:"text",tillTag:"tag"},F=(e,t)=>t.map(t=>({...t,key:t?.key?.includes(e)?t.key:`${e} ${t.key}`.trim()})),G=(e,t)=>t?e:e.filter(e=>{return t=e.value,!(/(?:^|\/)[^/\[]+\[\d+\](?=\/|$)/.test(t)||/^\(.+\)\[\d+\]$/.test(t));var t}),B=e=>e?.textContent?.replace(/\s+/g," ")?.trim()||"",Z=e=>{if(!(e&&e instanceof Element))return[];const t=[],n=Array.from(e.children||[]);for(;n.length;){const e=n.shift();e.classList?.contains("flntooltip")||(t.push(e),n.push(...Array.from(e.children||[])))}return t},U=(e,t)=>{const n=[];let r="previous"===t?e.previousElementSibling:e.nextElementSibling;for(;r;)r.classList?.contains("flntooltip")||n.push(r),r="previous"===t?r.previousElementSibling:r.nextElementSibling;return n},J=(e,t)=>{const n=[];for(let r=e;r?.parentElement;r=r.parentElement){let e="previous"===t?r.previousElementSibling:r.nextElementSibling;for(;e;)e.classList?.contains("flntooltip")||(n.push(e),n.push({node:e,expand:()=>Z(e)})),e="previous"===t?e.previousElementSibling:e.nextElementSibling}return n},K=e=>{if(!(e instanceof Element))return!0;const t=e.textContent?.trim();return!e.id&&!e.className&&0===e.attributes.length&&(!t||t.length>60)&&e.children.length>2},Q=(e,t,n,r,a,o,l)=>{const s=Y(n),i=[];let c=0;const u=t.includes("descendant")?10:20;for(const a of e){if(c++>u)break;if(i.length>=5)break;const e="node"in a?a.node:a;if(!(e instanceof Element))continue;if(K(e))continue;const f=[()=>X(e,r),()=>{const t=[],n=Array.from(e.attributes||[]).filter(t=>t?.value&&!/\d/.test(t.value)&&E(t,e));for(const r of n){const n=r.value;if(!n)continue;const a=C(e,`@${r.name}`,n);a&&t.push({key:"contains",value:a});const o=T(e,`@${r.name}`,n);o&&t.push({key:"starts-with",value:o})}return t},()=>[{key:"tag",value:z(e,r)}]];for(let a=0;a<f.length;a++){let c=f[a]();if(!c.length)continue;l||(c=c.filter(e=>!/\[\d+\]/.test(e.value)));const u=new Set;c=c.filter(e=>!u.has(e.value)&&(u.add(e.value),!0)),c=c.slice(0,5);for(const e of c){const a=`${e.value}/${t}::${s}`;if(_(a,n,r)){if(i.push({key:o,value:a}),i.length>=2)return i;if(i.length>=5)return i}}if(l&&a===f.length-1)for(const a of c){const l=g(a.value,e,r);for(let e=1;e<=Math.min(l,3);e++){const l=`(${a.value})[${e}]/${t}::${s}`;if(_(l,n,r)){if(i.push({key:o,value:l}),i.length>=2)return i;if(i.length>=5)return i}}}}if("expand"in a&&a.expand){const e=a.expand();let l=0;for(const a of e){if(l++>15)break;if(i.length>=5)break;if(!(a instanceof Element))continue;if(K(a))continue;const e=X(a,r);for(const a of e.slice(0,5)){const e=`${a.value}/${t}::${s}`;if(_(e,n,r)){if(i.push({key:o,value:e}),i.length>=2)return i;if(i.length>=5)return i}}}}}return i},ee=(e,t,n,r)=>{const a=[];for(let t=e.parentElement;t;t=t.parentElement)a.push(t);const o=Z(e),l=U(e,"previous"),s=U(e,"next"),i=J(e,"previous"),c=J(e,"next"),u=(e,t=20)=>e.length>t?e.slice(0,t):e,f=[[{nodes:u(s),axis:"preceding-sibling",key:"preceding-sibling"},{nodes:u(l),axis:"following-sibling",key:"following-sibling"}],[{nodes:u(a),axis:"child",key:"child"},{nodes:u(o,15),axis:"parent",key:"parent"}],[{nodes:u(a),axis:"descendant",key:"descendant"},{nodes:u(a),axis:"descendant-or-self",key:"descendant-or-self"},{nodes:u(o,10),axis:"ancestor",key:"ancestor"},{nodes:u(o,10),axis:"ancestor-or-self",key:"ancestor-or-self"}],[{nodes:u(i),axis:"following",key:"following"},{nodes:u(c),axis:"preceding",key:"preceding"}]];for(const n of f)for(const a of n){const n=Q(a.nodes,a.axis,e,t,0,a.key,r);if(n.length>0)return n}return[]},te=(e,t,n,r,a)=>{const o=(e=>W[e]||e)(r);return"text"===o?F(r,((e,t,n)=>{if(!B(e))return[];if("text"===n){const n=k(e)||ne(e,t,!1,!1);return n?[{key:"xpath by text",value:n}]:[]}return[]})(e,t,o)):["contains","startsWith","normalizeSpace"].includes(o)?F(r,((e,t,n)=>{const r=[],a=Array.from(e.attributes||[]).filter(t=>t?.value&&E(t,e)),o=B(e),l="contains"===n?C:"startsWith"===n?T:"normalizeSpace"===n?R:null;if(!l)return r;if(a.forEach(t=>{const a=l(e,`@${t.name}`,t.value);a&&r.push({key:`xpath by ${n} ${t.name}`,value:a})}),o){const t=l(e,".",o);t&&r.push({key:`xpath by ${n} text`,value:t})}return r})(e,0,o)):["and","or"].includes(o)?F(r,((e,t,n,r)=>{const a=Array.from(e.attributes||[]).filter(t=>t?.value&&E(t,e));if("and"===r){const n=b(e,t,a);return n?[{key:"xpath by combination",value:n}]:[]}if("or"===r&&a.length>=1){const t=L(e,a);return t?[{key:"xpath by or",value:t}]:[]}return[]})(e,t,0,o)):"axes"===o?F(r,ee(e,t,0,!1)):"index"===o?F(r,((e,t)=>{const n=[],r=e.tagName.toLowerCase(),a=Array.from(t.querySelectorAll(r)).indexOf(e)+1;if(a<=0)return[];n.push({key:"xpath by index",value:`(//${r})[${a}]`});let o=e.parentElement;for(;o&&o!==t.body;){if(o.id||o.className){const t=o.id?M(o,"id",o.id):M(o,"class",o.className.toString());for(const a of t){const t=Array.from(o.querySelectorAll(r)).indexOf(e)+1;if(t>0){n.push({key:"xpath by index",value:`(${a}//${r})[${t}]`});break}}if(n.length>1)break}o=o.parentElement}return n})(e,t)):"tag"===o?F(r,[{key:"xpath till tag",value:z(e,t)}]):[]};const ne=(e,t,n,r)=>{if(""!=(e.textContent??"").trim()){const a=h(e);if(a)return w(e,t,".",a,n,r)}},re=(e,t,n,r,a)=>{const o=e;try{o.map(e=>{if("className"!==e.name&&"class"!==e.name&&E(e,t)){const o=((e,t,n,r,a)=>{let o;if(o=e.name,E(e,t))return w(t,n,`@${o}`,e.value,r,a)})(e,t,n,r,a);o&&I.push({key:`xpath by ${e.name}${r?" index":""}`,value:o})}});const l=ne(t,n,r,a);if(l&&I.push({key:"xpath by text"+(r?" index":""),value:l}),o.find(e=>"className"===e.name)&&E(o?.find(e=>"className"===e.name),t)){let e=((e,t,n,r)=>{let a=e.className;if("string"!=typeof a&&(a=""),a=a?.replace("flndisabled","disabled"),a=f("class",a),a=a?.trim(),a&&E({name:"class",value:a},e))return w(e,t,"@class",a,n,r)})(t,n,r,a);e&&I.push({key:"xpath by class",value:e})}if(!I.length&&o.length>1){const e=b(t,n,o);e&&I.push({key:"xpath by combination",value:e})}if(!I.length){const r=function(e,t,n){const{tagName:r}=t,a=t.textContent.trim();if(!a)return;const o=$(a),l=N(o);if(i(o)&&!l)return;const s=i(o)?l:o,c=y(t,s);for(const l of e)if(E(l,t)){let e=f(l.name,l.nodeValue);const u=`//${r}[@${l.name}='${e}' and ${c||(i(o)?`contains(normalize-space(.),${p(s)})`:/\s/.test(a)?`normalize-space(.)=${p(s)}`:`text()=${p(s)}`)}]`;if(u&&1==g(u,t,n))return u}}(e,t,n);r&&I.push({key:"xpath by textAttribute",value:r})}}catch(e){console.log(e)}},ae=(e,t,n,r,a=[],o=[])=>{I=[],console.log(e);const l=e,s=l?.getRootNode?.(),i=s?.nodeType===Node.DOCUMENT_FRAGMENT_NODE,c=i?s:l?.ownerDocument||t,u=l.tagName,{attributes:f}=l,m=a.length>0?a:Array.from(f);if(re(m,l,c,n,r),o.length){const e=o.flatMap(e=>te(l,c,0,e));return I=(e=>{const t=new Set;return e.filter(e=>!(!e?.value||t.has(e.value)||(t.add(e.value),0)))})(e.filter(e=>1===g(e.value,l,c))),G(I,n)}if(I.length){const t=I.length;for(let n=0;n<t;n++){let t=I[n].value;t="//*"+t.substring(t.indexOf("//")+2+u.length);1===g(t,e,c)&&I.push({key:`${I[n].key} regex`,value:t})}}return I.length||i||(I=ee(l,c,0,n)),G(I,n)};let oe=[];const le=(e,t,n)=>{try{const r=n.querySelectorAll(e);return 1===r.length&&r[0]===t}catch{return!1}},se=(e,t="single")=>{const n=[],r=e.getRootNode();try{const a=ie(e);if(a&&le(a,e,r)&&(n.push({key:"cssSelector by id",value:a}),"single"===t))return n;const o=me(e);o&&le(o,e,r)&&n.push({key:"cssSelector by class",value:o});const l=fe(e);l&&n.push({key:"cssSelector by name",value:l})}catch(e){console.error(e)}return n},ie=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const r=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if(e.id&&!m(e.id)){t+=`#${CSS.escape(e.id)}`,r.unshift(t);break}{let n=e,r=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&r++;1!==r&&(t+=`:nth-of-type(${r})`),1===r&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${r})`)}r.unshift(t),e=e.parentElement}return r.join(" > ")},ce=new Set(["id","class","style"]);function ue(e){return Array.from(e.attributes).filter(e=>!ce.has(e?.name?.toLowerCase())&&e.value&&!m(e.value)).map(e=>{return`[${e.name}="${t=e.value,String(t).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"]`;var t})}const fe=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.getRootNode(),r=e.tagName.toLowerCase();if("style"===r||"script"===r)return;const a=ue(e);for(const t of a){const a=`${r}${t}`;if(le(a,e,n))return a}},me=e=>{const t=e.ownerDocument?.defaultView;if(!(t&&e instanceof t.Element))return;const n=e.tagName.toLowerCase();if(n.includes("style")||n.includes("script"))return;const r=[];for(;e?.nodeType===Node.ELEMENT_NODE;){let t=e.nodeName?.toLowerCase();if("string"!=typeof e.className||!e.className||m(e.className)||oe?.find(t=>t.element===e&&"class"===t.attributeName)){let n=e,r=1;if(n.previousElementSibling)for(;n=n.previousElementSibling;)n.nodeName?.toLowerCase()===t&&r++;1!==r&&(t+=`:nth-of-type(${r})`),1===r&&n?.parentElement?.childElementCount>1&&(t+=`:nth-child(${r})`)}else if(e.classList.remove("marked-element-temp"),e.classList.remove("removePointers"),e.className){t+=`.${e.className.trim().replace(/\s+/g,".")}`,r.unshift(t);break}r.unshift(t),e=e.parentElement}return r.join(" > ")},de=(e,t,n)=>{const r=j(e);if(Se(t,r)===n)return!0;if(function(e){return/\[(?:\s*\.|\s*contains\s*\(\s*\.|\s*normalize-space\s*\(\s*\.)/.test(e)}(r)){const e=t.evaluate(r,t,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);for(let t=0;t<e.snapshotLength;t++)if(e.snapshotItem(t)===n)return!0}return!1},he=(e,t)=>{try{const n=e.querySelector(t);if(n)return n}catch(e){return console.error("Invalid CSS selector:",t,e),null}return $e(e.body,t)},ge=(e,t,n)=>he(t,e)===n,pe=e=>e.name?.toLowerCase().includes("cssselector")??!1,$e=(e,t)=>{const n=Array.from(e.querySelectorAll("*"));try{for(let e=0;e<n.length;e++)if(n[e].shadowRoot){const{shadowRoot:r}=n[e];if(r){const e=$e(r,t);if(e)return e;if(r&&!t.includes("dynamic"))return r.querySelector(t)}}}catch(e){console.log(e)}return null},ye=e=>e?.id||null,ve=e=>e.className||null,Ne=e=>e.textContent?.trim()||null,xe=e=>{const t=e;if(t.hasAttribute("name")){return`${t.getAttribute("name")}`||null}return null},Ee=["/preceding-sibling","/following-sibling","/parent","/descendant","/ancestor","/self","/ancestor-or-self","/child","/preceding","/following"];function Se(e,t){const n=e.defaultView;if(!n)return null;return(new n.XPathEvaluator).evaluate(t,e,null,n.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue}function we(e,t,n){if(e.includes(t)){const r=e.split(t)[0].trim(),a=n.defaultView;if(!a)return null;if(!e.includes("dynamic")){const t=new a.XPathEvaluator;if(t.evaluate(r,n,null,a.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue){let r;return t.evaluate(e,n,null,a.XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue?(r=e,r):(console.error("Complete Locator is Invalid:",e),r=e,r)}console.error("Source Locator Not Found:",r)}}return null}const be=(e,t)=>{const n=t.querySelectorAll("script, style, link[rel='stylesheet'], meta, noscript, embed, object, param, source, svg");n&&n.forEach(e=>{e.remove()});const r=new Set;let a=[];function o(e,t={}){const n=e?.value,o=t.value??e?.value,l={name:t.name??e?.name,type:t.type??e?.type,value:t.value??e?.value,reference:t.reference??e?.reference,status:t.status??e?.status,isRecorded:t.isRecorded??e?.isRecorded},s="Y"===e?.isSelfHealed;l.isSelfHealed=s?"Y":t.hasOwnProperty("isSelfHealed")?t.isSelfHealed:((e,t,n)=>t&&n&&t===n?null:"Y")(l.name,n,o),function(e){const t=`${e.name}:${e.value}`;r.has(t)||(r.add(t),a.push(e))}(l)}function l(e,t,n){if(pe(t))return he(e,n);if(t.name.includes("id")||n.startsWith("#"))return e.querySelector("#"+V(n));if(t.name.includes("className")||n.startsWith("."))return e.querySelector("."+n);if("name"===t.name){const t=V(n);return e.querySelector(`[name="${t}"]`)}if("tagName"===t.name)return e.querySelector(n);if("linkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.trim()===n)||null;if("partialLinkText"===t.name)return Array.from(e.querySelectorAll("a")).find(e=>e.textContent?.includes(n))||null;if(!t.name.includes("xpath")&&!n.startsWith("//")||t.type.match("dynamic"))return e.querySelector(n);{const r=Se(e,j(n));return r&&o(t,{value:n,isRecorded:String(t.isRecorded).includes("N")?"N":"Y"}),r}}function s(e,t,n){const r=e.querySelectorAll("iframe");for(const e of r)try{const r=e.contentDocument||e.contentWindow?.document;if(!r)continue;const a=l(r,t,n);if(a)return a}catch{continue}return null}const i=(e,t,n)=>{if(!e)return null;let r=e.trim();return r&&"null"!==r.toLowerCase()?(r=r.replace(/\\"/g,'"').replace(/\\'/g,"'"),r=r.replace(/^['"](.+?)['"]$/,"$1"),r=r.replace(/''/g,"'"),r=r.replace(/\[@(id|name)=['"]{2}(.+?)['"]{2}\]/g,"[@$1='$2']"),"id"!==t&&"name"!==t||(r=r.replace(/['"]/g,"").trim()),"xpath"!==t||"Y"!==n||e.startsWith("//")?!r||/^['"]{2}$/.test(r)?null:r:null):null};e:for(const n of e.locators)try{String(n.isRecorded||"");const c=e.locators.filter(e=>"N"===e.isRecorded);if(c.length>0)for(const e of c)o(e);const u=String(n.value||n.type||"");if(u.includes("dynamic")||u.match("dynamic")||u.includes("{")||u.includes("}")){o(n);continue}if(e.isShared.includes("Y"))break e;try{let c=null;const u=n.value.split(">>>");for(const e of u){if(!t){console.error("Element not found at:",e);break}const r=e.trim();if(c=l(t,n,r),c||(c=s(t,n,r)),c||(c=$e(t.body,r)),!c){console.error("Element not found at:",r);break}}const f=(e,t)=>{const n=`${e}:${t}`;return r.has(n)};if(c){const n=e.locators.filter(e=>"xpath"===e.name&&e.value),r=e.locators.filter(e=>pe(e)&&e.value);for(const e of n)de(e.value,t,c)&&o(e,{isSelfHealed:null});for(const e of r)ge(e.value,t,c)&&o(e,{isSelfHealed:null});const l=a.filter(e=>"xpath"===e.name&&e.value),s=new Set(l.map(e=>H(e.value))),u=[],d=ye(c);if(d&&!f("id",d)&&!m(d)){const n=e.locators.find(e=>"id"===e.name);q(t,"id",d,c)&&(u.push("id"),o(n,{name:"id",type:"static",isRecorded:"Y",value:d}))}const h=c.tagName;if(h&&!f("tagName",h)){const n=e.locators.find(e=>"tagName"===e.name);q(t,"tagName",h,c)&&(u.push("tagName"),o(n,{name:"tagName",type:"static",isRecorded:"Y",value:h}))}const g=Ne(c);if(g&&!m(g)){const n=e.locators.find(e=>"linkText"===e.name);q(t,"linkText",g,c)&&(u.push("linkText"),o(n,{name:"linkText",type:"static",isRecorded:"Y",value:g}))}const p=xe(c);if(p&&!f("name",p)&&!m(p)){const n=e.locators.find(e=>"name"===e.name);q(t,"name",p,c)&&(u.push("name"),o(n,{name:"name",type:"static",isRecorded:"Y",value:p}))}const $=ve(c);if($&&""!==$.trim()&&!$.includes(" ")&&!f("className",$)&&!m($)){const n=e.locators.find(e=>"className"===e.name);q(t,"className",$,c)&&(u.push("className"),o(n,{name:"className",type:"static",isRecorded:"Y",value:$}))}se(c,"single").forEach(e=>{e.value&&!f("cssSelector",e.value)&&o(void 0,{name:"cssSelector",value:e.value,type:"static",isRecorded:"Y"})});const y=Array.from(c.attributes).filter(e=>!u.includes(e.name));let v=[];try{v=ae(c,t,!1,!0,y)??[]}catch(e){console.error("Error generating XPath candidates:",e)}if(0!==v?.length){const e=n.filter(e=>!de(e.value,t,c));let r=0;for(const t of e){if(r>=e.length)break;const n=H(t.value);if(s.has(n))continue;const a=v.find(e=>e.value&&H(e.value)===n);a?.value&&(o(t,{name:"xpath",value:a.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),r++)}if(r<e.length)for(const t of v){if(r>=e.length)break;if(!t.value)continue;const n=H(t.value);s.has(n)||(o(t,{name:"xpath",value:t.value,type:"static",isRecorded:"Y",isSelfHealed:"Y"}),s.add(n),r++)}}for(const n of e.locators)try{for(const r of e.locators)if(r.value)for(const e of Ee)if(r.value.includes(e)){const a=we(r.value,e,t);if(a){o(r,{name:"xpath",value:a,isRecorded:""!==n.isRecorded&&null!==n.isRecorded?n.isRecorded:"Y"});break}}}catch(e){console.error("Error processing locator:",n,e)}if(a.length<5){const e=[{name:"id",value:ye(c)},{name:"name",value:xe(c)},{name:"className",value:ve(c)},{name:"tagName",value:c.tagName},{name:"linkText",value:Ne(c)}];for(const n of e){if(a.length>4)break;const{name:e,value:r}=n;r&&(m(r)||f(e,r)||"className"===e&&r.includes(" ")||q(t,e,r,c)&&o(void 0,{name:e,type:"static",value:r,isRecorded:"Y",isSelfHealed:"Y"}))}a.length<5&&se(c,"multiple").forEach(e=>{a.length>4||e.value&&(f("cssSelector",e.value)||q(t,"cssSelector",e.value,c)&&o(void 0,{name:"cssSelector",type:"static",value:e.value,isRecorded:"Y",isSelfHealed:"Y"}))})}const N=a.map(e=>({...e,value:i(e.value,e.name,e.isRecorded)})),x=[{name:`${e.name}`,desc:`${e.desc}`,type:`${e.type}`,locators:N.filter(e=>null!=e?.value&&""!==e.value),isShared:`${e.isShared}`,projectId:`${e.projectId}`,projectType:`${e.projectType}`,isRecorded:`${e.isRecorded}`,folder:`${e.folder}`,parentId:`${e.parentId}`,parentName:`${e.parentName}`,platform:`${e.platform}`,licenseId:`${e.licenseId}`,licenseType:`${e.licenseType}`,userId:`${e.userId}`}];return x}}catch(e){console.error("Error processing locator:",n,e);continue}}catch(e){console.error("Error processing locator:",n,e);continue}return null};async function ke(){return{fromHTML:async n=>{const r=new e,a=new t(n.replace(/\\"/g,'"').replace(/\\r/g,"\r").replace(/\\n/g,"\n").replace(/\\t/g,"\t"),{pretendToBeVisual:!0,runScripts:"dangerously",resources:"usable",virtualConsole:r}),{window:o}=a;global.document=o.document,global.Node=o.Node,global.Element=o.Element,global.HTMLElement=o.HTMLElement,global.SVGElement=o.SVGElement;const l=o.CSS??{};return l.escape||(l.escape=e=>String(e).replace(/[^a-zA-Z0-9_-]/g,"\\$&")),global.CSS=l,global.XPathResult=o.XPathResult,global.XPathEvaluator=o.XPathEvaluator,{autoHeal:async e=>be(e,o.document)}}}}export{ke as default};
2
2
  //# sourceMappingURL=xpath.mjs.map