Skip to content

Commit b15fe26

Browse files
authored
Merge pull request #677 from getmaxun/xpath-fix
fix: xpath validation check
2 parents 3cae31c + cb8e89c commit b15fe26

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/helpers/clientSelectorGenerator.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,11 @@ class ClientSelectorGenerator {
28202820
contextNode: Document | ShadowRoot
28212821
): HTMLElement[] {
28222822
try {
2823+
if (!this.isXPathSelector(xpath)) {
2824+
console.warn("Selector doesn't appear to be XPath:", xpath);
2825+
return [];
2826+
}
2827+
28232828
const document =
28242829
contextNode instanceof ShadowRoot
28252830
? (contextNode.host as HTMLElement).ownerDocument
@@ -2847,11 +2852,28 @@ class ClientSelectorGenerator {
28472852
}
28482853
}
28492854

2855+
private isXPathSelector(selector: string): boolean {
2856+
return selector.startsWith('//') ||
2857+
selector.startsWith('/') ||
2858+
selector.startsWith('./') ||
2859+
selector.includes('contains(@') ||
2860+
selector.includes('[count(') ||
2861+
selector.includes('@class=') ||
2862+
selector.includes('@id=') ||
2863+
selector.includes(' and ') ||
2864+
selector.includes(' or ');
2865+
}
2866+
28502867
private fallbackXPathEvaluation(
28512868
xpath: string,
28522869
contextNode: Document | ShadowRoot
28532870
): HTMLElement[] {
28542871
try {
2872+
if (this.isXPathSelector(xpath)) {
2873+
console.warn("⚠️ Complex XPath not supported in fallback:", xpath);
2874+
return [];
2875+
}
2876+
28552877
const simpleTagMatch = xpath.match(/^\/\/(\w+)$/);
28562878
if (simpleTagMatch) {
28572879
const tagName = simpleTagMatch[1];

0 commit comments

Comments
 (0)