@sqaitech/shared 0.30.10

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.
Files changed (169) hide show
  1. package/README.md +9 -0
  2. package/dist/es/baseDB.mjs +109 -0
  3. package/dist/es/build/copy-static.mjs +29 -0
  4. package/dist/es/common.mjs +37 -0
  5. package/dist/es/constants/example-code.mjs +202 -0
  6. package/dist/es/constants/index.mjs +24 -0
  7. package/dist/es/env/basic.mjs +6 -0
  8. package/dist/es/env/constants.mjs +97 -0
  9. package/dist/es/env/decide-model-config.mjs +172 -0
  10. package/dist/es/env/global-config-manager.mjs +91 -0
  11. package/dist/es/env/helper.mjs +48 -0
  12. package/dist/es/env/index.mjs +5 -0
  13. package/dist/es/env/init-debug.mjs +18 -0
  14. package/dist/es/env/model-config-manager.mjs +99 -0
  15. package/dist/es/env/parse.mjs +69 -0
  16. package/dist/es/env/types.mjs +253 -0
  17. package/dist/es/env/utils.mjs +18 -0
  18. package/dist/es/extractor/constants.mjs +2 -0
  19. package/dist/es/extractor/debug.mjs +6 -0
  20. package/dist/es/extractor/dom-util.mjs +92 -0
  21. package/dist/es/extractor/index.mjs +6 -0
  22. package/dist/es/extractor/locator.mjs +95 -0
  23. package/dist/es/extractor/tree.mjs +81 -0
  24. package/dist/es/extractor/util.mjs +244 -0
  25. package/dist/es/extractor/web-extractor.mjs +310 -0
  26. package/dist/es/img/box-select.mjs +184 -0
  27. package/dist/es/img/draw-box.mjs +42 -0
  28. package/dist/es/img/get-jimp.mjs +10 -0
  29. package/dist/es/img/get-photon.mjs +19 -0
  30. package/dist/es/img/get-sharp.mjs +11 -0
  31. package/dist/es/img/index.mjs +5 -0
  32. package/dist/es/img/info.mjs +32 -0
  33. package/dist/es/img/transform.mjs +192 -0
  34. package/dist/es/index.mjs +3 -0
  35. package/dist/es/logger.mjs +61 -0
  36. package/dist/es/node/fs.mjs +44 -0
  37. package/dist/es/node/index.mjs +1 -0
  38. package/dist/es/polyfills/async-hooks.mjs +2 -0
  39. package/dist/es/polyfills/index.mjs +1 -0
  40. package/dist/es/types/index.mjs +3 -0
  41. package/dist/es/us-keyboard-layout.mjs +1414 -0
  42. package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
  43. package/dist/es/utils.mjs +66 -0
  44. package/dist/lib/baseDB.js +149 -0
  45. package/dist/lib/build/copy-static.js +77 -0
  46. package/dist/lib/common.js +93 -0
  47. package/dist/lib/constants/example-code.js +239 -0
  48. package/dist/lib/constants/index.js +100 -0
  49. package/dist/lib/env/basic.js +40 -0
  50. package/dist/lib/env/constants.js +143 -0
  51. package/dist/lib/env/decide-model-config.js +212 -0
  52. package/dist/lib/env/global-config-manager.js +125 -0
  53. package/dist/lib/env/helper.js +89 -0
  54. package/dist/lib/env/index.js +94 -0
  55. package/dist/lib/env/init-debug.js +52 -0
  56. package/dist/lib/env/model-config-manager.js +133 -0
  57. package/dist/lib/env/parse.js +106 -0
  58. package/dist/lib/env/types.js +635 -0
  59. package/dist/lib/env/utils.js +61 -0
  60. package/dist/lib/extractor/constants.js +42 -0
  61. package/dist/lib/extractor/debug.js +12 -0
  62. package/dist/lib/extractor/dom-util.js +150 -0
  63. package/dist/lib/extractor/index.js +88 -0
  64. package/dist/lib/extractor/locator.js +141 -0
  65. package/dist/lib/extractor/tree.js +127 -0
  66. package/dist/lib/extractor/util.js +335 -0
  67. package/dist/lib/extractor/web-extractor.js +356 -0
  68. package/dist/lib/img/box-select.js +232 -0
  69. package/dist/lib/img/draw-box.js +89 -0
  70. package/dist/lib/img/get-jimp.js +72 -0
  71. package/dist/lib/img/get-photon.js +76 -0
  72. package/dist/lib/img/get-sharp.js +63 -0
  73. package/dist/lib/img/index.js +102 -0
  74. package/dist/lib/img/info.js +86 -0
  75. package/dist/lib/img/transform.js +279 -0
  76. package/dist/lib/index.js +43 -0
  77. package/dist/lib/logger.js +114 -0
  78. package/dist/lib/node/fs.js +97 -0
  79. package/dist/lib/node/index.js +60 -0
  80. package/dist/lib/polyfills/async-hooks.js +36 -0
  81. package/dist/lib/polyfills/index.js +60 -0
  82. package/dist/lib/types/index.js +37 -0
  83. package/dist/lib/us-keyboard-layout.js +1457 -0
  84. package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
  85. package/dist/lib/utils.js +136 -0
  86. package/dist/types/baseDB.d.ts +25 -0
  87. package/dist/types/build/copy-static.d.ts +31 -0
  88. package/dist/types/common.d.ts +12 -0
  89. package/dist/types/constants/example-code.d.ts +2 -0
  90. package/dist/types/constants/index.d.ts +22 -0
  91. package/dist/types/env/basic.d.ts +6 -0
  92. package/dist/types/env/constants.d.ts +40 -0
  93. package/dist/types/env/decide-model-config.d.ts +14 -0
  94. package/dist/types/env/global-config-manager.d.ts +32 -0
  95. package/dist/types/env/helper.d.ts +6 -0
  96. package/dist/types/env/index.d.ts +4 -0
  97. package/dist/types/env/init-debug.d.ts +1 -0
  98. package/dist/types/env/model-config-manager.d.ts +24 -0
  99. package/dist/types/env/parse.d.ts +12 -0
  100. package/dist/types/env/types.d.ts +294 -0
  101. package/dist/types/env/utils.d.ts +7 -0
  102. package/dist/types/extractor/constants.d.ts +1 -0
  103. package/dist/types/extractor/debug.d.ts +1 -0
  104. package/dist/types/extractor/dom-util.d.ts +26 -0
  105. package/dist/types/extractor/index.d.ts +33 -0
  106. package/dist/types/extractor/locator.d.ts +7 -0
  107. package/dist/types/extractor/tree.d.ts +9 -0
  108. package/dist/types/extractor/util.d.ts +43 -0
  109. package/dist/types/extractor/web-extractor.d.ts +19 -0
  110. package/dist/types/img/box-select.d.ts +25 -0
  111. package/dist/types/img/draw-box.d.ts +15 -0
  112. package/dist/types/img/get-jimp.d.ts +2 -0
  113. package/dist/types/img/get-photon.d.ts +8 -0
  114. package/dist/types/img/get-sharp.d.ts +3 -0
  115. package/dist/types/img/index.d.ts +4 -0
  116. package/dist/types/img/info.d.ts +29 -0
  117. package/dist/types/img/transform.d.ts +88 -0
  118. package/dist/types/index.d.ts +3 -0
  119. package/dist/types/logger.d.ts +4 -0
  120. package/dist/types/node/fs.d.ts +15 -0
  121. package/dist/types/node/index.d.ts +1 -0
  122. package/dist/types/polyfills/async-hooks.d.ts +6 -0
  123. package/dist/types/polyfills/index.d.ts +4 -0
  124. package/dist/types/types/index.d.ts +34 -0
  125. package/dist/types/us-keyboard-layout.d.ts +32 -0
  126. package/dist/types/utils.d.ts +22 -0
  127. package/package.json +106 -0
  128. package/src/baseDB.ts +158 -0
  129. package/src/build/copy-static.ts +62 -0
  130. package/src/common.ts +67 -0
  131. package/src/constants/example-code.ts +202 -0
  132. package/src/constants/index.ts +30 -0
  133. package/src/env/basic.ts +12 -0
  134. package/src/env/constants.ts +291 -0
  135. package/src/env/decide-model-config.ts +319 -0
  136. package/src/env/global-config-manager.ts +174 -0
  137. package/src/env/helper.ts +79 -0
  138. package/src/env/index.ts +4 -0
  139. package/src/env/init-debug.ts +29 -0
  140. package/src/env/model-config-manager.ts +145 -0
  141. package/src/env/parse.ts +131 -0
  142. package/src/env/types.ts +560 -0
  143. package/src/env/utils.ts +39 -0
  144. package/src/extractor/constants.ts +5 -0
  145. package/src/extractor/debug.ts +10 -0
  146. package/src/extractor/dom-util.ts +152 -0
  147. package/src/extractor/index.ts +50 -0
  148. package/src/extractor/locator.ts +179 -0
  149. package/src/extractor/tree.ts +179 -0
  150. package/src/extractor/util.ts +468 -0
  151. package/src/extractor/web-extractor.ts +481 -0
  152. package/src/img/box-select.ts +346 -0
  153. package/src/img/draw-box.ts +60 -0
  154. package/src/img/get-jimp.ts +12 -0
  155. package/src/img/get-photon.ts +48 -0
  156. package/src/img/get-sharp.ts +18 -0
  157. package/src/img/index.ts +24 -0
  158. package/src/img/info.ts +79 -0
  159. package/src/img/jimp.d.ts +4 -0
  160. package/src/img/transform.ts +396 -0
  161. package/src/index.ts +6 -0
  162. package/src/logger.ts +93 -0
  163. package/src/node/fs.ts +84 -0
  164. package/src/node/index.ts +1 -0
  165. package/src/polyfills/async-hooks.ts +6 -0
  166. package/src/polyfills/index.ts +4 -0
  167. package/src/types/index.ts +47 -0
  168. package/src/us-keyboard-layout.ts +723 -0
  169. package/src/utils.ts +127 -0
@@ -0,0 +1,244 @@
1
+ import { generateHashId } from "../utils.mjs";
2
+ import { extractTextWithPosition } from "./web-extractor.mjs";
3
+ const MAX_VALUE_LENGTH = 300;
4
+ let debugMode = false;
5
+ function setDebugMode(mode) {
6
+ debugMode = mode;
7
+ }
8
+ function getDebugMode() {
9
+ return debugMode;
10
+ }
11
+ function logger(..._msg) {
12
+ if (!debugMode) return;
13
+ console.log(..._msg);
14
+ }
15
+ function isElementPartiallyInViewport(rect, currentWindow, currentDocument, visibleAreaRatio = 2 / 3) {
16
+ const elementHeight = rect.height;
17
+ const elementWidth = rect.width;
18
+ const viewportRect = {
19
+ left: 0,
20
+ top: 0,
21
+ width: currentWindow.innerWidth || currentDocument.documentElement.clientWidth,
22
+ height: currentWindow.innerHeight || currentDocument.documentElement.clientHeight,
23
+ right: currentWindow.innerWidth || currentDocument.documentElement.clientWidth,
24
+ bottom: currentWindow.innerHeight || currentDocument.documentElement.clientHeight,
25
+ x: 0,
26
+ y: 0,
27
+ zoom: 1
28
+ };
29
+ const overlapRect = overlappedRect(rect, viewportRect);
30
+ if (!overlapRect) return false;
31
+ const visibleArea = overlapRect.width * overlapRect.height;
32
+ const totalArea = elementHeight * elementWidth;
33
+ return visibleArea / totalArea >= visibleAreaRatio;
34
+ }
35
+ function getPseudoElementContent(element, currentWindow) {
36
+ if (!(element instanceof currentWindow.HTMLElement)) return {
37
+ before: '',
38
+ after: ''
39
+ };
40
+ const beforeContent = currentWindow.getComputedStyle(element, '::before').getPropertyValue('content');
41
+ const afterContent = currentWindow.getComputedStyle(element, '::after').getPropertyValue('content');
42
+ return {
43
+ before: 'none' === beforeContent ? '' : beforeContent.replace(/"/g, ''),
44
+ after: 'none' === afterContent ? '' : afterContent.replace(/"/g, '')
45
+ };
46
+ }
47
+ function hasOverflowY(element, currentWindow) {
48
+ const style = currentWindow.getComputedStyle(element);
49
+ return 'scroll' === style.overflowY || 'auto' === style.overflowY || 'hidden' === style.overflowY;
50
+ }
51
+ function overlappedRect(rect1, rect2) {
52
+ const left = Math.max(rect1.left, rect2.left);
53
+ const top = Math.max(rect1.top, rect2.top);
54
+ const right = Math.min(rect1.right, rect2.right);
55
+ const bottom = Math.min(rect1.bottom, rect2.bottom);
56
+ if (left < right && top < bottom) return {
57
+ left,
58
+ top,
59
+ right,
60
+ bottom,
61
+ width: right - left,
62
+ height: bottom - top,
63
+ x: left,
64
+ y: top,
65
+ zoom: 1
66
+ };
67
+ return null;
68
+ }
69
+ function getRect(el, baseZoom, currentWindow) {
70
+ let originalRect;
71
+ let newZoom = 1;
72
+ if (el instanceof currentWindow.HTMLElement) {
73
+ originalRect = el.getBoundingClientRect();
74
+ if (!('currentCSSZoom' in el)) newZoom = Number.parseFloat(currentWindow.getComputedStyle(el).zoom) || 1;
75
+ } else {
76
+ const range = currentWindow.document.createRange();
77
+ range.selectNodeContents(el);
78
+ originalRect = range.getBoundingClientRect();
79
+ }
80
+ const zoom = newZoom * baseZoom;
81
+ return {
82
+ width: originalRect.width * zoom,
83
+ height: originalRect.height * zoom,
84
+ left: originalRect.left * zoom,
85
+ top: originalRect.top * zoom,
86
+ right: originalRect.right * zoom,
87
+ bottom: originalRect.bottom * zoom,
88
+ x: originalRect.x * zoom,
89
+ y: originalRect.y * zoom,
90
+ zoom
91
+ };
92
+ }
93
+ const isElementCovered = (el, rect, currentWindow)=>{
94
+ const x = rect.left + rect.width / 2;
95
+ const y = rect.top + rect.height / 2;
96
+ const topElement = currentWindow.document.elementFromPoint(x, y);
97
+ if (!topElement) return false;
98
+ if (topElement === el) return false;
99
+ if (null == el ? void 0 : el.contains(topElement)) return false;
100
+ if (null == topElement ? void 0 : topElement.contains(el)) return false;
101
+ const rectOfTopElement = getRect(topElement, 1, currentWindow);
102
+ const overlapRect = overlappedRect(rect, rectOfTopElement);
103
+ if (!overlapRect) return false;
104
+ logger(el, 'Element is covered by another element', {
105
+ topElement,
106
+ el,
107
+ rect,
108
+ x,
109
+ y
110
+ });
111
+ return true;
112
+ };
113
+ function elementRect(el, currentWindow, currentDocument, baseZoom = 1) {
114
+ if (!el) {
115
+ logger(el, 'Element is not in the DOM hierarchy');
116
+ return false;
117
+ }
118
+ if (!(el instanceof currentWindow.HTMLElement) && el.nodeType !== Node.TEXT_NODE && 'svg' !== el.nodeName.toLowerCase()) {
119
+ logger(el, 'Element is not in the DOM hierarchy');
120
+ return false;
121
+ }
122
+ if (el instanceof currentWindow.HTMLElement) {
123
+ const style = currentWindow.getComputedStyle(el);
124
+ if ('none' === style.display || 'hidden' === style.visibility || '0' === style.opacity && 'INPUT' !== el.tagName) {
125
+ logger(el, 'Element is hidden');
126
+ return false;
127
+ }
128
+ }
129
+ const rect = getRect(el, baseZoom, currentWindow);
130
+ if (0 === rect.width && 0 === rect.height) {
131
+ logger(el, 'Element has no size');
132
+ return false;
133
+ }
134
+ if (1 === baseZoom && isElementCovered(el, rect, currentWindow)) return false;
135
+ const isVisible = isElementPartiallyInViewport(rect, currentWindow, currentDocument);
136
+ let parent = el;
137
+ const parentUntilNonStatic = (currentNode)=>{
138
+ let parent = null == currentNode ? void 0 : currentNode.parentElement;
139
+ while(parent){
140
+ const style = currentWindow.getComputedStyle(parent);
141
+ if ('static' !== style.position) return parent;
142
+ parent = parent.parentElement;
143
+ }
144
+ return null;
145
+ };
146
+ while(parent && parent !== currentDocument.body){
147
+ if (!(parent instanceof currentWindow.HTMLElement)) {
148
+ parent = parent.parentElement;
149
+ continue;
150
+ }
151
+ const parentStyle = currentWindow.getComputedStyle(parent);
152
+ if ('hidden' === parentStyle.overflow) {
153
+ const parentRect = getRect(parent, 1, currentWindow);
154
+ const tolerance = 10;
155
+ if (rect.right < parentRect.left - tolerance || rect.left > parentRect.right + tolerance || rect.bottom < parentRect.top - tolerance || rect.top > parentRect.bottom + tolerance) {
156
+ logger(el, 'element is partially or totally hidden by an ancestor', {
157
+ rect,
158
+ parentRect
159
+ });
160
+ return false;
161
+ }
162
+ }
163
+ if ('fixed' === parentStyle.position || 'sticky' === parentStyle.position) break;
164
+ parent = 'absolute' === parentStyle.position ? parentUntilNonStatic(parent) : parent.parentElement;
165
+ }
166
+ return {
167
+ left: Math.round(rect.left),
168
+ top: Math.round(rect.top),
169
+ width: Math.round(rect.width),
170
+ height: Math.round(rect.height),
171
+ zoom: rect.zoom,
172
+ isVisible
173
+ };
174
+ }
175
+ function validTextNodeContent(node) {
176
+ if (!node) return false;
177
+ if (node.nodeType !== Node.ELEMENT_NODE && node.nodeType !== Node.TEXT_NODE && '#text' !== node.nodeName) return false;
178
+ const content = node.textContent || node.innerText;
179
+ if (content && !/^\s*$/.test(content)) return content.trim();
180
+ return false;
181
+ }
182
+ function getNodeAttributes(node, currentWindow) {
183
+ if (!node || !(node instanceof currentWindow.HTMLElement) || !node.attributes) return {};
184
+ const attributesList = Array.from(node.attributes).map((attr)=>{
185
+ if ('class' === attr.name) return [
186
+ attr.name,
187
+ `.${attr.value.split(' ').join('.')}`
188
+ ];
189
+ if (!attr.value) return [];
190
+ let value = attr.value;
191
+ if (value.startsWith('data:image')) value = 'image';
192
+ if (value.length > MAX_VALUE_LENGTH) value = `${value.slice(0, MAX_VALUE_LENGTH)}...`;
193
+ return [
194
+ attr.name,
195
+ value
196
+ ];
197
+ });
198
+ return Object.fromEntries(attributesList);
199
+ }
200
+ function midsceneGenerateHash(node, content, rect) {
201
+ const slicedHash = generateHashId(rect, content);
202
+ if (node) {
203
+ if (!window.midsceneNodeHashCacheList) setNodeHashCacheListOnWindow();
204
+ setNodeToCacheList(node, slicedHash);
205
+ }
206
+ return slicedHash;
207
+ }
208
+ function setNodeHashCacheListOnWindow() {
209
+ if ('undefined' != typeof window) window.midsceneNodeHashCacheList = [];
210
+ }
211
+ function setNodeToCacheList(node, id) {
212
+ if ('undefined' != typeof window) {
213
+ var _window_midsceneNodeHashCacheList;
214
+ if (getNodeFromCacheList(id)) return;
215
+ null == (_window_midsceneNodeHashCacheList = window.midsceneNodeHashCacheList) || _window_midsceneNodeHashCacheList.push({
216
+ node,
217
+ id
218
+ });
219
+ }
220
+ }
221
+ function getNodeFromCacheList(id) {
222
+ if ('undefined' != typeof window) {
223
+ var _window_midsceneNodeHashCacheList_find, _window_midsceneNodeHashCacheList;
224
+ return null == (_window_midsceneNodeHashCacheList = window.midsceneNodeHashCacheList) ? void 0 : null == (_window_midsceneNodeHashCacheList_find = _window_midsceneNodeHashCacheList.find((item)=>item.id === id)) ? void 0 : _window_midsceneNodeHashCacheList_find.node;
225
+ }
226
+ return null;
227
+ }
228
+ function generateId(numberId) {
229
+ return `${numberId}`;
230
+ }
231
+ function setGenerateHashOnWindow() {
232
+ if ('undefined' != typeof window) window.midsceneGenerateHash = midsceneGenerateHash;
233
+ }
234
+ function setMidsceneVisibleRectOnWindow() {
235
+ if ('undefined' != typeof window) window.midsceneVisibleRect = elementRect;
236
+ }
237
+ function setExtractTextWithPositionOnWindow() {
238
+ if ('undefined' != typeof window) window.extractTextWithPosition = extractTextWithPosition;
239
+ }
240
+ function getTopDocument() {
241
+ const container = document.body || document;
242
+ return container;
243
+ }
244
+ export { elementRect, generateId, getDebugMode, getNodeAttributes, getNodeFromCacheList, getPseudoElementContent, getRect, getTopDocument, hasOverflowY, isElementPartiallyInViewport, logger, midsceneGenerateHash, overlappedRect, setDebugMode, setExtractTextWithPositionOnWindow, setGenerateHashOnWindow, setMidsceneVisibleRectOnWindow, setNodeHashCacheListOnWindow, setNodeToCacheList, validTextNodeContent };
@@ -0,0 +1,310 @@
1
+ import { CONTAINER_MINI_HEIGHT, CONTAINER_MINI_WIDTH, NodeType } from "../constants/index.mjs";
2
+ import { isAElement, isButtonElement, isContainerElement, isFormElement, isImgElement, isTextElement } from "./dom-util.mjs";
3
+ import { descriptionOfTree } from "./tree.mjs";
4
+ import { elementRect, getNodeAttributes, getPseudoElementContent, getRect, getTopDocument, logger, midsceneGenerateHash, setDebugMode } from "./util.mjs";
5
+ let indexId = 0;
6
+ function tagNameOfNode(node) {
7
+ let tagName = '';
8
+ if (node instanceof HTMLElement) {
9
+ var _node_tagName;
10
+ tagName = null == (_node_tagName = node.tagName) ? void 0 : _node_tagName.toLowerCase();
11
+ } else {
12
+ const parentElement = node.parentElement;
13
+ if (parentElement && parentElement instanceof HTMLElement) {
14
+ var _parentElement_tagName;
15
+ tagName = null == (_parentElement_tagName = parentElement.tagName) ? void 0 : _parentElement_tagName.toLowerCase();
16
+ }
17
+ }
18
+ return tagName ? `<${tagName}>` : '';
19
+ }
20
+ function collectElementInfo(node, currentWindow, currentDocument, baseZoom = 1, basePoint = {
21
+ left: 0,
22
+ top: 0
23
+ }, isContainer = false) {
24
+ const rect = elementRect(node, currentWindow, currentDocument, baseZoom);
25
+ if (!rect) return null;
26
+ if (rect.width < CONTAINER_MINI_WIDTH || rect.height < CONTAINER_MINI_HEIGHT) return null;
27
+ if (0 !== basePoint.left || 0 !== basePoint.top) {
28
+ rect.left += basePoint.left;
29
+ rect.top += basePoint.top;
30
+ }
31
+ if (rect.height >= window.innerHeight && rect.width >= window.innerWidth) return null;
32
+ if (isFormElement(node)) {
33
+ const attributes = getNodeAttributes(node, currentWindow);
34
+ let valueContent = attributes.value || attributes.placeholder || node.textContent || '';
35
+ const nodeHashId = midsceneGenerateHash(node, valueContent, rect);
36
+ const tagName = node.tagName.toLowerCase();
37
+ if ('select' === node.tagName.toLowerCase()) {
38
+ const selectedOption = node.options[node.selectedIndex];
39
+ valueContent = (null == selectedOption ? void 0 : selectedOption.textContent) || '';
40
+ }
41
+ if (('input' === node.tagName.toLowerCase() || 'textarea' === node.tagName.toLowerCase()) && node.value) valueContent = node.value;
42
+ const elementInfo = {
43
+ id: nodeHashId,
44
+ nodeHashId,
45
+ nodeType: NodeType.FORM_ITEM,
46
+ indexId: indexId++,
47
+ attributes: {
48
+ ...attributes,
49
+ htmlTagName: `<${tagName}>`,
50
+ nodeType: NodeType.FORM_ITEM
51
+ },
52
+ content: valueContent.trim(),
53
+ rect,
54
+ center: [
55
+ Math.round(rect.left + rect.width / 2),
56
+ Math.round(rect.top + rect.height / 2)
57
+ ],
58
+ zoom: rect.zoom,
59
+ isVisible: rect.isVisible
60
+ };
61
+ return elementInfo;
62
+ }
63
+ if (isButtonElement(node)) {
64
+ const rect = mergeElementAndChildrenRects(node, currentWindow, currentDocument, baseZoom);
65
+ if (!rect) return null;
66
+ const attributes = getNodeAttributes(node, currentWindow);
67
+ const pseudo = getPseudoElementContent(node, currentWindow);
68
+ const content = node.innerText || pseudo.before || pseudo.after || '';
69
+ const nodeHashId = midsceneGenerateHash(node, content, rect);
70
+ const elementInfo = {
71
+ id: nodeHashId,
72
+ indexId: indexId++,
73
+ nodeHashId,
74
+ nodeType: NodeType.BUTTON,
75
+ attributes: {
76
+ ...attributes,
77
+ htmlTagName: tagNameOfNode(node),
78
+ nodeType: NodeType.BUTTON
79
+ },
80
+ content,
81
+ rect,
82
+ center: [
83
+ Math.round(rect.left + rect.width / 2),
84
+ Math.round(rect.top + rect.height / 2)
85
+ ],
86
+ zoom: rect.zoom,
87
+ isVisible: rect.isVisible
88
+ };
89
+ return elementInfo;
90
+ }
91
+ if (isImgElement(node)) {
92
+ var _node_nodeName;
93
+ const attributes = getNodeAttributes(node, currentWindow);
94
+ const nodeHashId = midsceneGenerateHash(node, '', rect);
95
+ const elementInfo = {
96
+ id: nodeHashId,
97
+ indexId: indexId++,
98
+ nodeHashId,
99
+ attributes: {
100
+ ...attributes,
101
+ ...(null == (_node_nodeName = node.nodeName) ? void 0 : _node_nodeName.toLowerCase()) === 'svg' ? {
102
+ svgContent: 'true'
103
+ } : {},
104
+ nodeType: NodeType.IMG,
105
+ htmlTagName: tagNameOfNode(node)
106
+ },
107
+ nodeType: NodeType.IMG,
108
+ content: '',
109
+ rect,
110
+ center: [
111
+ Math.round(rect.left + rect.width / 2),
112
+ Math.round(rect.top + rect.height / 2)
113
+ ],
114
+ zoom: rect.zoom,
115
+ isVisible: rect.isVisible
116
+ };
117
+ return elementInfo;
118
+ }
119
+ if (isTextElement(node)) {
120
+ var _node_textContent;
121
+ const text = null == (_node_textContent = node.textContent) ? void 0 : _node_textContent.trim().replace(/\n+/g, ' ');
122
+ if (!text) return null;
123
+ const attributes = getNodeAttributes(node, currentWindow);
124
+ const attributeKeys = Object.keys(attributes);
125
+ if (!text.trim() && 0 === attributeKeys.length) return null;
126
+ const nodeHashId = midsceneGenerateHash(node, text, rect);
127
+ const elementInfo = {
128
+ id: nodeHashId,
129
+ indexId: indexId++,
130
+ nodeHashId,
131
+ nodeType: NodeType.TEXT,
132
+ attributes: {
133
+ ...attributes,
134
+ nodeType: NodeType.TEXT,
135
+ htmlTagName: tagNameOfNode(node)
136
+ },
137
+ center: [
138
+ Math.round(rect.left + rect.width / 2),
139
+ Math.round(rect.top + rect.height / 2)
140
+ ],
141
+ content: text,
142
+ rect,
143
+ zoom: rect.zoom,
144
+ isVisible: rect.isVisible
145
+ };
146
+ return elementInfo;
147
+ }
148
+ if (isAElement(node)) {
149
+ const attributes = getNodeAttributes(node, currentWindow);
150
+ const pseudo = getPseudoElementContent(node, currentWindow);
151
+ const content = node.innerText || pseudo.before || pseudo.after || '';
152
+ const nodeHashId = midsceneGenerateHash(node, content, rect);
153
+ const elementInfo = {
154
+ id: nodeHashId,
155
+ indexId: indexId++,
156
+ nodeHashId,
157
+ nodeType: NodeType.A,
158
+ attributes: {
159
+ ...attributes,
160
+ htmlTagName: tagNameOfNode(node),
161
+ nodeType: NodeType.A
162
+ },
163
+ content,
164
+ rect,
165
+ center: [
166
+ Math.round(rect.left + rect.width / 2),
167
+ Math.round(rect.top + rect.height / 2)
168
+ ],
169
+ zoom: rect.zoom,
170
+ isVisible: rect.isVisible
171
+ };
172
+ return elementInfo;
173
+ }
174
+ if (isContainerElement(node) || isContainer) {
175
+ const attributes = getNodeAttributes(node, currentWindow);
176
+ const nodeHashId = midsceneGenerateHash(node, '', rect);
177
+ const elementInfo = {
178
+ id: nodeHashId,
179
+ nodeHashId,
180
+ indexId: indexId++,
181
+ nodeType: NodeType.CONTAINER,
182
+ attributes: {
183
+ ...attributes,
184
+ nodeType: NodeType.CONTAINER,
185
+ htmlTagName: tagNameOfNode(node)
186
+ },
187
+ content: '',
188
+ rect,
189
+ center: [
190
+ Math.round(rect.left + rect.width / 2),
191
+ Math.round(rect.top + rect.height / 2)
192
+ ],
193
+ zoom: rect.zoom,
194
+ isVisible: rect.isVisible
195
+ };
196
+ return elementInfo;
197
+ }
198
+ return null;
199
+ }
200
+ function extractTextWithPosition(initNode, debugMode = false) {
201
+ const elementNode = extractTreeNode(initNode, debugMode);
202
+ const elementInfoArray = [];
203
+ function dfsTopChildren(node) {
204
+ if (node.node) elementInfoArray.push(node.node);
205
+ for(let i = 0; i < node.children.length; i++)dfsTopChildren(node.children[i]);
206
+ }
207
+ dfsTopChildren({
208
+ children: elementNode.children,
209
+ node: elementNode.node
210
+ });
211
+ return elementInfoArray;
212
+ }
213
+ function extractTreeNodeAsString(initNode, visibleOnly = false, debugMode = false) {
214
+ const elementNode = extractTreeNode(initNode, debugMode);
215
+ return descriptionOfTree(elementNode, void 0, false, visibleOnly);
216
+ }
217
+ function extractTreeNode(initNode, debugMode = false) {
218
+ setDebugMode(debugMode);
219
+ indexId = 0;
220
+ const topDocument = getTopDocument();
221
+ const startNode = initNode || topDocument;
222
+ const topChildren = [];
223
+ function dfs(node, currentWindow, currentDocument, baseZoom = 1, basePoint = {
224
+ left: 0,
225
+ top: 0
226
+ }) {
227
+ if (!node) return null;
228
+ if (node.nodeType && 10 === node.nodeType) return null;
229
+ const elementInfo = collectElementInfo(node, currentWindow, currentDocument, baseZoom, basePoint);
230
+ if (node instanceof currentWindow.HTMLIFrameElement) {
231
+ if (node.contentWindow && node.contentWindow) return null;
232
+ }
233
+ const nodeInfo = {
234
+ node: elementInfo,
235
+ children: []
236
+ };
237
+ if ((null == elementInfo ? void 0 : elementInfo.nodeType) === NodeType.BUTTON || (null == elementInfo ? void 0 : elementInfo.nodeType) === NodeType.IMG || (null == elementInfo ? void 0 : elementInfo.nodeType) === NodeType.TEXT || (null == elementInfo ? void 0 : elementInfo.nodeType) === NodeType.FORM_ITEM || (null == elementInfo ? void 0 : elementInfo.nodeType) === NodeType.CONTAINER) return nodeInfo;
238
+ const rect = getRect(node, baseZoom, currentWindow);
239
+ for(let i = 0; i < node.childNodes.length; i++){
240
+ logger('will dfs', node.childNodes[i]);
241
+ const childNodeInfo = dfs(node.childNodes[i], currentWindow, currentDocument, rect.zoom, basePoint);
242
+ if (Array.isArray(childNodeInfo)) nodeInfo.children.push(...childNodeInfo);
243
+ else if (childNodeInfo) nodeInfo.children.push(childNodeInfo);
244
+ }
245
+ if (null === nodeInfo.node) {
246
+ if (0 === nodeInfo.children.length) return null;
247
+ return nodeInfo.children;
248
+ }
249
+ return nodeInfo;
250
+ }
251
+ const rootNodeInfo = dfs(startNode, window, document, 1, {
252
+ left: 0,
253
+ top: 0
254
+ });
255
+ if (Array.isArray(rootNodeInfo)) topChildren.push(...rootNodeInfo);
256
+ else if (rootNodeInfo) topChildren.push(rootNodeInfo);
257
+ if (startNode === topDocument) {
258
+ const iframes = document.querySelectorAll('iframe');
259
+ for(let i = 0; i < iframes.length; i++){
260
+ const iframe = iframes[i];
261
+ if (iframe.contentDocument && iframe.contentWindow) {
262
+ const iframeInfo = collectElementInfo(iframe, window, document, 1);
263
+ if (iframeInfo) {
264
+ const iframeChildren = dfs(iframe.contentDocument.body, iframe.contentWindow, iframe.contentDocument, 1, {
265
+ left: iframeInfo.rect.left,
266
+ top: iframeInfo.rect.top
267
+ });
268
+ if (Array.isArray(iframeChildren)) topChildren.push(...iframeChildren);
269
+ else if (iframeChildren) topChildren.push(iframeChildren);
270
+ }
271
+ }
272
+ }
273
+ }
274
+ return {
275
+ node: null,
276
+ children: topChildren
277
+ };
278
+ }
279
+ function mergeElementAndChildrenRects(node, currentWindow, currentDocument, baseZoom = 1) {
280
+ const selfRect = elementRect(node, currentWindow, currentDocument, baseZoom);
281
+ if (!selfRect) return null;
282
+ let minLeft = selfRect.left;
283
+ let minTop = selfRect.top;
284
+ let maxRight = selfRect.left + selfRect.width;
285
+ let maxBottom = selfRect.top + selfRect.height;
286
+ function traverse(child) {
287
+ for(let i = 0; i < child.childNodes.length; i++){
288
+ const sub = child.childNodes[i];
289
+ if (1 === sub.nodeType) {
290
+ const rect = elementRect(sub, currentWindow, currentDocument, baseZoom);
291
+ if (rect) {
292
+ minLeft = Math.min(minLeft, rect.left);
293
+ minTop = Math.min(minTop, rect.top);
294
+ maxRight = Math.max(maxRight, rect.left + rect.width);
295
+ maxBottom = Math.max(maxBottom, rect.top + rect.height);
296
+ }
297
+ traverse(sub);
298
+ }
299
+ }
300
+ }
301
+ traverse(node);
302
+ return {
303
+ ...selfRect,
304
+ left: minLeft,
305
+ top: minTop,
306
+ width: maxRight - minLeft,
307
+ height: maxBottom - minTop
308
+ };
309
+ }
310
+ export { collectElementInfo, extractTextWithPosition, extractTreeNode, extractTreeNodeAsString, mergeElementAndChildrenRects };