@usertour/helpers 0.0.6 → 0.0.7

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.
@@ -1,3 +1,6 @@
1
+ import {
2
+ finderV2
3
+ } from "./chunk-DEG6MTU7.js";
1
4
  import {
2
5
  off,
3
6
  on
@@ -13,7 +16,6 @@ import {
13
16
 
14
17
  // src/conditions.ts
15
18
  import { computePosition, hide } from "@floating-ui/dom";
16
- import { finderV2 } from "@usertour-packages/finder";
17
19
  import {
18
20
  BizEvents,
19
21
  ContentDataType,
@@ -0,0 +1,318 @@
1
+ // src/finderx.ts
2
+ import { finder as finderLib } from "@medv/finder";
3
+ var finderAttrs = [
4
+ "data-for",
5
+ "data-id",
6
+ "data-testid",
7
+ "data-test-id",
8
+ "for",
9
+ "id",
10
+ "name",
11
+ "placeholder",
12
+ "role"
13
+ ];
14
+ var defaultConfig = {
15
+ idName: () => false,
16
+ className: () => false,
17
+ tagName: () => false,
18
+ attr: () => false,
19
+ seedMinLength: 1,
20
+ optimizedMinLength: 2,
21
+ threshold: 1e3,
22
+ maxNumberOfTries: 1e4
23
+ };
24
+ var finderConfigs = [
25
+ {
26
+ ...defaultConfig,
27
+ tagName: () => true
28
+ },
29
+ {
30
+ ...defaultConfig,
31
+ idName: () => true
32
+ },
33
+ {
34
+ ...defaultConfig,
35
+ tagName: () => true,
36
+ attr: (name) => finderAttrs.includes(name)
37
+ },
38
+ {
39
+ ...defaultConfig,
40
+ className: () => true,
41
+ attr: (name) => finderAttrs.includes(name)
42
+ },
43
+ {
44
+ ...defaultConfig,
45
+ tagName: () => true,
46
+ idName: () => true,
47
+ className: () => true,
48
+ attr: () => false
49
+ },
50
+ {
51
+ ...defaultConfig,
52
+ tagName: () => true,
53
+ idName: () => true,
54
+ className: () => true,
55
+ attr: (name) => finderAttrs.includes(name)
56
+ }
57
+ ];
58
+ function getMaxDepth(node) {
59
+ if (node.parentNode) {
60
+ return getMaxDepth(node.parentNode);
61
+ }
62
+ return node.depth;
63
+ }
64
+ function queryNodeListBySelectors(selectors, rootDocument, removeRepeat = true) {
65
+ const nodes = [];
66
+ if (!selectors) {
67
+ return nodes;
68
+ }
69
+ for (const s of selectors) {
70
+ const els = rootDocument.querySelectorAll(s.replace(/\\\\/g, "\\"));
71
+ if (els && els.length > 0) {
72
+ nodes.push(...Array.from(els));
73
+ }
74
+ }
75
+ return removeRepeat ? [...new Set(nodes)] : nodes;
76
+ }
77
+ function findMostRecurringNode(nodes) {
78
+ const m = /* @__PURE__ */ new Map();
79
+ let finalNode = nodes[0];
80
+ let count = 0;
81
+ for (const node of nodes) {
82
+ const i = m.get(node) ? m.get(node) + 1 : 1;
83
+ m.set(node, i);
84
+ }
85
+ m.forEach((value, key) => {
86
+ if (value > count) {
87
+ count = value;
88
+ finalNode = key;
89
+ }
90
+ });
91
+ return finalNode;
92
+ }
93
+ function compareParentNode(node, el, rootDocument, isCompareSibings = false) {
94
+ let nodeParentNode = node.parentNode;
95
+ let elParentElement = el.parentElement;
96
+ const maxDepth = getMaxDepth(node);
97
+ const xresult = {
98
+ maxDepth,
99
+ failedDepth: 0,
100
+ success: true
101
+ };
102
+ while (nodeParentNode && elParentElement) {
103
+ if (elParentElement === rootDocument) {
104
+ break;
105
+ }
106
+ if (elParentElement === document.body || elParentElement === document.documentElement || elParentElement.parentElement === document.body) {
107
+ break;
108
+ }
109
+ const parentNodes = queryNodeListBySelectors(nodeParentNode.selectors, rootDocument);
110
+ const isMatchSibings = isCompareSibings ? compareSibingsNode(nodeParentNode, elParentElement, rootDocument) : true;
111
+ if (!parentNodes || parentNodes.length === 0 || !parentNodes.includes(elParentElement) || !isMatchSibings) {
112
+ xresult.failedDepth = nodeParentNode.depth;
113
+ xresult.success = false;
114
+ }
115
+ nodeParentNode = nodeParentNode.parentNode;
116
+ elParentElement = elParentElement.parentElement;
117
+ }
118
+ return xresult;
119
+ }
120
+ function compareSibingsNode(node, el, rootDocument) {
121
+ let isMatchNext = true;
122
+ let isMatchPrevious = true;
123
+ const { previousElementSelectors, nextElementSelectors } = node;
124
+ if (nextElementSelectors && nextElementSelectors.length > 0) {
125
+ const nextElementSiblings = queryNodeListBySelectors(nextElementSelectors, rootDocument);
126
+ isMatchNext = el.nextElementSibling && nextElementSiblings.includes(el.nextElementSibling);
127
+ }
128
+ if (previousElementSelectors && previousElementSelectors.length > 0) {
129
+ const previousElementSiblings = queryNodeListBySelectors(
130
+ previousElementSelectors,
131
+ rootDocument
132
+ );
133
+ isMatchPrevious = el.previousElementSibling && previousElementSiblings.includes(el.previousElementSibling);
134
+ }
135
+ return isMatchNext && isMatchPrevious;
136
+ }
137
+ function queryElementSelectors(input) {
138
+ const classes = Array.from(input.classList);
139
+ const selectors = [];
140
+ const configs = [...finderConfigs];
141
+ for (const className of classes) {
142
+ configs.push({
143
+ ...defaultConfig,
144
+ className: (name) => {
145
+ if (classes.filter((cn) => cn !== className).includes(name)) {
146
+ return false;
147
+ }
148
+ return true;
149
+ }
150
+ });
151
+ }
152
+ try {
153
+ for (const cfg of configs) {
154
+ selectors.push(finder(input, cfg));
155
+ }
156
+ } catch (_) {
157
+ return selectors;
158
+ }
159
+ return [...new Set(selectors)];
160
+ }
161
+ function parseSelectorsTree(input, parentNode, depth = 0) {
162
+ const selectors = queryElementSelectors(input);
163
+ if (selectors.length === 0) {
164
+ return parentNode;
165
+ }
166
+ const currentNode = {
167
+ previousElementSelectors: [],
168
+ nextElementSelectors: [],
169
+ selectors,
170
+ depth
171
+ };
172
+ if (input.previousElementSibling) {
173
+ currentNode.previousElementSelectors = queryElementSelectors(input.previousElementSibling);
174
+ }
175
+ if (input.nextElementSibling) {
176
+ currentNode.nextElementSelectors = queryElementSelectors(input.nextElementSibling);
177
+ }
178
+ if (parentNode === null) {
179
+ if (input.parentElement) {
180
+ parseSelectorsTree(input.parentElement, currentNode, depth + 1);
181
+ }
182
+ return currentNode;
183
+ }
184
+ parentNode.parentNode = currentNode;
185
+ if (input.parentElement) {
186
+ parseSelectorsTree(input.parentElement, currentNode, depth + 1);
187
+ }
188
+ return parentNode;
189
+ }
190
+ function finderMostPrecisionElement(elements, node, rootDocument, precision) {
191
+ const successEls = [];
192
+ let failedData = {
193
+ el: null,
194
+ failedDepth: 0,
195
+ maxDepth: 0
196
+ };
197
+ for (const el of elements) {
198
+ const { success, failedDepth, maxDepth } = compareParentNode(node, el, rootDocument);
199
+ if (success) {
200
+ successEls.push(el);
201
+ } else if (!failedData.el || failedDepth > failedData.failedDepth) {
202
+ failedData = { el, failedDepth, maxDepth };
203
+ }
204
+ }
205
+ if (successEls.length === 1) {
206
+ return successEls[0];
207
+ }
208
+ if (successEls.length > 1) {
209
+ let tempEl = successEls[0];
210
+ let tempFailedDepth = 0;
211
+ for (const el of successEls) {
212
+ const { success, failedDepth } = compareParentNode(node, el, rootDocument, true);
213
+ if (success) {
214
+ return el;
215
+ }
216
+ if (failedDepth > tempFailedDepth) {
217
+ tempFailedDepth = failedDepth;
218
+ tempEl = el;
219
+ }
220
+ }
221
+ return tempEl;
222
+ }
223
+ if (failedData.el) {
224
+ const { failedDepth, maxDepth, el } = failedData;
225
+ const rate = (failedDepth - 1) / maxDepth * 10;
226
+ if (rate >= precision) {
227
+ return el;
228
+ }
229
+ }
230
+ return null;
231
+ }
232
+ function finder(input, options) {
233
+ return finderLib(input, options);
234
+ }
235
+ function parserX(input) {
236
+ return parseSelectorsTree(input, null);
237
+ }
238
+ function parserV2(element) {
239
+ var _a;
240
+ const content = (_a = element.innerText) != null ? _a : "";
241
+ const selectors = parseSelectorsTree(element, null);
242
+ const selectorsList = queryElementSelectors(element);
243
+ return { content, selectors, selectorsList };
244
+ }
245
+ function finderV2(target, root) {
246
+ const {
247
+ selectors,
248
+ content = "",
249
+ sequence = 0,
250
+ precision = "strict",
251
+ isDynamicContent = false,
252
+ customSelector = "",
253
+ type = "auto"
254
+ } = target;
255
+ if (type === "auto") {
256
+ const mapping = {
257
+ looser: 1,
258
+ loose: 3,
259
+ loosest: 5,
260
+ strict: 7,
261
+ stricter: 8,
262
+ strictest: 10
263
+ };
264
+ const el = finderX(selectors, root, mapping[precision]);
265
+ if (el) {
266
+ if (isDynamicContent && content && el.innerText !== content) {
267
+ return null;
268
+ }
269
+ return el;
270
+ }
271
+ } else {
272
+ const sequenceMapping = {
273
+ "1st": 0,
274
+ "2st": 1,
275
+ "3st": 2,
276
+ "4st": 3,
277
+ "5st": 4
278
+ };
279
+ if (customSelector) {
280
+ const selector = customSelector.replace(/\\\\/g, "\\");
281
+ const els = root.querySelectorAll(selector);
282
+ if (els.length > 0) {
283
+ const el = els[sequenceMapping[sequence]] || els[0];
284
+ if (content && el.innerText.trim() !== content) {
285
+ return null;
286
+ }
287
+ return el;
288
+ }
289
+ }
290
+ }
291
+ return null;
292
+ }
293
+ function finderX(node, root, precision = 10) {
294
+ if (!node || node.selectors.length === 0) {
295
+ return null;
296
+ }
297
+ const rootDocument = root || document;
298
+ const elements = [];
299
+ const nodeList = queryNodeListBySelectors(node.selectors, rootDocument, false);
300
+ if (!nodeList || nodeList.length === 0) {
301
+ return null;
302
+ }
303
+ if ([...new Set(nodeList)].length !== nodeList.length) {
304
+ const el = findMostRecurringNode(nodeList);
305
+ elements.push(el);
306
+ } else {
307
+ elements.push(...nodeList);
308
+ }
309
+ return finderMostPrecisionElement(elements, node, rootDocument, precision);
310
+ }
311
+
312
+ export {
313
+ finder,
314
+ parserX,
315
+ parserV2,
316
+ finderV2,
317
+ finderX
318
+ };
@@ -51,7 +51,251 @@ __export(conditions_exports, {
51
51
  });
52
52
  module.exports = __toCommonJS(conditions_exports);
53
53
  var import_dom = require("@floating-ui/dom");
54
- var import_finder = require("@usertour-packages/finder");
54
+
55
+ // src/finderx.ts
56
+ var import_finder = require("@medv/finder");
57
+ var finderAttrs = [
58
+ "data-for",
59
+ "data-id",
60
+ "data-testid",
61
+ "data-test-id",
62
+ "for",
63
+ "id",
64
+ "name",
65
+ "placeholder",
66
+ "role"
67
+ ];
68
+ var defaultConfig = {
69
+ idName: () => false,
70
+ className: () => false,
71
+ tagName: () => false,
72
+ attr: () => false,
73
+ seedMinLength: 1,
74
+ optimizedMinLength: 2,
75
+ threshold: 1e3,
76
+ maxNumberOfTries: 1e4
77
+ };
78
+ var finderConfigs = [
79
+ {
80
+ ...defaultConfig,
81
+ tagName: () => true
82
+ },
83
+ {
84
+ ...defaultConfig,
85
+ idName: () => true
86
+ },
87
+ {
88
+ ...defaultConfig,
89
+ tagName: () => true,
90
+ attr: (name) => finderAttrs.includes(name)
91
+ },
92
+ {
93
+ ...defaultConfig,
94
+ className: () => true,
95
+ attr: (name) => finderAttrs.includes(name)
96
+ },
97
+ {
98
+ ...defaultConfig,
99
+ tagName: () => true,
100
+ idName: () => true,
101
+ className: () => true,
102
+ attr: () => false
103
+ },
104
+ {
105
+ ...defaultConfig,
106
+ tagName: () => true,
107
+ idName: () => true,
108
+ className: () => true,
109
+ attr: (name) => finderAttrs.includes(name)
110
+ }
111
+ ];
112
+ function getMaxDepth(node) {
113
+ if (node.parentNode) {
114
+ return getMaxDepth(node.parentNode);
115
+ }
116
+ return node.depth;
117
+ }
118
+ function queryNodeListBySelectors(selectors, rootDocument, removeRepeat = true) {
119
+ const nodes = [];
120
+ if (!selectors) {
121
+ return nodes;
122
+ }
123
+ for (const s of selectors) {
124
+ const els = rootDocument.querySelectorAll(s.replace(/\\\\/g, "\\"));
125
+ if (els && els.length > 0) {
126
+ nodes.push(...Array.from(els));
127
+ }
128
+ }
129
+ return removeRepeat ? [...new Set(nodes)] : nodes;
130
+ }
131
+ function findMostRecurringNode(nodes) {
132
+ const m = /* @__PURE__ */ new Map();
133
+ let finalNode = nodes[0];
134
+ let count = 0;
135
+ for (const node of nodes) {
136
+ const i = m.get(node) ? m.get(node) + 1 : 1;
137
+ m.set(node, i);
138
+ }
139
+ m.forEach((value, key) => {
140
+ if (value > count) {
141
+ count = value;
142
+ finalNode = key;
143
+ }
144
+ });
145
+ return finalNode;
146
+ }
147
+ function compareParentNode(node, el, rootDocument, isCompareSibings = false) {
148
+ let nodeParentNode = node.parentNode;
149
+ let elParentElement = el.parentElement;
150
+ const maxDepth = getMaxDepth(node);
151
+ const xresult = {
152
+ maxDepth,
153
+ failedDepth: 0,
154
+ success: true
155
+ };
156
+ while (nodeParentNode && elParentElement) {
157
+ if (elParentElement === rootDocument) {
158
+ break;
159
+ }
160
+ if (elParentElement === document.body || elParentElement === document.documentElement || elParentElement.parentElement === document.body) {
161
+ break;
162
+ }
163
+ const parentNodes = queryNodeListBySelectors(nodeParentNode.selectors, rootDocument);
164
+ const isMatchSibings = isCompareSibings ? compareSibingsNode(nodeParentNode, elParentElement, rootDocument) : true;
165
+ if (!parentNodes || parentNodes.length === 0 || !parentNodes.includes(elParentElement) || !isMatchSibings) {
166
+ xresult.failedDepth = nodeParentNode.depth;
167
+ xresult.success = false;
168
+ }
169
+ nodeParentNode = nodeParentNode.parentNode;
170
+ elParentElement = elParentElement.parentElement;
171
+ }
172
+ return xresult;
173
+ }
174
+ function compareSibingsNode(node, el, rootDocument) {
175
+ let isMatchNext = true;
176
+ let isMatchPrevious = true;
177
+ const { previousElementSelectors, nextElementSelectors } = node;
178
+ if (nextElementSelectors && nextElementSelectors.length > 0) {
179
+ const nextElementSiblings = queryNodeListBySelectors(nextElementSelectors, rootDocument);
180
+ isMatchNext = el.nextElementSibling && nextElementSiblings.includes(el.nextElementSibling);
181
+ }
182
+ if (previousElementSelectors && previousElementSelectors.length > 0) {
183
+ const previousElementSiblings = queryNodeListBySelectors(
184
+ previousElementSelectors,
185
+ rootDocument
186
+ );
187
+ isMatchPrevious = el.previousElementSibling && previousElementSiblings.includes(el.previousElementSibling);
188
+ }
189
+ return isMatchNext && isMatchPrevious;
190
+ }
191
+ function finderMostPrecisionElement(elements, node, rootDocument, precision) {
192
+ const successEls = [];
193
+ let failedData = {
194
+ el: null,
195
+ failedDepth: 0,
196
+ maxDepth: 0
197
+ };
198
+ for (const el of elements) {
199
+ const { success, failedDepth, maxDepth } = compareParentNode(node, el, rootDocument);
200
+ if (success) {
201
+ successEls.push(el);
202
+ } else if (!failedData.el || failedDepth > failedData.failedDepth) {
203
+ failedData = { el, failedDepth, maxDepth };
204
+ }
205
+ }
206
+ if (successEls.length === 1) {
207
+ return successEls[0];
208
+ }
209
+ if (successEls.length > 1) {
210
+ let tempEl = successEls[0];
211
+ let tempFailedDepth = 0;
212
+ for (const el of successEls) {
213
+ const { success, failedDepth } = compareParentNode(node, el, rootDocument, true);
214
+ if (success) {
215
+ return el;
216
+ }
217
+ if (failedDepth > tempFailedDepth) {
218
+ tempFailedDepth = failedDepth;
219
+ tempEl = el;
220
+ }
221
+ }
222
+ return tempEl;
223
+ }
224
+ if (failedData.el) {
225
+ const { failedDepth, maxDepth, el } = failedData;
226
+ const rate = (failedDepth - 1) / maxDepth * 10;
227
+ if (rate >= precision) {
228
+ return el;
229
+ }
230
+ }
231
+ return null;
232
+ }
233
+ function finderV2(target, root) {
234
+ const {
235
+ selectors,
236
+ content = "",
237
+ sequence = 0,
238
+ precision = "strict",
239
+ isDynamicContent = false,
240
+ customSelector = "",
241
+ type = "auto"
242
+ } = target;
243
+ if (type === "auto") {
244
+ const mapping = {
245
+ looser: 1,
246
+ loose: 3,
247
+ loosest: 5,
248
+ strict: 7,
249
+ stricter: 8,
250
+ strictest: 10
251
+ };
252
+ const el = finderX(selectors, root, mapping[precision]);
253
+ if (el) {
254
+ if (isDynamicContent && content && el.innerText !== content) {
255
+ return null;
256
+ }
257
+ return el;
258
+ }
259
+ } else {
260
+ const sequenceMapping = {
261
+ "1st": 0,
262
+ "2st": 1,
263
+ "3st": 2,
264
+ "4st": 3,
265
+ "5st": 4
266
+ };
267
+ if (customSelector) {
268
+ const selector = customSelector.replace(/\\\\/g, "\\");
269
+ const els = root.querySelectorAll(selector);
270
+ if (els.length > 0) {
271
+ const el = els[sequenceMapping[sequence]] || els[0];
272
+ if (content && el.innerText.trim() !== content) {
273
+ return null;
274
+ }
275
+ return el;
276
+ }
277
+ }
278
+ }
279
+ return null;
280
+ }
281
+ function finderX(node, root, precision = 10) {
282
+ if (!node || node.selectors.length === 0) {
283
+ return null;
284
+ }
285
+ const rootDocument = root || document;
286
+ const elements = [];
287
+ const nodeList = queryNodeListBySelectors(node.selectors, rootDocument, false);
288
+ if (!nodeList || nodeList.length === 0) {
289
+ return null;
290
+ }
291
+ if ([...new Set(nodeList)].length !== nodeList.length) {
292
+ const el = findMostRecurringNode(nodeList);
293
+ elements.push(el);
294
+ } else {
295
+ elements.push(...nodeList);
296
+ }
297
+ return finderMostPrecisionElement(elements, node, rootDocument, precision);
298
+ }
55
299
 
56
300
  // src/condition.ts
57
301
  var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
@@ -180,7 +424,7 @@ var ArrayProto = Array.prototype;
180
424
  var nativeForEach = ArrayProto.forEach;
181
425
  var nativeIndexOf = ArrayProto.indexOf;
182
426
  var navigator = global == null ? void 0 : global.navigator;
183
- var document = global == null ? void 0 : global.document;
427
+ var document2 = global == null ? void 0 : global.document;
184
428
  var location = global == null ? void 0 : global.location;
185
429
  var fetch = global == null ? void 0 : global.fetch;
186
430
  var XMLHttpRequest = (global == null ? void 0 : global.XMLHttpRequest) && "withCredentials" in new global.XMLHttpRequest() ? global.XMLHttpRequest : void 0;
@@ -252,10 +496,10 @@ var isActivedContentRulesCondition = (rules, contentSession) => {
252
496
  };
253
497
  var isVisible = async (el) => {
254
498
  var _a, _b;
255
- if (!((_a = document) == null ? void 0 : _a.body)) {
499
+ if (!((_a = document2) == null ? void 0 : _a.body)) {
256
500
  return false;
257
501
  }
258
- const { middlewareData } = await (0, import_dom.computePosition)(el, document.body, {
502
+ const { middlewareData } = await (0, import_dom.computePosition)(el, document2.body, {
259
503
  strategy: "fixed",
260
504
  middleware: [(0, import_dom.hide)()]
261
505
  });
@@ -279,10 +523,10 @@ var isClicked = (el) => {
279
523
  };
280
524
  var isActiveRulesByElement = async (rules) => {
281
525
  const { data } = rules;
282
- if (!document) {
526
+ if (!document2) {
283
527
  return false;
284
528
  }
285
- const el = (0, import_finder.finderV2)(data.elementData, document);
529
+ const el = finderV2(data.elementData, document2);
286
530
  const isPresent = el ? await isVisible(el) : false;
287
531
  const isDisabled = el ? el.disabled : false;
288
532
  switch (data.logic) {
@@ -306,10 +550,10 @@ var isActiveRulesByTextInput = async (rules) => {
306
550
  const {
307
551
  data: { elementData, logic, value }
308
552
  } = rules;
309
- if (!document) {
553
+ if (!document2) {
310
554
  return false;
311
555
  }
312
- const el = (0, import_finder.finderV2)(elementData, document);
556
+ const el = finderV2(elementData, document2);
313
557
  if (!el) {
314
558
  return false;
315
559
  }
@@ -344,10 +588,10 @@ var isActiveRulesByTextFill = async (rules) => {
344
588
  const {
345
589
  data: { elementData }
346
590
  } = rules;
347
- if (!document) {
591
+ if (!document2) {
348
592
  return false;
349
593
  }
350
- const el = (0, import_finder.finderV2)(elementData, document);
594
+ const el = finderV2(elementData, document2);
351
595
  if (!el) {
352
596
  return false;
353
597
  }
@@ -363,13 +607,13 @@ var isActiveRulesByTextFill = async (rules) => {
363
607
  return true;
364
608
  }
365
609
  if (timestamp !== -1 && now - timestamp > 1e3 && value !== el.value) {
366
- off(document, "click", onKeyup);
610
+ off(document2, "click", onKeyup);
367
611
  fillCache.set(el, { timestamp, value, isActive: true });
368
612
  return true;
369
613
  }
370
614
  return false;
371
615
  }
372
- on(document, "keyup", onKeyup);
616
+ on(document2, "keyup", onKeyup);
373
617
  fillCache.set(el, { timestamp: -1, value: el.value, isActive: false });
374
618
  return false;
375
619
  };
@@ -17,7 +17,8 @@ import {
17
17
  isVisible,
18
18
  parseUrlParams,
19
19
  wait
20
- } from "./chunk-OASNBRBF.js";
20
+ } from "./chunk-7IK5Q5N2.js";
21
+ import "./chunk-DEG6MTU7.js";
21
22
  import "./chunk-Y6FPPOKF.js";
22
23
  import "./chunk-H7VA3ML2.js";
23
24
  import "./chunk-FNQIIEWK.js";