Add a fallback if DOMParser does not exist

This commit is contained in:
nachtjasmin 2023-07-22 12:55:28 +02:00
parent bd14d190a2
commit 58a8404a8d
No known key found for this signature in database

View file

@ -92,6 +92,19 @@ export function insertAfter(insertion, target) {
* @returns {string}
*/
export function htmlDecode(input) {
if (typeof window === "undefined" || !window.DOMParser) {
const replacements = {
"&": "&",
""": '"',
"&lt;": "<",
"&gt;": ">",
"&nbsp;": "",
};
for (const [html, text] of Object.entries(replacements)) input = input.replaceAll(html, text);
return input;
}
const doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}