Files
BetterSEQTA-Plus/src/seqta/utils/stringToHTML.ts
T
google-labs-jules[bot] 074e73b0fd Add JSDoc comments to various utility functions and core files.
This change adds JSDoc-style comments to several functions and classes across the codebase to improve readability and maintainability.

Comments were added to:
- `src/SEQTA.ts`: Explained the `init()` function.
- `src/seqta/utils/waitForElm.ts`: Detailed the `waitForElm()` function, its parameters, and behavior.
- `src/seqta/utils/stringToHTML.ts`: Clarified the `stringToHTML()` function, including its sanitization and styling features.
- `src/seqta/utils/delay.ts`: Added a brief explanation for the `delay()` utility.
- `src/seqta/utils/mutex.ts`: Documented the `Mutex` class and its `acquire` method (renamed from `lock`), explaining its asynchronous locking mechanism and the role of the returned unlock function.
2025-05-29 12:19:57 +00:00

33 lines
1.2 KiB
TypeScript

import DOMPurify from "dompurify";
/**
* Converts an HTML string into a DOM element, with sanitization and optional styling.
*
* This function first sanitizes the input HTML string using DOMPurify to prevent XSS attacks.
* The sanitization process allows 'onclick' attributes and specific URI schemes.
* Then, it parses the sanitized string into an HTML document and returns its body.
* Optionally, it can apply predefined CSS styles to the body element.
*
* @param {string} str The HTML string to convert.
* @param {boolean} [styles=false] Whether to apply predefined styles to the document body.
* @returns {HTMLElement} The body element of the parsed and sanitized HTML document.
*/
export default function stringToHTML(str: string, styles = false) {
const parser = new DOMParser();
str = DOMPurify.sanitize(str, {
ADD_ATTR: ["onclick"],
ALLOWED_URI_REGEXP:
/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|chrome-extension):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
});
const doc = parser.parseFromString(str, "text/html");
if (styles) {
doc.body.style.cssText =
"height: auto; overflow: scroll; margin: 0px; background: var(--background-primary);";
}
return doc.body;
}