@cocreate/element-prototype 1.24.0 → 1.25.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 +24 -0
- package/package.json +1 -1
- package/src/getAttribute.js +1 -1
- package/src/getValue.js +196 -34
- package/src/index.js +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
# [1.25.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.1...v1.25.0) (2024-08-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* init getAttribute before getValue and setValue ([e0cc56a](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/e0cc56a2a1dffc69d3916c7653d94269d429309c))
|
|
7
|
+
* removed element.value as already exist ([280b1c6](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/280b1c6b8798d0bd45f4f9665539bdf4b18a0ff6))
|
|
8
|
+
* use hasOwnProperty to avoid prototype ([b7736db](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b7736db4102d9b5f88c2ff9aa508a847a8a7b6eb))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* Add support for various regex operations in getValue function ([b87b1a2](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b87b1a2e82677cd3f6eb149712ffb68e95f0f2be))
|
|
14
|
+
* encode and decode value ([4550990](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/455099035eebc934fdfb28ac5660db719d82dcd6))
|
|
15
|
+
* support toLocaleString and locale atttribute ([8ebfbd6](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/8ebfbd6f61e7f1efdf661cbdaddfa0b0905c5aa0))
|
|
16
|
+
* supports regex parsing of value ([b2e1a81](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b2e1a817799dbfb7b541543eb24a00c5f1cc88b1))
|
|
17
|
+
|
|
18
|
+
## [1.24.1](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.24.0...v1.24.1) (2024-06-19)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* type file uses storage ([6b0a434](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/6b0a43406a26312c1d500dc2fd17a097c33f74db))
|
|
24
|
+
|
|
1
25
|
# [1.24.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.23.0...v1.24.0) (2024-06-12)
|
|
2
26
|
|
|
3
27
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/element-prototype",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.25.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
|
@@ -28,7 +28,7 @@ Element.prototype.getAttribute = function (name) {
|
|
|
28
28
|
'$session_id': 'session_id'
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
if (localKeys
|
|
31
|
+
if (localKeys.hasOwnProperty(value)) {
|
|
32
32
|
let newValue = localStorage.getItem(localKeys[value]);
|
|
33
33
|
|
|
34
34
|
if (!attributes.has(localKeys[value])) {
|
package/src/getValue.js
CHANGED
|
@@ -18,7 +18,7 @@ HTMLHeadingElement.prototype.getValue = function () {
|
|
|
18
18
|
// TODO: check if using a a switch case will provide better performance
|
|
19
19
|
const getValue = (element) => {
|
|
20
20
|
let value = element.value || element.getAttribute('value') || "";
|
|
21
|
-
if (element.hasAttribute('component') || element.hasAttribute('plugin')) {
|
|
21
|
+
if (element.hasAttribute('component') || element.hasAttribute('plugin') || element.type === 'file' || element.getAttribute('type') === 'file') {
|
|
22
22
|
value = storage.get(element)
|
|
23
23
|
storage.delete(element)
|
|
24
24
|
return value
|
|
@@ -105,6 +105,10 @@ const getValue = (element) => {
|
|
|
105
105
|
case 'toUnixTimestamp':
|
|
106
106
|
value = Math.floor(value.getTime() / 1000);
|
|
107
107
|
break;
|
|
108
|
+
case 'toLocaleString':
|
|
109
|
+
let locale = element.getAttribute('locale') || 'en-US'
|
|
110
|
+
value = value[valueType](locale);
|
|
111
|
+
break;
|
|
108
112
|
default:
|
|
109
113
|
if (typeof value[valueType] === 'function') {
|
|
110
114
|
value = value[valueType]();
|
|
@@ -130,22 +134,6 @@ const getValue = (element) => {
|
|
|
130
134
|
value = element.innerHTML;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
|
-
if (!Array.isArray(value)) {
|
|
134
|
-
if (prefix || suffix)
|
|
135
|
-
value = prefix + value + suffix;
|
|
136
|
-
|
|
137
|
-
if (valueType == 'array')
|
|
138
|
-
value = [value];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (value && (valueType == 'object' || valueType == 'json')) {
|
|
142
|
-
try {
|
|
143
|
-
value = JSON.parse(value)
|
|
144
|
-
} catch (error) {
|
|
145
|
-
value = value
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
137
|
if (valueType === 'boolean') {
|
|
150
138
|
if (!value || value === 'fasle')
|
|
151
139
|
return false
|
|
@@ -190,30 +178,122 @@ const getValue = (element) => {
|
|
|
190
178
|
}
|
|
191
179
|
}
|
|
192
180
|
|
|
193
|
-
|
|
194
|
-
let
|
|
181
|
+
// try {
|
|
182
|
+
// let replace = element.getAttribute('value-replace');
|
|
183
|
+
// let replaceAll = element.getAttribute('value-replaceall');
|
|
195
184
|
|
|
196
|
-
if (replace || replaceAll) {
|
|
197
|
-
|
|
198
|
-
|
|
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
|
+
// }
|
|
199
193
|
|
|
200
|
-
|
|
201
|
-
|
|
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
|
+
try {
|
|
209
|
+
let replace = element.getAttribute('value-replace');
|
|
210
|
+
let replaceAll = element.getAttribute('value-replaceall');
|
|
211
|
+
let test = element.getAttribute('value-test');
|
|
212
|
+
let match = element.getAttribute('value-match');
|
|
213
|
+
let split = element.getAttribute('value-split');
|
|
214
|
+
let lastIndex = element.getAttribute('value-lastindex');
|
|
215
|
+
let search = element.getAttribute('value-search');
|
|
216
|
+
let exec = element.getAttribute('value-exec');
|
|
217
|
+
|
|
218
|
+
if (replace || replaceAll || test || match || split || lastIndex || search || exec) {
|
|
219
|
+
let { regex, replacement } = regexParser(replace || replaceAll || test || match || split || lastIndex || search || exec);
|
|
220
|
+
|
|
221
|
+
if (regex) {
|
|
202
222
|
if (replace)
|
|
203
|
-
|
|
204
|
-
else
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
223
|
+
replace = regex;
|
|
224
|
+
else if (replaceAll)
|
|
225
|
+
replaceAll = regex;
|
|
226
|
+
else if (test)
|
|
227
|
+
test = regex;
|
|
228
|
+
else if (match)
|
|
229
|
+
match = regex;
|
|
230
|
+
else if (split)
|
|
231
|
+
split = regex;
|
|
232
|
+
else if (lastIndex)
|
|
233
|
+
lastIndex = regex;
|
|
234
|
+
else if (search)
|
|
235
|
+
search = regex;
|
|
236
|
+
else if (exec)
|
|
237
|
+
exec = regex;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
replacement = replacement || element.getAttribute('value-replacement') || "";
|
|
241
|
+
|
|
242
|
+
if (replacement !== undefined) {
|
|
243
|
+
if (replace) {
|
|
244
|
+
value = value.replace(replace, replacement);
|
|
245
|
+
} else if (replaceAll) {
|
|
246
|
+
value = value.replaceAll(replaceAll, replacement);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (test) {
|
|
251
|
+
value = regex.test(value);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (match) {
|
|
255
|
+
const matches = value.match(match);
|
|
256
|
+
if (matches) {
|
|
257
|
+
value = matches[1] || matches[0]; // prioritize capturing group match if available
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (split) {
|
|
262
|
+
value = value.split(split);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (lastIndex) {
|
|
266
|
+
regex.lastIndex = 0; // Ensure starting index is 0
|
|
267
|
+
regex.test(value);
|
|
268
|
+
value = regex.lastIndex;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (search) {
|
|
272
|
+
value = value.search(search);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (exec) {
|
|
276
|
+
const execResult = regex.exec(value);
|
|
277
|
+
if (execResult) {
|
|
278
|
+
value = execResult[1] || execResult[0]; // prioritize capturing group match if available
|
|
279
|
+
} else {
|
|
280
|
+
value = null;
|
|
281
|
+
}
|
|
208
282
|
}
|
|
209
|
-
} else {
|
|
210
|
-
if (replace)
|
|
211
|
-
value = value.replace(replace, replaceWith);
|
|
212
|
-
else
|
|
213
|
-
value = value.replaceAll(replaceAll, replaceWith);
|
|
214
283
|
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
console.error('getValue() error:', error, element);
|
|
215
286
|
}
|
|
216
287
|
|
|
288
|
+
// TODO: encode and decode needs a method to prevent multiple encode of an already encoded value
|
|
289
|
+
let encode = element.getAttribute('value-encode')
|
|
290
|
+
if (encode)
|
|
291
|
+
value = encodeValue(value, encode)
|
|
292
|
+
|
|
293
|
+
let decode = element.getAttribute('value-decode')
|
|
294
|
+
if (decode)
|
|
295
|
+
value = decodeValue(value, decode)
|
|
296
|
+
|
|
217
297
|
let lowercase = element.getAttribute('value-lowercase')
|
|
218
298
|
if (lowercase || lowercase === '')
|
|
219
299
|
value = value.toLowerCase()
|
|
@@ -221,8 +301,90 @@ const getValue = (element) => {
|
|
|
221
301
|
if (uppercase || uppercase === '')
|
|
222
302
|
value = value.toUpperCase()
|
|
223
303
|
|
|
304
|
+
if (!Array.isArray(value)) {
|
|
305
|
+
if (prefix || suffix)
|
|
306
|
+
value = prefix + value + suffix;
|
|
307
|
+
|
|
308
|
+
if (valueType == 'array')
|
|
309
|
+
value = [value];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (value && (valueType == 'object' || valueType == 'json')) {
|
|
313
|
+
try {
|
|
314
|
+
value = JSON.parse(value)
|
|
315
|
+
} catch (error) {
|
|
316
|
+
value = value
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
224
320
|
return value;
|
|
225
321
|
|
|
226
322
|
};
|
|
227
323
|
|
|
324
|
+
function regexParser(string) {
|
|
325
|
+
let regex, replacement;
|
|
326
|
+
let regexMatch = string.match(/\/(.+)\/([gimuy]*)/);
|
|
327
|
+
if (regexMatch) {
|
|
328
|
+
regex = new RegExp(regexMatch[1], regexMatch[2]);
|
|
329
|
+
const splitReplace = string.split(', ');
|
|
330
|
+
replacement = splitReplace.length > 1 ? splitReplace[1].slice(1, -1) : "";
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return { regex, replacement }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function encodeValue(value, encodingType) {
|
|
337
|
+
switch (encodingType.toLowerCase()) {
|
|
338
|
+
case 'url':
|
|
339
|
+
case 'uri':
|
|
340
|
+
return encodeURI(value.replace(/ /g, "%20"));
|
|
341
|
+
case 'uri-component':
|
|
342
|
+
return encodeURIComponent(value.replace(/ /g, "%20"));
|
|
343
|
+
case 'base64':
|
|
344
|
+
case 'atob':
|
|
345
|
+
const encoder = new TextEncoder();
|
|
346
|
+
const uint8Array = encoder.encode(value);
|
|
347
|
+
return btoa(String.fromCharCode(...uint8Array));
|
|
348
|
+
case 'html-entities':
|
|
349
|
+
return value.replace(/[\u00A0-\u9999<>\&]/g, (i) => {
|
|
350
|
+
return `&#${i.charCodeAt(0)};`;
|
|
351
|
+
});
|
|
352
|
+
case 'json':
|
|
353
|
+
return JSON.stringify(value);
|
|
354
|
+
default:
|
|
355
|
+
throw new Error(`Unsupported encoding type: ${encodingType}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function decodeValue(value, decodingType) {
|
|
360
|
+
switch (decodingType.toLowerCase()) {
|
|
361
|
+
case 'url':
|
|
362
|
+
case 'uri':
|
|
363
|
+
return decodeURI(value);
|
|
364
|
+
case 'uri-component':
|
|
365
|
+
return decodeURIComponent(value);
|
|
366
|
+
case 'base64':
|
|
367
|
+
case 'btoa': // New case for Base64 decoding (alias for 'base64')
|
|
368
|
+
try {
|
|
369
|
+
const decodedArray = Uint8Array.from(atob(value), (c) => c.charCodeAt(0));
|
|
370
|
+
const decoder = new TextDecoder();
|
|
371
|
+
return decoder.decode(decodedArray);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
throw new Error(`Invalid Base64 string: ${error.message}`);
|
|
374
|
+
}
|
|
375
|
+
case 'html-entities':
|
|
376
|
+
const tempElement = document.createElement('div');
|
|
377
|
+
tempElement.innerHTML = value;
|
|
378
|
+
return tempElement.textContent;
|
|
379
|
+
case 'json':
|
|
380
|
+
try {
|
|
381
|
+
return JSON.parse(value);
|
|
382
|
+
} catch (error) {
|
|
383
|
+
throw new Error(`Invalid JSON string: ${error.message}`);
|
|
384
|
+
}
|
|
385
|
+
default:
|
|
386
|
+
throw new Error(`Unsupported decoding type: ${decodingType}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
228
390
|
export { getValue, storage };
|
package/src/index.js
CHANGED
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
// you must obtain a commercial license from CoCreate LLC.
|
|
21
21
|
// For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
|
|
22
22
|
|
|
23
|
+
import { getAttribute } from './getAttribute';
|
|
23
24
|
import { setValue } from './setValue';
|
|
24
25
|
import { getValue } from './getValue';
|
|
25
|
-
import { getAttribute } from './getAttribute';
|
|
26
26
|
|
|
27
|
-
export default { getValue, setValue
|
|
27
|
+
export default { getAttribute, getValue, setValue }
|