Skip to content

Commit 56fba82

Browse files
author
Antoine Greuzard
committed
Refactored option element creation in form.js
Replaced innerHTML assignments in createOptionElement function with document.createElement calls. This change enhances security by avoiding potential cross-site scripting (XSS) vulnerabilities, since it mitigates the danger of injecting malicious content through innerHTML. It also improves code readability and maintainability.
1 parent d0632f3 commit 56fba82

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

back-end/post-type/form/form.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,40 @@ function createInputWithLabel(parent, labelText, name, type, className) {
342342
function createOptionElement() {
343343
let optionElement = document.createElement('p');
344344
optionElement.className = "option-field";
345-
optionElement.innerHTML = `<label>${influactiveFormsTranslations.optionLabelLabelText} ` +
346-
`<input type="text" class="option-label" name="influactive_form_fields[][options][][label]">` +
347-
`</label> ` +
348-
`<label>${influactiveFormsTranslations.optionValueLabelText} ` +
349-
`<input type="text" class="option-value" name="influactive_form_fields[][options][][value]">` +
350-
`</label> ` +
351-
`<a href="#" class="remove_option">${influactiveFormsTranslations.removeOptionText}</a>`;
345+
346+
// Create the option label
347+
let optionLabel = document.createElement('label');
348+
optionLabel.textContent = influactiveFormsTranslations.optionLabelLabelText + " ";
349+
let optionLabelInput = document.createElement('input');
350+
optionLabelInput.type = "text";
351+
optionLabelInput.className = "option-label";
352+
optionLabelInput.name = "influactive_form_fields[][options][][label]";
353+
optionLabel.appendChild(optionLabelInput);
354+
optionElement.appendChild(optionLabel);
355+
356+
// Add space
357+
optionElement.appendChild(document.createTextNode(" "));
358+
359+
// Create the value label
360+
let valueLabel = document.createElement('label');
361+
valueLabel.textContent = influactiveFormsTranslations.optionValueLabelText + " ";
362+
let valueLabelInput = document.createElement('input');
363+
valueLabelInput.type = "text";
364+
valueLabelInput.className = "option-value";
365+
valueLabelInput.name = "influactive_form_fields[][options][][value]";
366+
valueLabel.appendChild(valueLabelInput);
367+
optionElement.appendChild(valueLabel);
368+
369+
// Add space
370+
optionElement.appendChild(document.createTextNode(" "));
371+
372+
// Create remove option link
373+
let removeOptionLink = document.createElement('a');
374+
removeOptionLink.href = "#";
375+
removeOptionLink.className = "remove_option";
376+
removeOptionLink.textContent = influactiveFormsTranslations.removeOptionText;
377+
optionElement.appendChild(removeOptionLink);
378+
352379
return optionElement;
353380
}
354381

0 commit comments

Comments
 (0)