@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,7 +5,7 @@ import { assert } from "../utils.mjs";
5
5
  import { maskConfig, parseJson } from "./helper.mjs";
6
6
  import { initDebugConfig } from "./init-debug.mjs";
7
7
  const MODEL_CONFIG_DOC_URL = 'https://midscenejs.com/model-api-setup.html';
8
- const getCurrentVersion = ()=>"1.17.2";
8
+ const getCurrentVersion = ()=>"1.17.3-beta-20261029072240.0";
9
9
  const getInvalidModelFamilyMessage = (modelFamily)=>`Invalid MIDSCENE_MODEL_FAMILY value: ${modelFamily}. Current version v${getCurrentVersion()} accepts the following model families: ${MODEL_FAMILY_VALUES.join(', ')}. You can also visit ${MODEL_CONFIG_DOC_URL} for the latest configuration information.`;
10
10
  const KEYS_MAP = {
11
11
  locate: LOCATE_MODEL_CONFIG_KEYS,
@@ -256,6 +256,7 @@ const ALL_ENV_KEYS = [
256
256
  ];
257
257
  const MODEL_FAMILY_VALUES = [
258
258
  'jev',
259
+ 'ant-ling',
259
260
  'doubao-vision',
260
261
  'doubao-seed',
261
262
  'gemini',
@@ -1,4 +1,6 @@
1
1
  import { isSvgElement } from "./dom-util.mjs";
2
+ import { deepestElementAtPoint, getContainingShadowRoot, hasRunningFiniteAnimation } from "./shadow-dom.mjs";
3
+ import { buildShadowLocator, resolveShadowLocator, shadowLocatorPrefix } from "./shadow-locator.mjs";
2
4
  import { getNodeFromCacheList, getRect, isElementPartiallyInViewport, logger } from "./util.mjs";
3
5
  import { collectElementInfo } from "./web-extractor.mjs";
4
6
  const SUB_XPATH_SEPARATOR = '|>>|';
@@ -78,6 +80,8 @@ const buildCurrentElementXpath = (element, isOrderSensitive, isLeafElement, limi
78
80
  return `${prefix}${tagSelector}[${index}]`;
79
81
  };
80
82
  const getElementXpath = (element, isOrderSensitive = false, isLeafElement = false, limitToCurrentDocument = false)=>{
83
+ const shadowRoot = getContainingShadowRoot(element);
84
+ if (shadowRoot) return buildShadowLocator(element, shadowRoot, getElementXpath(shadowRoot.host, isOrderSensitive, false, limitToCurrentDocument), isOrderSensitive, isLeafElement);
81
85
  if (element.nodeType === Node.TEXT_NODE) {
82
86
  const parentNode = element.parentNode;
83
87
  if (parentNode && parentNode.nodeType === Node.ELEMENT_NODE) {
@@ -152,7 +156,9 @@ function getXpathsByPoint(point, isOrderSensitive) {
152
156
  let lastFoundElement = null;
153
157
  while(depth < MAX_DEPTH){
154
158
  depth++;
155
- const element = currentDocument.elementFromPoint(left, top);
159
+ const host = currentDocument.elementFromPoint(left, top);
160
+ const element = host ? deepestElementAtPoint(host, left, top) : null;
161
+ if (host && !element) return null;
156
162
  if (!element) {
157
163
  if (lastFoundElement) {
158
164
  const fullXPath = getElementXpath(lastFoundElement, isOrderSensitive, true, true);
@@ -196,19 +202,23 @@ function getXpathsByPoint(point, isOrderSensitive) {
196
202
  }
197
203
  return null;
198
204
  }
199
- function getNodeInfoByXpath(xpath) {
205
+ function getNodeInfoByXpath(xpath, rootDocument = document) {
200
206
  const parts = xpath.split(SUB_XPATH_SEPARATOR).map((p)=>p.trim()).filter(Boolean);
201
207
  if (0 === parts.length) return null;
202
- let currentDocument = 'undefined' != typeof document ? document : void 0;
208
+ let currentDocument = rootDocument;
203
209
  let node = null;
204
210
  for(let i = 0; i < parts.length; i++){
205
211
  const currentXpath = parts[i];
206
- const xpathResult = currentDocument.evaluate(currentXpath, currentDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
207
- if (1 !== xpathResult.snapshotLength) {
208
- logger(`[midscene:locator] XPath "${currentXpath}" matched ${xpathResult.snapshotLength} elements (expected 1), discarding.`);
209
- return null;
212
+ if (currentXpath.startsWith(shadowLocatorPrefix)) node = resolveShadowLocator(currentXpath, (host)=>getNodeInfoByXpath(host, currentDocument));
213
+ else {
214
+ const xpathResult = currentDocument.evaluate(currentXpath, currentDocument, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
215
+ if (1 !== xpathResult.snapshotLength) {
216
+ logger(`[midscene:locator] XPath "${currentXpath}" matched ${xpathResult.snapshotLength} elements (expected 1), discarding.`);
217
+ return null;
218
+ }
219
+ node = xpathResult.snapshotItem(0);
210
220
  }
211
- node = xpathResult.snapshotItem(0);
221
+ if (!node) return null;
212
222
  if (i < parts.length - 1) if (!node || node.nodeType !== Node.ELEMENT_NODE || 'iframe' !== node.tagName.toLowerCase()) return null;
213
223
  else try {
214
224
  const contentDocument = node.contentDocument;
@@ -224,9 +234,10 @@ function getNodeInfoByXpath(xpath) {
224
234
  }
225
235
  return node;
226
236
  }
227
- function getElementInfoByXpath(xpath) {
237
+ function getElementInfoByXpath(xpath, options = {}) {
228
238
  const node = getNodeInfoByXpath(xpath);
229
239
  if (!node) return null;
240
+ if (options.forCache && ('shadowRoot' in node && node.shadowRoot || hasRunningFiniteAnimation(node))) return null;
230
241
  let targetWindow = 'undefined' != typeof window ? window : void 0;
231
242
  let targetDocument = 'undefined' != typeof document ? document : void 0;
232
243
  if (node.ownerDocument?.defaultView) {
@@ -0,0 +1,31 @@
1
+ function getContainingShadowRoot(node) {
2
+ const root = node.getRootNode?.();
3
+ return root && 'host' in root ? root : null;
4
+ }
5
+ function getComposedParent(node) {
6
+ if ('assignedSlot' in node && node.assignedSlot) return node.assignedSlot;
7
+ return node.parentNode || getContainingShadowRoot(node)?.host || null;
8
+ }
9
+ function composedContains(ancestor, node) {
10
+ for(let current = node; current; current = getComposedParent(current))if (current === ancestor) return true;
11
+ return false;
12
+ }
13
+ function deepestElementAtPoint(element, left, top) {
14
+ const visited = new Set();
15
+ while(element.shadowRoot){
16
+ if (visited.has(element)) return null;
17
+ visited.add(element);
18
+ const inner = element.shadowRoot.elementFromPoint(left, top);
19
+ if (!inner || inner === element) return null;
20
+ element = inner;
21
+ }
22
+ return element;
23
+ }
24
+ function hasRunningFiniteAnimation(node) {
25
+ for(let current = node; current; current = getComposedParent(current))if (1 === current.nodeType && 'getAnimations' in current) for (const animation of current.getAnimations()){
26
+ const endTime = animation.effect?.getComputedTiming().endTime;
27
+ if ('running' === animation.playState && 'number' == typeof endTime && Number.isFinite(endTime)) return true;
28
+ }
29
+ return false;
30
+ }
31
+ export { composedContains, deepestElementAtPoint, getComposedParent, getContainingShadowRoot, hasRunningFiniteAnimation };
@@ -0,0 +1,77 @@
1
+ const shadowLocatorPrefix = 'midscene-shadow-v1:';
2
+ const normalizeText = (text)=>text.replace(/\s+/g, ' ').trim();
3
+ function buildShadowLocator(node, root, host, orderSensitive, leaf) {
4
+ const textNode = 3 === node.nodeType;
5
+ const element = textNode ? node.parentElement : node;
6
+ if (!element) throw new Error('Cannot cache a detached shadow text node');
7
+ const steps = [];
8
+ for(let current = element; current; current = current.parentElement){
9
+ const parent = current.parentNode;
10
+ if (!parent) throw new Error('Cannot cache a detached shadow element');
11
+ const tag = current.localName;
12
+ const namespace = current.namespaceURI;
13
+ const siblings = Array.from(parent.children).filter((sibling)=>sibling.localName === tag && sibling.namespaceURI === namespace);
14
+ const text = normalizeText(current.textContent || '');
15
+ steps.unshift({
16
+ tag,
17
+ namespace,
18
+ match: !orderSensitive && (leaf || textNode) && current === element && text ? {
19
+ kind: 'text',
20
+ value: text
21
+ } : {
22
+ kind: 'index',
23
+ value: siblings.indexOf(current)
24
+ }
25
+ });
26
+ if (parent === root) {
27
+ const locator = {
28
+ host,
29
+ steps,
30
+ text: textNode ? normalizeText(node.textContent || '') : null
31
+ };
32
+ return shadowLocatorPrefix + encodeURIComponent(JSON.stringify(locator));
33
+ }
34
+ }
35
+ throw new Error('Shadow target does not belong to the supplied root');
36
+ }
37
+ function isRecord(value) {
38
+ return 'object' == typeof value && null !== value;
39
+ }
40
+ function isShadowStep(value) {
41
+ if (!isRecord(value) || 'string' != typeof value.tag || !value.tag || !(null === value.namespace || 'string' == typeof value.namespace) || !isRecord(value.match)) return false;
42
+ const match = value.match;
43
+ return 'text' === match.kind && 'string' == typeof match.value || 'index' === match.kind && 'number' == typeof match.value && Number.isInteger(match.value) && match.value >= 0;
44
+ }
45
+ function parseShadowLocator(encoded) {
46
+ let value;
47
+ try {
48
+ value = JSON.parse(decodeURIComponent(encoded.slice(shadowLocatorPrefix.length)));
49
+ } catch (cause) {
50
+ throw new Error('Invalid encoded shadow locator', {
51
+ cause
52
+ });
53
+ }
54
+ if (!isRecord(value) || 'string' != typeof value.host || !value.host || !Array.isArray(value.steps) || !value.steps.length || !value.steps.every(isShadowStep) || !(null === value.text || 'string' == typeof value.text)) throw new Error('Invalid shadow locator: expected host, steps and text target');
55
+ return {
56
+ host: value.host,
57
+ steps: value.steps,
58
+ text: value.text
59
+ };
60
+ }
61
+ function resolveShadowLocator(encoded, resolveHost) {
62
+ const locator = parseShadowLocator(encoded);
63
+ const host = resolveHost(locator.host);
64
+ if (!host || !('shadowRoot' in host) || !host.shadowRoot) return null;
65
+ let parent = host.shadowRoot;
66
+ for (const step of locator.steps){
67
+ let candidates = Array.from(parent.children).filter((child)=>child.localName === step.tag && child.namespaceURI === step.namespace);
68
+ const match = step.match;
69
+ candidates = 'text' === match.kind ? candidates.filter((child)=>normalizeText(child.textContent || '') === match.value) : candidates.slice(match.value, match.value + 1);
70
+ if (1 !== candidates.length) return null;
71
+ parent = candidates[0];
72
+ }
73
+ if (null === locator.text) return parent;
74
+ const texts = Array.from(parent.childNodes).filter((child)=>3 === child.nodeType && normalizeText(child.textContent || '') === locator.text);
75
+ return 1 === texts.length ? texts[0] : null;
76
+ }
77
+ export { buildShadowLocator, resolveShadowLocator, shadowLocatorPrefix };
@@ -1,4 +1,5 @@
1
1
  import { generateHashId } from "../utils.mjs";
2
+ import { composedContains, deepestElementAtPoint } from "./shadow-dom.mjs";
2
3
  import { extractTextWithPosition } from "./web-extractor.mjs";
3
4
  const MAX_VALUE_LENGTH = 300;
4
5
  let debugMode = false;
@@ -94,11 +95,12 @@ function getRect(el, baseZoom, currentWindow) {
94
95
  const isElementCovered = (el, rect, currentWindow)=>{
95
96
  const x = rect.left + rect.width / 2;
96
97
  const y = rect.top + rect.height / 2;
97
- const topElement = currentWindow.document.elementFromPoint(x, y);
98
+ const host = currentWindow.document.elementFromPoint(x, y);
99
+ const topElement = host ? deepestElementAtPoint(host, x, y) || host : null;
98
100
  if (!topElement) return false;
99
101
  if (topElement === el) return false;
100
- if (el?.contains(topElement)) return false;
101
- if (topElement?.contains(el)) return false;
102
+ if (composedContains(el, topElement)) return false;
103
+ if (composedContains(topElement, el)) return false;
102
104
  const rectOfTopElement = getRect(topElement, 1, currentWindow);
103
105
  const overlapRect = overlappedRect(rect, rectOfTopElement);
104
106
  if (!overlapRect) return false;