@midscene/shared 1.17.2 → 1.17.3-beta-20261029072240.0

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.
@@ -5,6 +5,10 @@ export declare const getElementXpath: (element: Node, isOrderSensitive?: boolean
5
5
  * Returns a local xpath within the node's own document (limitToCurrentDocument=true). */
6
6
  export declare function getXpathsById(id: string): string[] | null;
7
7
  export declare function getXpathsByPoint(point: Point, isOrderSensitive: boolean): string[] | null;
8
- export declare function getNodeInfoByXpath(xpath: string): Node | null;
9
- export declare function getElementInfoByXpath(xpath: string): ElementInfo | null;
8
+ /** Resolve a normal XPath or an internal compound locator in its document. */
9
+ export declare function getNodeInfoByXpath(xpath: string, rootDocument?: Document): Node | null;
10
+ /** Look up geometry; cache replay additionally rejects hosts and finite animations. */
11
+ export declare function getElementInfoByXpath(xpath: string, options?: {
12
+ forCache?: boolean;
13
+ }): ElementInfo | null;
10
14
  //# sourceMappingURL=locator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"locator.d.ts","sourceRoot":"","sources":["../../../src/extractor/locator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AACrC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AA2JtC,eAAO,MAAM,eAAe,GAC1B,SAAS,IAAI,EACb,0BAAwB,EACxB,uBAAqB,EACrB,gCAA8B,KAC7B,MAiHF,CAAC;AAEF;0FAC0F;AAC1F,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAKzD;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,EACZ,gBAAgB,EAAE,OAAO,GACxB,MAAM,EAAE,GAAG,IAAI,CA6EjB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CA4D7D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAuCvE"}
1
+ {"version":3,"file":"locator.d.ts","sourceRoot":"","sources":["../../../src/extractor/locator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AACrC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAqKtC,eAAO,MAAM,eAAe,GAC1B,SAAS,IAAI,EACb,0BAAwB,EACxB,uBAAqB,EACrB,gCAA8B,KAC7B,MAgIF,CAAC;AAEF;0FAC0F;AAC1F,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAKzD;AAED,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,EACZ,gBAAgB,EAAE,OAAO,GACxB,MAAM,EAAE,GAAG,IAAI,CA+EjB;AAED,8EAA8E;AAC9E,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,YAAY,GAAE,QAAmB,GAChC,IAAI,GAAG,IAAI,CAkEb;AAED,uFAAuF;AACvF,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAO,GACnC,WAAW,GAAG,IAAI,CA+CpB"}
@@ -0,0 +1,10 @@
1
+ /** Return the open shadow root containing a node, including text nodes. */
2
+ export declare function getContainingShadowRoot(node: Node): ShadowRoot | null;
3
+ /** Walk the rendered ancestry across slots and shadow boundaries. */
4
+ export declare function getComposedParent(node: Node): Node | null;
5
+ export declare function composedContains(ancestor: Node, node: Node): boolean;
6
+ /** Resolve the deepest accessible hit, or reject an unresolved shadow host. */
7
+ export declare function deepestElementAtPoint(element: Element, left: number, top: number): Element | null;
8
+ /** A finite running animation can invalidate a cached rectangle before input. */
9
+ export declare function hasRunningFiniteAnimation(node: Node): boolean;
10
+ //# sourceMappingURL=shadow-dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shadow-dom.d.ts","sourceRoot":"","sources":["../../../src/extractor/shadow-dom.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,IAAI,GAAG,UAAU,GAAG,IAAI,CAGrE;AAED,qEAAqE;AACrE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAKzD;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CASpE;AAED,+EAA+E;AAC/E,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,GACV,OAAO,GAAG,IAAI,CAUhB;AAED,iFAAiF;AACjF,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAkB7D"}
@@ -0,0 +1,6 @@
1
+ export declare const shadowLocatorPrefix = "midscene-shadow-v1:";
2
+ /** Serialize a root-local element or text target and the locator of its host. */
3
+ export declare function buildShadowLocator(node: Node, root: ShadowRoot, host: string, orderSensitive: boolean, leaf: boolean): string;
4
+ /** Resolve each step uniquely; missing/ambiguous targets are normal cache misses. */
5
+ export declare function resolveShadowLocator(encoded: string, resolveHost: (path: string) => Node | null): Node | null;
6
+ //# sourceMappingURL=shadow-locator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shadow-locator.d.ts","sourceRoot":"","sources":["../../../src/extractor/shadow-locator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB,wBAAwB,CAAC;AAgBzD,iFAAiF;AACjF,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,OAAO,EACvB,IAAI,EAAE,OAAO,GACZ,MAAM,CAqCR;AAkDD,qFAAqF;AACrF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,SAAS,CAE5E;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,UAAU,CAAC,IAAI,GAAG,IAAI,EAC5B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,IAAI,GACT,MAAM,CAWR;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,UAO1C;AAED,wBAAgB,uBAAuB,SAItC;AAED,wBAAgB,8BAA8B,SAI7C;AAED,wBAAgB,kCAAkC,SAIjD;AAED,wBAAgB,cAAc,IAAI,UAAU,CAAC,WAAW,CAGvD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midscene/shared",
3
- "version": "1.17.2",
3
+ "version": "1.17.3-beta-20261029072240.0",
4
4
  "repository": "https://github.com/web-infra-dev/midscene",
5
5
  "homepage": "https://midscenejs.com/",
6
6
  "types": "./dist/types/index.d.ts",
package/src/env/types.ts CHANGED
@@ -392,6 +392,7 @@ export type TGlobalConfig = Record<TEnvKeys, string | undefined>;
392
392
  */
393
393
  export type TModelFamily =
394
394
  | 'jev'
395
+ | 'ant-ling'
395
396
  | 'qwen2.5-vl'
396
397
  | 'qwen3-vl'
397
398
  | 'qwen3'
@@ -415,6 +416,7 @@ export type TModelFamily =
415
416
 
416
417
  export const MODEL_FAMILY_VALUES: TModelFamily[] = [
417
418
  'jev',
419
+ 'ant-ling',
418
420
  'doubao-vision',
419
421
  'doubao-seed',
420
422
  'gemini',
@@ -1,6 +1,16 @@
1
1
  import type { ElementInfo } from '.';
2
2
  import type { Point } from '../types';
3
3
  import { isSvgElement } from './dom-util';
4
+ import {
5
+ deepestElementAtPoint,
6
+ getContainingShadowRoot,
7
+ hasRunningFiniteAnimation,
8
+ } from './shadow-dom';
9
+ import {
10
+ buildShadowLocator,
11
+ resolveShadowLocator,
12
+ shadowLocatorPrefix,
13
+ } from './shadow-locator';
4
14
  import {
5
15
  getNodeFromCacheList,
6
16
  getRect,
@@ -160,6 +170,22 @@ export const getElementXpath = (
160
170
  isLeafElement = false,
161
171
  limitToCurrentDocument = false,
162
172
  ): string => {
173
+ const shadowRoot = getContainingShadowRoot(element);
174
+ if (shadowRoot) {
175
+ return buildShadowLocator(
176
+ element,
177
+ shadowRoot,
178
+ getElementXpath(
179
+ shadowRoot.host,
180
+ isOrderSensitive,
181
+ false,
182
+ limitToCurrentDocument,
183
+ ),
184
+ isOrderSensitive,
185
+ isLeafElement,
186
+ );
187
+ }
188
+
163
189
  // process text node
164
190
  if (element.nodeType === Node.TEXT_NODE) {
165
191
  const parentNode = element.parentNode;
@@ -182,7 +208,6 @@ export const getElementXpath = (
182
208
  if (element.nodeType !== Node.ELEMENT_NODE) return '';
183
209
 
184
210
  const el = element as Element;
185
-
186
211
  // special element handling (iframe-aware: prefix with frame path when not limitToCurrentDocument)
187
212
  try {
188
213
  const nodeName = el.nodeName.toLowerCase();
@@ -299,7 +324,9 @@ export function getXpathsByPoint(
299
324
 
300
325
  while (depth < MAX_DEPTH) {
301
326
  depth++;
302
- const element = currentDocument.elementFromPoint(left, top);
327
+ const host = currentDocument.elementFromPoint(left, top);
328
+ const element = host ? deepestElementAtPoint(host, left, top) : null;
329
+ if (host && !element) return null;
303
330
 
304
331
  if (!element) {
305
332
  if (lastFoundElement) {
@@ -365,35 +392,45 @@ export function getXpathsByPoint(
365
392
  return null;
366
393
  }
367
394
 
368
- export function getNodeInfoByXpath(xpath: string): Node | null {
395
+ /** Resolve a normal XPath or an internal compound locator in its document. */
396
+ export function getNodeInfoByXpath(
397
+ xpath: string,
398
+ rootDocument: Document = document,
399
+ ): Node | null {
369
400
  const parts = xpath
370
401
  .split(SUB_XPATH_SEPARATOR)
371
402
  .map((p) => p.trim())
372
403
  .filter(Boolean);
373
404
  if (parts.length === 0) return null;
374
405
 
375
- let currentDocument: Document =
376
- typeof document !== 'undefined' ? document : (undefined as any);
406
+ let currentDocument = rootDocument;
377
407
  let node: Node | null = null;
378
408
 
379
409
  for (let i = 0; i < parts.length; i++) {
380
410
  const currentXpath = parts[i];
381
- const xpathResult = currentDocument.evaluate(
382
- currentXpath,
383
- currentDocument,
384
- null,
385
- XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
386
- null,
387
- );
388
-
389
- if (xpathResult.snapshotLength !== 1) {
390
- logger(
391
- `[midscene:locator] XPath "${currentXpath}" matched ${xpathResult.snapshotLength} elements (expected 1), discarding.`,
411
+ if (currentXpath.startsWith(shadowLocatorPrefix)) {
412
+ node = resolveShadowLocator(currentXpath, (host) =>
413
+ getNodeInfoByXpath(host, currentDocument),
414
+ );
415
+ } else {
416
+ const xpathResult = currentDocument.evaluate(
417
+ currentXpath,
418
+ currentDocument,
419
+ null,
420
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
421
+ null,
392
422
  );
393
- return null;
394
- }
395
423
 
396
- node = xpathResult.snapshotItem(0);
424
+ if (xpathResult.snapshotLength !== 1) {
425
+ logger(
426
+ `[midscene:locator] XPath "${currentXpath}" matched ${xpathResult.snapshotLength} elements (expected 1), discarding.`,
427
+ );
428
+ return null;
429
+ }
430
+
431
+ node = xpathResult.snapshotItem(0);
432
+ }
433
+ if (!node) return null;
397
434
 
398
435
  if (i < parts.length - 1) {
399
436
  if (
@@ -427,9 +464,21 @@ export function getNodeInfoByXpath(xpath: string): Node | null {
427
464
  return node;
428
465
  }
429
466
 
430
- export function getElementInfoByXpath(xpath: string): ElementInfo | null {
467
+ /** Look up geometry; cache replay additionally rejects hosts and finite animations. */
468
+ export function getElementInfoByXpath(
469
+ xpath: string,
470
+ options: { forCache?: boolean } = {},
471
+ ): ElementInfo | null {
431
472
  const node = getNodeInfoByXpath(xpath);
432
473
  if (!node) return null;
474
+ // Old caches may point at a shadow host instead of the actual internal target.
475
+ // Its center is not a safe substitute (e.g. a date field versus a calendar day).
476
+ if (
477
+ options.forCache &&
478
+ (('shadowRoot' in node && node.shadowRoot) ||
479
+ hasRunningFiniteAnimation(node))
480
+ )
481
+ return null;
433
482
 
434
483
  let targetWindow: Window =
435
484
  typeof window !== 'undefined' ? window : (undefined as any);
@@ -0,0 +1,62 @@
1
+ /** Return the open shadow root containing a node, including text nodes. */
2
+ export function getContainingShadowRoot(node: Node): ShadowRoot | null {
3
+ const root = node.getRootNode?.();
4
+ return root && 'host' in root ? (root as ShadowRoot) : null;
5
+ }
6
+
7
+ /** Walk the rendered ancestry across slots and shadow boundaries. */
8
+ export function getComposedParent(node: Node): Node | null {
9
+ if ('assignedSlot' in node && node.assignedSlot) {
10
+ return node.assignedSlot as HTMLSlotElement;
11
+ }
12
+ return node.parentNode || getContainingShadowRoot(node)?.host || null;
13
+ }
14
+
15
+ export function composedContains(ancestor: Node, node: Node): boolean {
16
+ for (
17
+ let current: Node | null = node;
18
+ current;
19
+ current = getComposedParent(current)
20
+ ) {
21
+ if (current === ancestor) return true;
22
+ }
23
+ return false;
24
+ }
25
+
26
+ /** Resolve the deepest accessible hit, or reject an unresolved shadow host. */
27
+ export function deepestElementAtPoint(
28
+ element: Element,
29
+ left: number,
30
+ top: number,
31
+ ): Element | null {
32
+ const visited = new Set<Element>();
33
+ while (element.shadowRoot) {
34
+ if (visited.has(element)) return null;
35
+ visited.add(element);
36
+ const inner = element.shadowRoot.elementFromPoint(left, top);
37
+ if (!inner || inner === element) return null;
38
+ element = inner;
39
+ }
40
+ return element;
41
+ }
42
+
43
+ /** A finite running animation can invalidate a cached rectangle before input. */
44
+ export function hasRunningFiniteAnimation(node: Node): boolean {
45
+ for (
46
+ let current: Node | null = node;
47
+ current;
48
+ current = getComposedParent(current)
49
+ ) {
50
+ if (current.nodeType !== 1 || !('getAnimations' in current)) continue;
51
+ for (const animation of (current as Element).getAnimations()) {
52
+ const endTime = animation.effect?.getComputedTiming().endTime;
53
+ if (
54
+ animation.playState === 'running' &&
55
+ typeof endTime === 'number' &&
56
+ Number.isFinite(endTime)
57
+ )
58
+ return true;
59
+ }
60
+ }
61
+ return false;
62
+ }
@@ -0,0 +1,144 @@
1
+ // Encode the complete shadow segment so XPath text and iframe separators cannot
2
+ // collide with its grammar. Ordinary XPath strings keep their existing meaning.
3
+ export const shadowLocatorPrefix = 'midscene-shadow-v1:';
4
+
5
+ type Match = { kind: 'text'; value: string } | { kind: 'index'; value: number };
6
+ interface ShadowStep {
7
+ tag: string;
8
+ namespace: string | null;
9
+ match: Match;
10
+ }
11
+ interface ShadowLocator {
12
+ host: string;
13
+ steps: ShadowStep[];
14
+ text: string | null;
15
+ }
16
+
17
+ const normalizeText = (text: string) => text.replace(/\s+/g, ' ').trim();
18
+
19
+ /** Serialize a root-local element or text target and the locator of its host. */
20
+ export function buildShadowLocator(
21
+ node: Node,
22
+ root: ShadowRoot,
23
+ host: string,
24
+ orderSensitive: boolean,
25
+ leaf: boolean,
26
+ ): string {
27
+ const textNode = node.nodeType === 3;
28
+ const element = textNode ? node.parentElement : (node as Element);
29
+ if (!element) throw new Error('Cannot cache a detached shadow text node');
30
+ const steps: ShadowStep[] = [];
31
+ for (
32
+ let current: Element | null = element;
33
+ current;
34
+ current = current.parentElement
35
+ ) {
36
+ const parent = current.parentNode;
37
+ if (!parent) throw new Error('Cannot cache a detached shadow element');
38
+ const tag = current.localName;
39
+ const namespace = current.namespaceURI;
40
+ const siblings = Array.from(parent.children).filter(
41
+ (sibling) =>
42
+ sibling.localName === tag && sibling.namespaceURI === namespace,
43
+ );
44
+ const text = normalizeText(current.textContent || '');
45
+ steps.unshift({
46
+ tag,
47
+ namespace,
48
+ match:
49
+ !orderSensitive && (leaf || textNode) && current === element && text
50
+ ? { kind: 'text', value: text }
51
+ : { kind: 'index', value: siblings.indexOf(current) },
52
+ });
53
+ if (parent === root) {
54
+ const locator: ShadowLocator = {
55
+ host,
56
+ steps,
57
+ text: textNode ? normalizeText(node.textContent || '') : null,
58
+ };
59
+ return shadowLocatorPrefix + encodeURIComponent(JSON.stringify(locator));
60
+ }
61
+ }
62
+ throw new Error('Shadow target does not belong to the supplied root');
63
+ }
64
+
65
+ function isRecord(value: unknown): value is Record<string, unknown> {
66
+ return typeof value === 'object' && value !== null;
67
+ }
68
+
69
+ function isShadowStep(value: unknown): value is ShadowStep {
70
+ if (
71
+ !isRecord(value) ||
72
+ typeof value.tag !== 'string' ||
73
+ !value.tag ||
74
+ !(value.namespace === null || typeof value.namespace === 'string') ||
75
+ !isRecord(value.match)
76
+ )
77
+ return false;
78
+ const match = value.match;
79
+ return (
80
+ (match.kind === 'text' && typeof match.value === 'string') ||
81
+ (match.kind === 'index' &&
82
+ typeof match.value === 'number' &&
83
+ Number.isInteger(match.value) &&
84
+ match.value >= 0)
85
+ );
86
+ }
87
+
88
+ function parseShadowLocator(encoded: string): ShadowLocator {
89
+ let value: unknown;
90
+ try {
91
+ value = JSON.parse(
92
+ decodeURIComponent(encoded.slice(shadowLocatorPrefix.length)),
93
+ );
94
+ } catch (cause) {
95
+ throw new Error('Invalid encoded shadow locator', { cause });
96
+ }
97
+ if (
98
+ !isRecord(value) ||
99
+ typeof value.host !== 'string' ||
100
+ !value.host ||
101
+ !Array.isArray(value.steps) ||
102
+ !value.steps.length ||
103
+ !value.steps.every(isShadowStep) ||
104
+ !(value.text === null || typeof value.text === 'string')
105
+ ) {
106
+ throw new Error(
107
+ 'Invalid shadow locator: expected host, steps and text target',
108
+ );
109
+ }
110
+ return { host: value.host, steps: value.steps, text: value.text };
111
+ }
112
+
113
+ /** Resolve each step uniquely; missing/ambiguous targets are normal cache misses. */
114
+ export function resolveShadowLocator(
115
+ encoded: string,
116
+ resolveHost: (path: string) => Node | null,
117
+ ): Node | null {
118
+ const locator = parseShadowLocator(encoded);
119
+ const host = resolveHost(locator.host);
120
+ if (!host || !('shadowRoot' in host) || !host.shadowRoot) return null;
121
+ let parent: ShadowRoot | Element = host.shadowRoot as ShadowRoot;
122
+ for (const step of locator.steps) {
123
+ let candidates: Element[] = Array.from(parent.children).filter(
124
+ (child) =>
125
+ child.localName === step.tag && child.namespaceURI === step.namespace,
126
+ );
127
+ const match = step.match;
128
+ candidates =
129
+ match.kind === 'text'
130
+ ? candidates.filter(
131
+ (child) => normalizeText(child.textContent || '') === match.value,
132
+ )
133
+ : candidates.slice(match.value, match.value + 1);
134
+ if (candidates.length !== 1) return null;
135
+ parent = candidates[0];
136
+ }
137
+ if (locator.text === null) return parent;
138
+ const texts = Array.from(parent.childNodes).filter(
139
+ (child) =>
140
+ child.nodeType === 3 &&
141
+ normalizeText(child.textContent || '') === locator.text,
142
+ );
143
+ return texts.length === 1 ? texts[0] : null;
144
+ }
@@ -1,5 +1,6 @@
1
1
  import type { Rect } from '../types';
2
2
  import { generateHashId } from '../utils';
3
+ import { composedContains, deepestElementAtPoint } from './shadow-dom';
3
4
  import { extractTextWithPosition } from './web-extractor';
4
5
 
5
6
  const MAX_VALUE_LENGTH = 300;
@@ -180,7 +181,8 @@ const isElementCovered = (
180
181
  const y = rect.top + rect.height / 2;
181
182
 
182
183
  // Gets the element above that point
183
- const topElement = currentWindow.document.elementFromPoint(x, y);
184
+ const host = currentWindow.document.elementFromPoint(x, y);
185
+ const topElement = host ? deepestElementAtPoint(host, x, y) || host : null;
184
186
  if (!topElement) {
185
187
  return false; // usually because it's outside the screen
186
188
  }
@@ -188,11 +190,11 @@ const isElementCovered = (
188
190
  if (topElement === el) {
189
191
  return false;
190
192
  }
191
- if (el?.contains(topElement)) {
193
+ if (composedContains(el, topElement)) {
192
194
  return false;
193
195
  }
194
196
 
195
- if ((topElement as HTMLElement)?.contains(el)) {
197
+ if (composedContains(topElement, el)) {
196
198
  return false;
197
199
  }
198
200