From cb8e89ca6e40901f44671420da601e5221209d41 Mon Sep 17 00:00:00 2001 From: Rohit Date: Mon, 7 Jul 2025 12:28:25 +0530 Subject: [PATCH] feat: add xpath validation checks --- src/helpers/clientSelectorGenerator.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/helpers/clientSelectorGenerator.ts b/src/helpers/clientSelectorGenerator.ts index 028f8efe..62efe3b2 100644 --- a/src/helpers/clientSelectorGenerator.ts +++ b/src/helpers/clientSelectorGenerator.ts @@ -2820,6 +2820,11 @@ class ClientSelectorGenerator { contextNode: Document | ShadowRoot ): HTMLElement[] { try { + if (!this.isXPathSelector(xpath)) { + console.warn("Selector doesn't appear to be XPath:", xpath); + return []; + } + const document = contextNode instanceof ShadowRoot ? (contextNode.host as HTMLElement).ownerDocument @@ -2847,11 +2852,28 @@ class ClientSelectorGenerator { } } + private isXPathSelector(selector: string): boolean { + return selector.startsWith('//') || + selector.startsWith('/') || + selector.startsWith('./') || + selector.includes('contains(@') || + selector.includes('[count(') || + selector.includes('@class=') || + selector.includes('@id=') || + selector.includes(' and ') || + selector.includes(' or '); + } + private fallbackXPathEvaluation( xpath: string, contextNode: Document | ShadowRoot ): HTMLElement[] { try { + if (this.isXPathSelector(xpath)) { + console.warn("⚠️ Complex XPath not supported in fallback:", xpath); + return []; + } + const simpleTagMatch = xpath.match(/^\/\/(\w+)$/); if (simpleTagMatch) { const tagName = simpleTagMatch[1];