Skip to content

Commit 3ac5f8d

Browse files
committed
feat: loop through regex improved value handling
1 parent 7f2ca4b commit 3ac5f8d

File tree

1 file changed

+89
-67
lines changed

1 file changed

+89
-67
lines changed

src/getValue.js

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,26 @@ const getValue = (element) => {
128128
value = element.srcdoc;
129129
} else if (element.hasAttribute('value')) {
130130
value = element.getAttribute('value');
131-
} else if (valueType === 'text') {
132-
value = element.innerText;
133131
} 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+
}
135151
}
136152

137153
if (valueType === 'boolean') {
@@ -179,81 +195,87 @@ const getValue = (element) => {
179195
}
180196

181197
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
212227

213228
replacement = replacement || element.getAttribute('value-replacement') || "";
214229

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;
222234

223-
if (test) {
224-
value = regex.test(value);
225-
}
235+
case 'value-replaceall':
236+
value = value.replaceAll(regexAttributeValue, replacement);
237+
break;
226238

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;
233242

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;
237249

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;
243253

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;
247259

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;
255276
}
256277
}
278+
257279
} catch (error) {
258280
console.error('getValue() error:', error, element);
259281
}

0 commit comments

Comments
 (0)