@cocreate/element-prototype 1.26.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 +15 -0
- package/package.json +1 -1
- package/prettier.config.js +16 -0
- package/src/getValue.js +89 -67
- package/src/setValue.js +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
# [1.26.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.25.0...v1.26.0) (2024-09-21)
|
|
2
17
|
|
|
3
18
|
|
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
|
@@ -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') {
|
|
@@ -179,81 +195,87 @@ const getValue = (element) => {
|
|
|
179
195
|
}
|
|
180
196
|
|
|
181
197
|
try {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
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
|
|
212
227
|
|
|
213
228
|
replacement = replacement || element.getAttribute('value-replacement') || "";
|
|
214
229
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
value = value.replace(
|
|
218
|
-
|
|
219
|
-
value = value.replaceAll(replaceAll, replacement);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
230
|
+
switch (attributes[i].name) {
|
|
231
|
+
case 'value-replace':
|
|
232
|
+
value = value.replace(regexAttributeValue, replacement);
|
|
233
|
+
break;
|
|
222
234
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
235
|
+
case 'value-replaceall':
|
|
236
|
+
value = value.replaceAll(regexAttributeValue, replacement);
|
|
237
|
+
break;
|
|
226
238
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
value = matches[1] || matches[0]; // prioritize capturing group match if available
|
|
231
|
-
}
|
|
232
|
-
}
|
|
239
|
+
case 'value-test':
|
|
240
|
+
value = regex.test(value);
|
|
241
|
+
break;
|
|
233
242
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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;
|
|
237
249
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
value = regex.lastIndex;
|
|
242
|
-
}
|
|
250
|
+
case 'value-split':
|
|
251
|
+
value = value.split(regexAttributeValue);
|
|
252
|
+
break;
|
|
243
253
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
254
|
+
case 'value-lastindex':
|
|
255
|
+
regex.lastIndex = 0; // Ensure starting index is 0
|
|
256
|
+
regex.test(value);
|
|
257
|
+
value = regex.lastIndex;
|
|
258
|
+
break;
|
|
247
259
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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;
|
|
255
276
|
}
|
|
256
277
|
}
|
|
278
|
+
|
|
257
279
|
} catch (error) {
|
|
258
280
|
console.error('getValue() error:', error, element);
|
|
259
281
|
}
|
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 = "";
|