automation_model 1.0.565-dev → 1.0.565-stage

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,126 +0,0 @@
1
- /**
2
- * Find elements by universal selector and check if they match the provided text pattern.
3
- */
4
- function findMatchingElements(textToMatch, options = {}, root = document) {
5
- function findLeafElements(elements) {
6
- const nonLeaf = new Set();
7
- elements.forEach((el) => {
8
- elements.forEach((otherEl) => {
9
- if (el !== otherEl && el.contains(otherEl)) {
10
- nonLeaf.add(el);
11
- }
12
- });
13
- });
14
- return elements.filter((el) => !nonLeaf.has(el));
15
- }
16
-
17
- function escapeRegex(s) {
18
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
19
- }
20
- function collectAllShadowDomElements(element, result = []) {
21
- // Check and add the element if it has a shadow root
22
- if (element instanceof Element && element.shadowRoot) {
23
- result.push(element);
24
- // Also search within the shadow root
25
- collectAllShadowDomElements(element.shadowRoot, result);
26
- }
27
-
28
- // Iterate over child nodes
29
- element.childNodes.forEach((child) => {
30
- // Recursively call the function for each child node
31
- if (child instanceof Element) {
32
- collectAllShadowDomElements(child, result);
33
- }
34
- });
35
-
36
- return result;
37
- }
38
- /**
39
- * Convert a single snippet into an OR group for:
40
- * 1) literal text
41
- * 2) interpreted as a raw regex
42
- */
43
- function snippetToAlternatives(snippet) {
44
- if (snippet.startsWith("/") && snippet.endsWith("/")) {
45
- return snippet.slice(1, -1);
46
- }
47
- const literalPattern = escapeRegex(snippet);
48
- return literalPattern;
49
- }
50
-
51
- /**
52
- * Build a single RegExp that:
53
- * - splits the user's text by whitespace
54
- * - for each token, matches either literal or regex
55
- * - allows arbitrary whitespace between tokens
56
- */
57
- function buildLooseRegexFromText(text, options) {
58
- if (options.singleRegex === true) {
59
- return new RegExp(text, options.ignoreCase === false ? "" : "i");
60
- }
61
- const tokens = text.split(/\s+/);
62
- let pattern = tokens.map((token) => snippetToAlternatives(token)).join("\\s*");
63
- if (options.exactMatch === true) {
64
- pattern = `^${pattern}$`;
65
- }
66
- // check if one of the tokens end with /i
67
- const endWithI = tokens.some((token) => token.endsWith("/i"));
68
- return new RegExp(pattern, endWithI || options.ignoreCase === false ? "" : "i");
69
- }
70
- let climb = 0;
71
- // check if the text to merge end with ^ follow by a number (climb), e.g. "some text^2" we should set the climb and remove the ^2 from the text
72
- if (textToMatch.match(/\^(\d+)$/)) {
73
- climb = parseInt(textToMatch.match(/\^(\d+)$/)[1]);
74
- textToMatch = textToMatch.replace(/\^(\d+)$/, "");
75
- }
76
-
77
- let tag = options.tag || "*";
78
- // Build the pattern
79
- const regex = buildLooseRegexFromText(textToMatch, options);
80
- // Query all elements
81
- let elements = Array.from(root.querySelectorAll(tag));
82
-
83
- let shadowHosts = [];
84
- collectAllShadowDomElements(document, shadowHosts);
85
- for (let i = 0; i < shadowHosts.length; i++) {
86
- let shadowElement = shadowHosts[i].shadowRoot;
87
- if (!shadowElement) {
88
- continue;
89
- }
90
- let shadowElements = Array.from(shadowElement.querySelectorAll(tag));
91
- elements = elements.concat(shadowElements);
92
- }
93
-
94
- // filter out elements that are style or script tags
95
- elements = elements.filter((el) => !["STYLE", "SCRIPT", "HEAD"].includes(el.tagName));
96
- elements = findLeafElements(
97
- elements.filter((el) => {
98
- // Normalize text content
99
- let normalized = options.innerText === false || !el.innerText ? el.textContent : el.innerText;
100
- if (!normalized) {
101
- normalized = "";
102
- }
103
- let normalizedSpace = normalized.replace(/\s+/g, " ").trim();
104
- let normalizedNoSpace = normalized.replace(/\s+/g, "").trim();
105
- regex.lastIndex = 0; // reset if using 'g'
106
- return regex.test(normalizedSpace) || regex.test(normalizedNoSpace);
107
- })
108
- );
109
- // if climb is greater than 0, we should climb up the DOM tree for each element
110
- if (climb > 0) {
111
- let newElements = [];
112
- for (let i = 0; i < elements.length; i++) {
113
- let el = elements[i];
114
- for (let j = 0; j < climb; j++) {
115
- if (!el.parentElement) {
116
- break;
117
- }
118
- el = el.parentElement;
119
- }
120
- newElements.push(el);
121
- }
122
- elements = newElements;
123
- }
124
- return elements;
125
- }
126
- window.findMatchingElements = findMatchingElements;