@cocreate/element-prototype 1.29.3 → 1.30.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.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/getAttribute.js +21 -2
- package/src/operators.js +311 -52
- package/src/setValue.js +8 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.30.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.29.3...v1.30.0) (2025-10-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add async support for getAttribute and enhance processOperators with async handling ([88cfe33](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/88cfe33deff47ecd3c80b1c4c8b2de04b38530b1))
|
|
7
|
+
|
|
1
8
|
## [1.29.3](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.29.2...v1.29.3) (2025-07-13)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/element-prototype",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.30.0",
|
|
4
4
|
"description": "A simple element-prototype component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"element-prototype",
|
package/src/getAttribute.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { processOperators } from "./operators";
|
|
1
|
+
import { processOperators, processOperatorsAsync } from "./operators";
|
|
2
2
|
|
|
3
3
|
// Store a reference to the original getAttribute function
|
|
4
4
|
const originalGetAttribute = Element.prototype.getAttribute;
|
|
@@ -41,10 +41,29 @@ function getAttribute(element, name) {
|
|
|
41
41
|
return processOperators(element, value);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Asynchronously gets an attribute and processes its value for operators.
|
|
46
|
+
* @param {Element} element - The element from which to get the attribute.
|
|
47
|
+
* @param {string} name - The attribute name.
|
|
48
|
+
* @returns {Promise<string|object>} - A promise that resolves to the processed attribute value.
|
|
49
|
+
*/
|
|
50
|
+
async function getAttributeAsync(element, name) {
|
|
51
|
+
if (!(element instanceof Element)) {
|
|
52
|
+
throw new Error("First argument must be an Element");
|
|
53
|
+
}
|
|
54
|
+
let value = originalGetAttribute.call(element, name);
|
|
55
|
+
return await processOperatorsAsync(element, value);
|
|
56
|
+
}
|
|
57
|
+
|
|
44
58
|
// Override the getAttribute method on Element prototype
|
|
45
59
|
Element.prototype.getAttribute = function (name) {
|
|
46
60
|
return getAttribute(this, name); // Use the custom getAttribute function
|
|
47
61
|
};
|
|
48
62
|
|
|
63
|
+
// Add getAttributeAsync to the Element prototype
|
|
64
|
+
Element.prototype.getAttributeAsync = function (name) {
|
|
65
|
+
return getAttributeAsync(this, name);
|
|
66
|
+
};
|
|
67
|
+
|
|
49
68
|
// Export the custom getAttribute function
|
|
50
|
-
export { getAttribute };
|
|
69
|
+
export { getAttribute, getAttributeAsync };
|
package/src/operators.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ObjectId, queryElements } from "@cocreate/utils";
|
|
1
|
+
import { ObjectId, queryElements, getValueFromObject } from "@cocreate/utils";
|
|
2
2
|
|
|
3
3
|
// Operators handled directly for simple, synchronous value retrieval
|
|
4
4
|
const customOperators = new Map(
|
|
@@ -36,6 +36,12 @@ const customOperators = new Map(
|
|
|
36
36
|
return path === "/" ? "" : path;
|
|
37
37
|
},
|
|
38
38
|
$param: (element, args) => args,
|
|
39
|
+
$getObjectValue: (element, args) => {
|
|
40
|
+
if (Array.isArray(args) && args.length >= 2) {
|
|
41
|
+
return getValueFromObject(args[0], args[1]);
|
|
42
|
+
}
|
|
43
|
+
return "";
|
|
44
|
+
},
|
|
39
45
|
$setValue: (element, args) => element.setValue(...args) || "",
|
|
40
46
|
$true: () => true,
|
|
41
47
|
$false: () => false,
|
|
@@ -73,6 +79,172 @@ const knownOperatorKeys = [
|
|
|
73
79
|
...propertyOperators
|
|
74
80
|
].sort((a, b) => b.length - a.length);
|
|
75
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Helper function to check if a string path starts with a known bare operator.
|
|
84
|
+
* This logic is necessary to separate '$value' from '[0].src' when no parentheses remain.
|
|
85
|
+
*/
|
|
86
|
+
const findBareOperatorInPath = (path, knownOperatorKeys) => {
|
|
87
|
+
const trimmedPath = path.trim();
|
|
88
|
+
for (const key of knownOperatorKeys) {
|
|
89
|
+
if (trimmedPath.startsWith(key)) {
|
|
90
|
+
const remaining = trimmedPath.substring(key.length);
|
|
91
|
+
// Edge Case Fix: Ensure the operator is followed by a space, bracket, dot, or end-of-string.
|
|
92
|
+
// This prevents "valueThing" from being incorrectly split into "$value" + "Thing".
|
|
93
|
+
if (remaining.length === 0 || /^\s|\[|\./.test(remaining)) {
|
|
94
|
+
return key;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Finds the innermost function call (operator + its balanced parentheses argument)
|
|
103
|
+
* in the expression. This is the core logic for iterative parsing.
|
|
104
|
+
* * @param {string} expression The full expression string.
|
|
105
|
+
* @returns {{operator: string, args: string, fullMatch: string} | null} Details of the innermost function call, or null.
|
|
106
|
+
*/
|
|
107
|
+
const findInnermostFunctionCall = (expression) => {
|
|
108
|
+
let balance = 0;
|
|
109
|
+
let deepestStart = -1;
|
|
110
|
+
let deepestEnd = -1;
|
|
111
|
+
let deepestBalance = -1;
|
|
112
|
+
let inSingleQuote = false;
|
|
113
|
+
let inDoubleQuote = false;
|
|
114
|
+
|
|
115
|
+
// First pass: Find the indices of the DEEPEST balanced parenthesis pair.
|
|
116
|
+
for (let i = 0; i < expression.length; i++) {
|
|
117
|
+
const char = expression[i];
|
|
118
|
+
|
|
119
|
+
if (char === '"' && !inSingleQuote) {
|
|
120
|
+
inDoubleQuote = !inDoubleQuote;
|
|
121
|
+
continue;
|
|
122
|
+
} else if (char === "'" && !inDoubleQuote) {
|
|
123
|
+
inSingleQuote = !inDoubleQuote;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (inSingleQuote || inDoubleQuote) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (char === '(') {
|
|
132
|
+
balance++;
|
|
133
|
+
// Track the index of the open parenthesis that belongs to the deepest balance level
|
|
134
|
+
if (balance > deepestBalance) {
|
|
135
|
+
deepestBalance = balance;
|
|
136
|
+
deepestStart = i;
|
|
137
|
+
deepestEnd = -1; // Reset end until match is found
|
|
138
|
+
}
|
|
139
|
+
} else if (char === ')') {
|
|
140
|
+
if (balance === deepestBalance) {
|
|
141
|
+
// This is the closing parenthesis that matches the deepest open parenthesis found
|
|
142
|
+
deepestEnd = i;
|
|
143
|
+
}
|
|
144
|
+
balance--;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// If we didn't find a balanced pair, or the match is invalid, exit.
|
|
149
|
+
if (deepestStart === -1 || deepestEnd === -1 || deepestEnd <= deepestStart) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Now we have the innermost argument content indices (deepestStart + 1 to deepestEnd - 1)
|
|
154
|
+
const rawArgs = expression.substring(deepestStart + 1, deepestEnd).trim();
|
|
155
|
+
|
|
156
|
+
// Step 2: Find the operator name backward from the deepestStart index.
|
|
157
|
+
let operatorStart = -1;
|
|
158
|
+
let nonWhitespaceFound = false;
|
|
159
|
+
|
|
160
|
+
for (let i = deepestStart - 1; i >= 0; i--) {
|
|
161
|
+
const char = expression[i];
|
|
162
|
+
|
|
163
|
+
// Skip trailing whitespace between operator and '('
|
|
164
|
+
if (!nonWhitespaceFound) {
|
|
165
|
+
if (/\s/.test(char)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
nonWhitespaceFound = true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Find the start of the operator name
|
|
172
|
+
// This regex captures letters, numbers, hyphens, and the required $
|
|
173
|
+
let isOperatorChar = /[\w\-\$]/.test(char);
|
|
174
|
+
|
|
175
|
+
if (!isOperatorChar) {
|
|
176
|
+
operatorStart = i + 1;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
operatorStart = i;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (operatorStart === -1) operatorStart = 0;
|
|
183
|
+
|
|
184
|
+
const operatorNameCandidate = expression.substring(operatorStart, deepestStart).trim();
|
|
185
|
+
|
|
186
|
+
// Step 3: Validate the operator name
|
|
187
|
+
if (knownOperatorKeys.includes(operatorNameCandidate)) {
|
|
188
|
+
// Construct the full match string: operatorNameCandidate(rawArgs)
|
|
189
|
+
const fullMatch = expression.substring(operatorStart, deepestEnd + 1);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
operator: operatorNameCandidate,
|
|
193
|
+
args: rawArgs,
|
|
194
|
+
fullMatch: fullMatch
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return null; // Operator name invalid or not found
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Main function to find the innermost operator.
|
|
203
|
+
* * Logic flow is updated to prioritize finding the innermost FUNCTION CALL,
|
|
204
|
+
* then fall back to finding a BARE OPERATOR if no function calls remain.
|
|
205
|
+
* * @param {string} expression The expression to parse.
|
|
206
|
+
* @returns {{operator: string, args: string, rawContent: string, fullMatch?: string} | {operator: null, args: string, rawContent: string}}
|
|
207
|
+
*/
|
|
208
|
+
const findInnermostOperator = (expression) => {
|
|
209
|
+
|
|
210
|
+
// --- 1. PRIORITY: Find Innermost FUNCTION CALL (Operator with Parentheses) ---
|
|
211
|
+
const functionCall = findInnermostFunctionCall(expression);
|
|
212
|
+
|
|
213
|
+
if (functionCall) {
|
|
214
|
+
// Return the full function expression details, including the full string section
|
|
215
|
+
return {
|
|
216
|
+
operator: functionCall.operator,
|
|
217
|
+
args: functionCall.args,
|
|
218
|
+
rawContent: functionCall.args, // The content inside the parentheses (arguments)
|
|
219
|
+
fullMatch: functionCall.fullMatch // The operator(args) string (the complete section to replace)
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// --- 2. FALLBACK: Find BARE OPERATOR (e.g., $value path) ---
|
|
224
|
+
|
|
225
|
+
// If no function calls are found, the entire expression is treated as the raw content
|
|
226
|
+
const rawContent = expression.trim();
|
|
227
|
+
|
|
228
|
+
// Now check the raw content to see if it starts with a bare operator ($value)
|
|
229
|
+
const innermostOperator = findBareOperatorInPath(rawContent, knownOperatorKeys);
|
|
230
|
+
|
|
231
|
+
if (innermostOperator) {
|
|
232
|
+
const operatorArgs = rawContent.substring(innermostOperator.length).trim();
|
|
233
|
+
return {
|
|
234
|
+
operator: innermostOperator,
|
|
235
|
+
args: operatorArgs,
|
|
236
|
+
rawContent: rawContent,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Fallback if no known operator is found
|
|
241
|
+
return {
|
|
242
|
+
operator: null,
|
|
243
|
+
args: rawContent,
|
|
244
|
+
rawContent: rawContent
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
|
|
76
248
|
function escapeRegexKey(key) {
|
|
77
249
|
if (key.startsWith("$")) {
|
|
78
250
|
return "\\" + key; // Escape the leading $
|
|
@@ -82,71 +254,140 @@ function escapeRegexKey(key) {
|
|
|
82
254
|
return key; // Should not happen with current keys, but fallback
|
|
83
255
|
}
|
|
84
256
|
|
|
85
|
-
const operatorPattern = knownOperatorKeys.map(escapeRegexKey).join("|");
|
|
86
|
-
|
|
87
|
-
// Regex to find potential operators and their arguments
|
|
88
|
-
// $1: Potential operator identifier (e.g., $user_id, $closestDiv)
|
|
89
|
-
// $2: Arguments within parentheses (optional)
|
|
90
|
-
const regex = new RegExp(`(${operatorPattern})(?:\\s*\\((.*?)\\))?`, "g");
|
|
91
|
-
|
|
92
257
|
/**
|
|
93
258
|
* Synchronously processes a string, finding and replacing operators recursively.
|
|
94
259
|
* Assumes ALL underlying operations (getValue, queryElements) are synchronous.
|
|
95
260
|
* @param {Element | null} element - Context element.
|
|
96
261
|
* @param {string} value - String containing operators.
|
|
97
262
|
* @param {string[]} [exclude=[]] - Operator prefixes to ignore.
|
|
98
|
-
* @returns {string} - Processed string.
|
|
263
|
+
* @returns {string | {value: string, params: Promise[]}} - Processed string or an object containing the partially processed string and unresolved Promises.
|
|
99
264
|
*/
|
|
100
|
-
function processOperators(
|
|
265
|
+
function processOperators(
|
|
266
|
+
element,
|
|
267
|
+
value,
|
|
268
|
+
exclude = [],
|
|
269
|
+
parent,
|
|
270
|
+
params = []
|
|
271
|
+
) {
|
|
101
272
|
// Early exit if no operators are possible or value is not a string
|
|
102
273
|
if (typeof value !== "string" || !value.includes("$")) {
|
|
103
274
|
return value;
|
|
104
275
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
276
|
+
|
|
277
|
+
let processedValue = value;
|
|
278
|
+
let hasPromise = false;
|
|
279
|
+
|
|
280
|
+
while (processedValue.includes("$")) {
|
|
281
|
+
|
|
282
|
+
// --- PROMISE TOKEN RESOLUTION ---
|
|
283
|
+
// If the processedValue starts with a resolved parameter token from the previous async step,
|
|
284
|
+
// substitute the token with its actual (now resolved) value from the params array.
|
|
285
|
+
const paramMatch = processedValue.match(/^\$\$PARAM_(\d+)\$\$/);
|
|
286
|
+
if (paramMatch && Array.isArray(params) && params.length > 0) {
|
|
287
|
+
const index = parseInt(paramMatch[1], 10);
|
|
288
|
+
if (index < params.length) {
|
|
289
|
+
const resolvedTokenValue = params[index];
|
|
290
|
+
processedValue = processedValue.replace(paramMatch[0], resolvedTokenValue);
|
|
291
|
+
// After replacement, we restart the loop to find the *next* innermost operator
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// --- END TOKEN RESOLUTION ---
|
|
296
|
+
|
|
297
|
+
const { operator, args, rawContent, fullMatch } = findInnermostOperator(processedValue);
|
|
298
|
+
|
|
299
|
+
if (!operator) {
|
|
300
|
+
break; // No more operators found
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (operator === "$param" && !args) {
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (operator && !exclude.includes(operator)) {
|
|
308
|
+
|
|
309
|
+
// --- Determine textToReplace ---
|
|
310
|
+
// The fullMatch property from findInnermostOperator ensures we correctly replace
|
|
311
|
+
// the whole expression (e.g., "$param(...)") or just the bare operator ($value path).
|
|
312
|
+
const textToReplace = fullMatch || rawContent;
|
|
313
|
+
// --- END textToReplace CALCULATION ---
|
|
314
|
+
|
|
315
|
+
let resolvedValue = resolveOperator(element, operator, args, parent, params);
|
|
316
|
+
|
|
317
|
+
if (resolvedValue instanceof Promise) {
|
|
318
|
+
const paramIndex = params.length; // Get the index *before* push
|
|
319
|
+
params.push(resolvedValue); // Store the Promise
|
|
320
|
+
|
|
321
|
+
// CRITICAL FIX: Replace the matched expression with a unique token, then break.
|
|
322
|
+
processedValue = processedValue.replace(textToReplace, `$$PARAM_${paramIndex}$$`);
|
|
323
|
+
hasPromise = true;
|
|
324
|
+
break; // Stop processing and yield
|
|
115
325
|
}
|
|
116
326
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
let resolvedValue = resolveOperator(
|
|
122
|
-
element,
|
|
123
|
-
operator,
|
|
124
|
-
args.replace(/^['"]|['"]$/g, "").trim(),
|
|
125
|
-
parent
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (operator === "$param") {
|
|
129
|
-
params.push(resolvedValue);
|
|
130
|
-
return "";
|
|
131
|
-
}
|
|
327
|
+
if (params.some((p) => p instanceof Promise)) {
|
|
328
|
+
hasPromise = true;
|
|
329
|
+
break; // A nested call found a promise, stop and yield
|
|
330
|
+
}
|
|
132
331
|
|
|
133
|
-
|
|
332
|
+
let replacement = "";
|
|
333
|
+
if (operator === "$param") {
|
|
334
|
+
params.push(resolvedValue);
|
|
134
335
|
} else {
|
|
135
|
-
|
|
136
|
-
// return the original matched string (no replacement).
|
|
137
|
-
return match;
|
|
336
|
+
replacement = resolvedValue ?? "";
|
|
138
337
|
}
|
|
338
|
+
|
|
339
|
+
// Manually replace the matched part of the string
|
|
340
|
+
processedValue = processedValue.replace(textToReplace, replacement);
|
|
341
|
+
|
|
342
|
+
} else {
|
|
343
|
+
// If operator is excluded, we need to advance past it to avoid infinite loop
|
|
344
|
+
break;
|
|
139
345
|
}
|
|
140
|
-
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (hasPromise) {
|
|
349
|
+
return { value: processedValue, params };
|
|
350
|
+
}
|
|
141
351
|
|
|
142
352
|
if (params.length) {
|
|
143
|
-
|
|
353
|
+
if (processedValue.trim() === "") {
|
|
354
|
+
return params;
|
|
355
|
+
}
|
|
144
356
|
}
|
|
145
357
|
|
|
146
358
|
return processedValue;
|
|
147
359
|
}
|
|
148
360
|
|
|
149
|
-
|
|
361
|
+
async function processOperatorsAsync(
|
|
362
|
+
element,
|
|
363
|
+
value,
|
|
364
|
+
exclude = [],
|
|
365
|
+
parent,
|
|
366
|
+
params = []
|
|
367
|
+
) {
|
|
368
|
+
let result = processOperators(element, value, exclude, parent, params);
|
|
369
|
+
|
|
370
|
+
while (typeof result === "object" && result.params) {
|
|
371
|
+
const resolvedParams = await Promise.all(result.params);
|
|
372
|
+
// Note: The second argument passed to processOperators here is the partially processed string (result.value)
|
|
373
|
+
// which now contains the PARAM tokens. The third argument is the array of resolved values (resolvedParams)
|
|
374
|
+
// which will be used to replace those tokens in the subsequent processOperators call.
|
|
375
|
+
result = processOperators(
|
|
376
|
+
element,
|
|
377
|
+
result.value,
|
|
378
|
+
exclude,
|
|
379
|
+
parent,
|
|
380
|
+
resolvedParams
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (result instanceof Promise) {
|
|
385
|
+
return await result;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
|
|
150
391
|
/**
|
|
151
392
|
* Synchronously determines and executes the action for processing a single operator token.
|
|
152
393
|
* @param {HTMLElement|null} element - The context element from which to derive values or execute methods.
|
|
@@ -155,18 +396,28 @@ function processOperators(element, value, exclude = [], parent) {
|
|
|
155
396
|
* @param {string} parent - Context in which the function is called, potentially affecting behavior or processing.
|
|
156
397
|
* @returns {string} The final resolved value after applying the operator to the given elements.
|
|
157
398
|
*/
|
|
158
|
-
function resolveOperator(element, operator, args, parent) {
|
|
399
|
+
function resolveOperator(element, operator, args, parent, params) {
|
|
400
|
+
// If a promise is already in the params, we must stop and wait for it to be resolved.
|
|
401
|
+
if (params.some((p) => p instanceof Promise)) {
|
|
402
|
+
return "";
|
|
403
|
+
}
|
|
404
|
+
|
|
159
405
|
// If args contain any operators (indicated by '$'), process them recursively
|
|
160
|
-
if (args && args.includes("$")) {
|
|
406
|
+
if (args && typeof args === "string" && args.includes("$")) {
|
|
161
407
|
// Reprocess args to resolve any nested operators
|
|
162
|
-
args = processOperators(element, args, "", operator);
|
|
408
|
+
args = processOperators(element, args, "", operator, params);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (params.some((p) => p instanceof Promise)) {
|
|
412
|
+
return operator;
|
|
163
413
|
}
|
|
164
414
|
|
|
165
415
|
// Initialize an array of elements to operate on, starting with the single element reference if provided
|
|
166
416
|
let targetElements = element ? [element] : [];
|
|
167
|
-
|
|
168
|
-
// If
|
|
169
|
-
|
|
417
|
+
|
|
418
|
+
// If the argument is a string and the operator is NOT a custom utility that expects raw data
|
|
419
|
+
// (like $param, $parse), we assume the argument is a selector.
|
|
420
|
+
if (args && typeof args === "string" && !customOperators.has(operator)) {
|
|
170
421
|
targetElements = queryElements({
|
|
171
422
|
element, // Use the context element as the base for querying
|
|
172
423
|
selector: args // Selector from args to find matching elements
|
|
@@ -182,7 +433,7 @@ function resolveOperator(element, operator, args, parent) {
|
|
|
182
433
|
// If the result is a string and still contains unresolved operators, process them further
|
|
183
434
|
if (value && typeof value === "string" && value.includes("$")) {
|
|
184
435
|
// Resolve any remaining operators within the value string
|
|
185
|
-
value = processOperators(element, value, parent);
|
|
436
|
+
value = processOperators(element, value, parent, params);
|
|
186
437
|
}
|
|
187
438
|
|
|
188
439
|
// Return the final processed value, fully resolved
|
|
@@ -216,8 +467,10 @@ function processValues(elements, operator, args, parent) {
|
|
|
216
467
|
if (typeof rawValue === "function") {
|
|
217
468
|
// If arguments are provided as an array
|
|
218
469
|
if (Array.isArray(args)) {
|
|
219
|
-
// If
|
|
220
|
-
if (args.
|
|
470
|
+
// If the custom operator is NOT $param and has arguments, something is wrong with the flow.
|
|
471
|
+
// However, if it's a generic method on an element (like $setValue), the args are passed via spread.
|
|
472
|
+
if (customOperators.has(operator) && operator !== "$setValue" && operator !== "$getObjectValue" && args.length) {
|
|
473
|
+
// For simple custom operators that don't take multiple args, return an empty string.
|
|
221
474
|
return "";
|
|
222
475
|
}
|
|
223
476
|
// Invoke the function using the element and spread array arguments
|
|
@@ -235,6 +488,12 @@ function processValues(elements, operator, args, parent) {
|
|
|
235
488
|
return rawValue;
|
|
236
489
|
}
|
|
237
490
|
} else {
|
|
491
|
+
if (
|
|
492
|
+
rawValue instanceof Promise ||
|
|
493
|
+
(typeof rawValue === "object" && rawValue !== null)
|
|
494
|
+
) {
|
|
495
|
+
return rawValue;
|
|
496
|
+
}
|
|
238
497
|
// Otherwise, append the stringified rawValue to the aggregated result, defaulting to an empty string if it's nullish
|
|
239
498
|
aggregatedString += String(rawValue ?? "");
|
|
240
499
|
}
|
|
@@ -267,4 +526,4 @@ function getSubdomain() {
|
|
|
267
526
|
return null;
|
|
268
527
|
}
|
|
269
528
|
|
|
270
|
-
export { processOperators };
|
|
529
|
+
export { processOperators, processOperatorsAsync };
|
package/src/setValue.js
CHANGED
|
@@ -87,7 +87,14 @@ const setValue = (el, value, dispatch) => {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
} else if (el.type === "radio") {
|
|
90
|
-
|
|
90
|
+
const wasChecked = el.checked;
|
|
91
|
+
if (el.value == value) {
|
|
92
|
+
el.checked = true;
|
|
93
|
+
} else {
|
|
94
|
+
el.checked = false;
|
|
95
|
+
}
|
|
96
|
+
// Only continue if checked state changed
|
|
97
|
+
if (el.checked === wasChecked) return;
|
|
91
98
|
} else if (el.type === "password") {
|
|
92
99
|
el.value = __decryptPassword(value);
|
|
93
100
|
} else if (el.tagName == "SELECT") {
|