Fixes some DOM text reinterpretation issues in JS. (#1722)

* Fixed "DOM text reinterpreted as HTML error"
This commit is contained in:
Christoph Schlaepfer authored and GitHub committed 2023-10-13 14:48:35 +02:00
1 parent 63a71305ec
commit d7264541bd
2 files changed
+60 -16

No files matched your search

@@ -329,27 +329,44 @@ globalThis.FOSSBilling = {
const container = document.querySelector('.toast-container'); // Get the existing container or create if not present
const element = document.createElement('div');
container.appendChild(element); // Append the toast container to the container
container.appendChild(element);
element.classList.add('toast', 'show'); // Add 'show' class to display the toast immediately
element.setAttribute('role', 'alert');
element.setAttribute('aria-live', 'assertive');
element.setAttribute('aria-atomic', 'true');
element.innerHTML = `
<div class="toast-header">
<span class="p-2 border border-light bg-${color} rounded-circle me-2"></span>
<strong class="me-auto">System message</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">${message}</div>
`;
// Create header div and its children elements
const headerDiv = document.createElement('div');
headerDiv.className = 'toast-header';
const spanEl = document.createElement('span');
spanEl.className = `p-2 border border-light bg-${color} rounded-circle me-2`;
headerDiv.appendChild(spanEl);
const strongEl = document.createElement('strong');
strongEl.className = 'me-auto';
strongEl.textContent = 'System message';
headerDiv.appendChild(strongEl);
const closeButton = document.createElement('button');
closeButton.type = 'button';
closeButton.className = 'btn-close';
closeButton.setAttribute('data-bs-dismiss', 'toast');
closeButton.setAttribute('aria-label', 'Close');
headerDiv.appendChild(closeButton);
element.appendChild(headerDiv);
// Create body div and set its text content
const bodyDiv = document.createElement('div');
bodyDiv.className = 'toast-body';
bodyDiv.textContent = message; // Safely set the message content
element.appendChild(bodyDiv);
element.addEventListener('hidden.bs.toast', () => {
container.removeChild(element); // Remove the toast element from the container when it's hidden
});
container.appendChild(element); // Append the toast to the container
// Create a new Bootstrap toast instance and show it
const toast = new bootstrap.Toast(element);
toast.show();
@@ -358,6 +375,7 @@ globalThis.FOSSBilling = {
$(function () {
//===== Global ajax methods =====//
@@ -3,18 +3,44 @@ document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('#theme-settings fieldset').forEach((el, index) => {
let show = '', collapsed = '';
index === 0 ? show = 'show' : collapsed = 'collapsed';
let heading = el.querySelector('legend').innerText;
let accordionBtn = `<button class="accordion-button ${collapsed}" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-body-${index}" aria-expanded="true" aria-controls="collapse-${index}">${heading}</button>`;
let accordionContent = `<div id="collapse-body-${index}" class="accordion-collapse collapse ${show}"><div class="accordion-body pt-0">${el.innerHTML}</div></div>`;
let heading = el.querySelector('legend').textContent; // use textContent
// Create accordion-button
let accordionBtn = document.createElement('button');
accordionBtn.className = `accordion-button ${collapsed}`;
accordionBtn.type = 'button';
accordionBtn.setAttribute('data-bs-toggle', 'collapse');
accordionBtn.setAttribute('data-bs-target', `#collapse-body-${index}`);
accordionBtn.setAttribute('aria-expanded', 'true');
accordionBtn.setAttribute('aria-controls', `collapse-${index}`);
accordionBtn.textContent = heading; // set the text content
// Create accordion-collapse and its child accordion-body
let accordionContent = document.createElement('div');
accordionContent.id = `collapse-body-${index}`;
accordionContent.className = `accordion-collapse collapse ${show}`;
let accordionBody = document.createElement('div');
accordionBody.className = 'accordion-body pt-0';
accordionBody.innerHTML = el.innerHTML; // This is okay since el.innerHTML is from the DOM already
accordionContent.appendChild(accordionBody);
// Create wrap button with accordion-header class
let wrapBtn = document.createElement('h2');
wrapBtn.classList.add('accordion-header');
wrapBtn.innerHTML = accordionBtn;
wrapBtn.appendChild(accordionBtn); // append the button
// Create accordion-item and combine all parts
let accordionItem = document.createElement('div');
accordionItem.classList.add('accordion-item');
accordionItem.innerHTML = wrapBtn.outerHTML + accordionContent;
accordionItem.appendChild(wrapBtn);
accordionItem.appendChild(accordionContent);
// Replace the original element with the new structure
el.after(accordionItem);
el.remove();
});
document.querySelectorAll('#theme-settings table').forEach((el) => {
el.classList.add('w-100');