@cocreate/element-prototype 1.25.0 → 1.27.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 +29 -0
- package/package.json +1 -1
- package/prettier.config.js +16 -0
- package/src/getValue.js +131 -103
- package/src/setValue.js +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
# [1.27.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.26.0...v1.27.0) (2024-11-04)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* pretier.config.js and file formating ([1428fd9](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/1428fd96719f9411d97af5f067a4f95e99a94eed))
|
|
7
|
+
* removed exec returning null ([240543b](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/240543b19eafe8cd2714ee7f81f9edf0c20774bf))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* add prettier.config.js and format files ([0acda3c](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/0acda3c61bd6f832a3544d202a7e59a87ee464d3))
|
|
13
|
+
* loop through regex improved value handling ([3ac5f8d](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/3ac5f8dfc978cfc25322b5ef927b8df995c0ab50))
|
|
14
|
+
* value-dispatch attribute to dispatch event even if value is null or undefined ([7f2ca4b](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/7f2ca4b18ec668f77895b3d369b379d4420aa046))
|
|
15
|
+
|
|
16
|
+
# [1.26.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.25.0...v1.26.0) (2024-09-21)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* removed commented code ([67b9cb4](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/67b9cb43a399b0c856ac8c285b4aa13d3ea93859))
|
|
22
|
+
* valueType default empty string to support startsWith ([4515bc2](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/4515bc2b6a1466eca1d210692002977fa6113540))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* if value-type object and parse fails snitizze to remove any string outside of object and attempt parse again ([9344106](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/93441066dc255bb5becc85594bc5c3d11c1766d5))
|
|
28
|
+
* value-type array.$keys array.$value and array.$entries will return an array from object ([25df40d](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/25df40d1adbbff43f06029b9a22e29f9ec4371c4))
|
|
29
|
+
|
|
1
30
|
# [1.25.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.1...v1.25.0) (2024-08-24)
|
|
2
31
|
|
|
3
32
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/element-prototype",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.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/getValue.js
CHANGED
|
@@ -26,7 +26,7 @@ const getValue = (element) => {
|
|
|
26
26
|
|
|
27
27
|
let prefix = element.getAttribute('value-prefix') || "";
|
|
28
28
|
let suffix = element.getAttribute('value-suffix') || "";
|
|
29
|
-
let valueType = element.getAttribute('value-type');
|
|
29
|
+
let valueType = element.getAttribute('value-type') || "";
|
|
30
30
|
|
|
31
31
|
if (element.type === "checkbox") {
|
|
32
32
|
let inputs = [element]
|
|
@@ -128,10 +128,26 @@ const getValue = (element) => {
|
|
|
128
128
|
value = element.srcdoc;
|
|
129
129
|
} else if (element.hasAttribute('value')) {
|
|
130
130
|
value = element.getAttribute('value');
|
|
131
|
-
} else if (valueType === 'text') {
|
|
132
|
-
value = element.innerText;
|
|
133
131
|
} else {
|
|
134
|
-
|
|
132
|
+
let targetElement = element;
|
|
133
|
+
|
|
134
|
+
// If value-exclude-selector exists, clone the element and remove the specified selectors
|
|
135
|
+
const excludeSelector = element.getAttribute('value-remove-selector');
|
|
136
|
+
if (excludeSelector) {
|
|
137
|
+
targetElement = element.cloneNode(true);
|
|
138
|
+
|
|
139
|
+
// Remove matching elements from the cloned element
|
|
140
|
+
targetElement.querySelectorAll(excludeSelector).forEach(el => el.remove());
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Determine whether to use outerHTML, innerHTML, or innerText based on valueType
|
|
144
|
+
if (valueType === 'text') {
|
|
145
|
+
value = targetElement.innerText;
|
|
146
|
+
} else if (valueType === 'outerHTML') {
|
|
147
|
+
value = targetElement.outerHTML;
|
|
148
|
+
} else {
|
|
149
|
+
value = targetElement.innerHTML;
|
|
150
|
+
}
|
|
135
151
|
}
|
|
136
152
|
|
|
137
153
|
if (valueType === 'boolean') {
|
|
@@ -178,109 +194,88 @@ const getValue = (element) => {
|
|
|
178
194
|
}
|
|
179
195
|
}
|
|
180
196
|
|
|
181
|
-
// try {
|
|
182
|
-
// let replace = element.getAttribute('value-replace');
|
|
183
|
-
// let replaceAll = element.getAttribute('value-replaceall');
|
|
184
|
-
|
|
185
|
-
// if (replace || replaceAll) {
|
|
186
|
-
// let { regex, replacement } = regexParser(replace || replaceAll)
|
|
187
|
-
// if (regex) {
|
|
188
|
-
// if (replace)
|
|
189
|
-
// replace = regex
|
|
190
|
-
// else if (replaceAll)
|
|
191
|
-
// replaceAll = regex
|
|
192
|
-
// }
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
// replacement = replacement || element.getAttribute('value-replacement') || "";
|
|
196
|
-
|
|
197
|
-
// if (replacement !== undefined) {
|
|
198
|
-
// if (replace)
|
|
199
|
-
// value = value.replace(replace, replacement);
|
|
200
|
-
// else
|
|
201
|
-
// value = value.replaceAll(replaceAll, replacement);
|
|
202
|
-
// }
|
|
203
|
-
// }
|
|
204
|
-
// } catch (error) {
|
|
205
|
-
// console.error('getValue() replace error:', error, element);
|
|
206
|
-
// }
|
|
207
|
-
|
|
208
197
|
try {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
198
|
+
|
|
199
|
+
const attributes = element.attributes; // Get all attributes of the element
|
|
200
|
+
const regexAttribute = [
|
|
201
|
+
'value-replace',
|
|
202
|
+
'value-replaceall',
|
|
203
|
+
'value-test',
|
|
204
|
+
'value-match',
|
|
205
|
+
'value-split',
|
|
206
|
+
'value-lastindex',
|
|
207
|
+
'value-search',
|
|
208
|
+
'value-exec'
|
|
209
|
+
];
|
|
210
|
+
// Process each attribute in order
|
|
211
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
212
|
+
if (value === null || value === undefined)
|
|
213
|
+
break
|
|
214
|
+
|
|
215
|
+
if (!regexAttribute.includes(attributes[i].name))
|
|
216
|
+
continue
|
|
217
|
+
|
|
218
|
+
let regexAttributeValue = attributes[i].value
|
|
219
|
+
|
|
220
|
+
if (!regexAttributeValue)
|
|
221
|
+
continue
|
|
222
|
+
|
|
223
|
+
let { regex, replacement } = regexParser(regexAttributeValue);
|
|
224
|
+
|
|
225
|
+
if (regex)
|
|
226
|
+
regexAttributeValue = regex
|
|
239
227
|
|
|
240
228
|
replacement = replacement || element.getAttribute('value-replacement') || "";
|
|
241
229
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
value = value.replace(
|
|
245
|
-
|
|
246
|
-
value = value.replaceAll(replaceAll, replacement);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
230
|
+
switch (attributes[i].name) {
|
|
231
|
+
case 'value-replace':
|
|
232
|
+
value = value.replace(regexAttributeValue, replacement);
|
|
233
|
+
break;
|
|
249
234
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
235
|
+
case 'value-replaceall':
|
|
236
|
+
value = value.replaceAll(regexAttributeValue, replacement);
|
|
237
|
+
break;
|
|
253
238
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
value = matches[1] || matches[0]; // prioritize capturing group match if available
|
|
258
|
-
}
|
|
259
|
-
}
|
|
239
|
+
case 'value-test':
|
|
240
|
+
value = regex.test(value);
|
|
241
|
+
break;
|
|
260
242
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
243
|
+
case 'value-match':
|
|
244
|
+
const matches = value.match(regexAttributeValue);
|
|
245
|
+
if (matches) {
|
|
246
|
+
value = matches[1] || matches[0]; // Prioritize capturing group if available
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
264
249
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
value = regex.lastIndex;
|
|
269
|
-
}
|
|
250
|
+
case 'value-split':
|
|
251
|
+
value = value.split(regexAttributeValue);
|
|
252
|
+
break;
|
|
270
253
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
254
|
+
case 'value-lastindex':
|
|
255
|
+
regex.lastIndex = 0; // Ensure starting index is 0
|
|
256
|
+
regex.test(value);
|
|
257
|
+
value = regex.lastIndex;
|
|
258
|
+
break;
|
|
274
259
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
260
|
+
case 'value-search':
|
|
261
|
+
value = value.search(regexAttributeValue);
|
|
262
|
+
break;
|
|
263
|
+
|
|
264
|
+
case 'value-exec':
|
|
265
|
+
const execResult = regex.exec(value);
|
|
266
|
+
if (execResult) {
|
|
267
|
+
value = execResult[1] || execResult[2] || execResult[0]; // Prioritize capturing group if available
|
|
268
|
+
} else {
|
|
269
|
+
// value = null;
|
|
270
|
+
}
|
|
271
|
+
break;
|
|
272
|
+
|
|
273
|
+
default:
|
|
274
|
+
// Ignore other attributes
|
|
275
|
+
break;
|
|
282
276
|
}
|
|
283
277
|
}
|
|
278
|
+
|
|
284
279
|
} catch (error) {
|
|
285
280
|
console.error('getValue() error:', error, element);
|
|
286
281
|
}
|
|
@@ -301,19 +296,52 @@ const getValue = (element) => {
|
|
|
301
296
|
if (uppercase || uppercase === '')
|
|
302
297
|
value = value.toUpperCase()
|
|
303
298
|
|
|
304
|
-
|
|
305
|
-
|
|
299
|
+
// Apply prefix and suffix first, before JSON parsing
|
|
300
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
301
|
+
if (prefix || suffix) {
|
|
306
302
|
value = prefix + value + suffix;
|
|
307
|
-
|
|
308
|
-
if (valueType == 'array')
|
|
309
|
-
value = [value];
|
|
303
|
+
}
|
|
310
304
|
}
|
|
311
305
|
|
|
312
|
-
|
|
306
|
+
// Handle JSON parsing for objects, arrays, or when valueType starts with 'array'
|
|
307
|
+
if (value && (valueType === 'object' || valueType === 'json' || valueType.startsWith('array'))) {
|
|
313
308
|
try {
|
|
314
|
-
value = JSON.parse(value)
|
|
309
|
+
value = JSON.parse(value);
|
|
315
310
|
} catch (error) {
|
|
316
|
-
|
|
311
|
+
const jsonRegex = /(\{[\s\S]*\}|\[[\s\S]*\])/;
|
|
312
|
+
const match = value.match(jsonRegex);
|
|
313
|
+
|
|
314
|
+
if (match) {
|
|
315
|
+
try {
|
|
316
|
+
value = JSON.parse(match[0]);
|
|
317
|
+
} catch (e) {
|
|
318
|
+
console.error('Failed to parse JSON after regex extraction:', e);
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
console.error('No valid JSON structure found in the string.');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Now handle array-specific logic if valueType starts with 'array'
|
|
327
|
+
if (valueType.startsWith('array')) {
|
|
328
|
+
if (!Array.isArray(value)) {
|
|
329
|
+
// If the parsed value is an object, apply array conversion based on operators
|
|
330
|
+
if (typeof value === 'object') {
|
|
331
|
+
if (valueType === 'array.$keys') {
|
|
332
|
+
value = Object.keys(value);
|
|
333
|
+
} else if (valueType === 'array.$values') {
|
|
334
|
+
value = Object.values(value);
|
|
335
|
+
} else if (valueType === 'array.$entries') {
|
|
336
|
+
value = Object.entries(value);
|
|
337
|
+
} else {
|
|
338
|
+
// Default behavior: wrap the object in an array
|
|
339
|
+
value = [value];
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
// If it's not an object (i.e., a primitive), wrap the value in an array
|
|
343
|
+
value = [value];
|
|
344
|
+
}
|
|
317
345
|
}
|
|
318
346
|
}
|
|
319
347
|
|
package/src/setValue.js
CHANGED
|
@@ -14,7 +14,8 @@ HTMLHeadingElement.prototype.setValue = function (value, dispatch) {
|
|
|
14
14
|
|
|
15
15
|
// TODO: check if using a a switch case will provide better performance
|
|
16
16
|
const setValue = (el, value, dispatch) => {
|
|
17
|
-
|
|
17
|
+
let valueDispatch = el.getAttribute('value-dispatch')
|
|
18
|
+
if ((valueDispatch || valueDispatch === '') && (value === '$false' || value === undefined || value === null)) {
|
|
18
19
|
return dispatchEvents(el, dispatch)
|
|
19
20
|
};
|
|
20
21
|
|
|
@@ -72,7 +73,7 @@ const setValue = (el, value, dispatch) => {
|
|
|
72
73
|
} else if (el.tagName == "SELECT" && el.hasAttribute('multiple') && Array.isArray(value)) {
|
|
73
74
|
let options = el.options;
|
|
74
75
|
for (let i = 0; i < options.length; i++) {
|
|
75
|
-
if (value.includes(options[i].value)) {
|
|
76
|
+
if (value.includes && value.includes(options[i].value)) {
|
|
76
77
|
options[i].selected = "selected";
|
|
77
78
|
} else {
|
|
78
79
|
options[i].selected = "";
|