@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 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.26.0",
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",
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ tabWidth: 4,
3
+ semi: true,
4
+ trailingComma: "none",
5
+ bracketSameLine: true,
6
+ useTabs: true,
7
+ overrides: [
8
+ {
9
+ files: ["*.json", "*.yml", "*.yaml"],
10
+ options: {
11
+ tabWidth: 2,
12
+ useTabs: false
13
+ },
14
+ }
15
+ ],
16
+ };
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
- value = element.innerHTML;
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
- let replace = element.getAttribute('value-replace');
183
- let replaceAll = element.getAttribute('value-replaceall');
184
- let test = element.getAttribute('value-test');
185
- let match = element.getAttribute('value-match');
186
- let split = element.getAttribute('value-split');
187
- let lastIndex = element.getAttribute('value-lastindex');
188
- let search = element.getAttribute('value-search');
189
- let exec = element.getAttribute('value-exec');
190
-
191
- if (replace || replaceAll || test || match || split || lastIndex || search || exec) {
192
- let { regex, replacement } = regexParser(replace || replaceAll || test || match || split || lastIndex || search || exec);
193
-
194
- if (regex) {
195
- if (replace)
196
- replace = regex;
197
- else if (replaceAll)
198
- replaceAll = regex;
199
- else if (test)
200
- test = regex;
201
- else if (match)
202
- match = regex;
203
- else if (split)
204
- split = regex;
205
- else if (lastIndex)
206
- lastIndex = regex;
207
- else if (search)
208
- search = regex;
209
- else if (exec)
210
- exec = regex;
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
- if (replacement !== undefined) {
216
- if (replace) {
217
- value = value.replace(replace, replacement);
218
- } else if (replaceAll) {
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
- if (test) {
224
- value = regex.test(value);
225
- }
235
+ case 'value-replaceall':
236
+ value = value.replaceAll(regexAttributeValue, replacement);
237
+ break;
226
238
 
227
- if (match) {
228
- const matches = value.match(match);
229
- if (matches) {
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
- if (split) {
235
- value = value.split(split);
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
- if (lastIndex) {
239
- regex.lastIndex = 0; // Ensure starting index is 0
240
- regex.test(value);
241
- value = regex.lastIndex;
242
- }
250
+ case 'value-split':
251
+ value = value.split(regexAttributeValue);
252
+ break;
243
253
 
244
- if (search) {
245
- value = value.search(search);
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
- if (exec) {
249
- const execResult = regex.exec(value);
250
- if (execResult) {
251
- value = execResult[1] || execResult[0]; // prioritize capturing group match if available
252
- } else {
253
- value = null;
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
- if (value === '$false') {
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 = "";