From 42b954f209fdc9914662307671342c2ed0e43c6d Mon Sep 17 00:00:00 2001 From: ItsVipra Date: Wed, 19 Jul 2023 11:54:59 +0200 Subject: [PATCH 1/3] add htmlDecode() to unescape special characters --- src/libs/domhelpers.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libs/domhelpers.js b/src/libs/domhelpers.js index cbcef9e..27baca2 100644 --- a/src/libs/domhelpers.js +++ b/src/libs/domhelpers.js @@ -85,3 +85,13 @@ export function insertAfter(insertion, target) { //docs: https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore#example_2 target.parentElement.insertBefore(insertion, target.nextSibling); } + +/** + * Turns HTML text into human-readable text + * @param {string} input HTML Text + * @returns {string} + */ +export function htmlDecode(input) { + const doc = new DOMParser().parseFromString(input, "text/html"); + return doc.documentElement.textContent; +} From bd14d190a2f757899eb795f618da186750af98a3 Mon Sep 17 00:00:00 2001 From: ItsVipra Date: Wed, 19 Jul 2023 11:55:18 +0200 Subject: [PATCH 2/3] add html decoding to pronoun sanitation --- src/libs/pronouns.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/pronouns.js b/src/libs/pronouns.js index aacc82e..0f0a708 100644 --- a/src/libs/pronouns.js +++ b/src/libs/pronouns.js @@ -1,4 +1,5 @@ import sanitizeHtml from "sanitize-html"; +import { htmlDecode } from "./domhelpers.js"; const fieldMatchers = [/\bpro.*nouns?\b/i, /\bpronomen\b/i, /(i )?go(es)? by/i]; const knownPronounUrls = [ @@ -179,9 +180,12 @@ function sanitizePronouns(str) { // Remove trailing characters that are used as separators. str = str.replace(/[-| :/]+$/, ""); - // Finally, remove leading and trailing whitespace. + // Remove leading and trailing whitespace. str = str.trim(); + //Finally, turn escaped characters (e.g. &,>) back into their original form + str = htmlDecode(str); + // If the result is empty, return null, otherwise the empty string. return str === "" ? null : str; } From 58a8404a8d902ec55da993fe61da97d908e71b61 Mon Sep 17 00:00:00 2001 From: nachtjasmin Date: Sat, 22 Jul 2023 12:55:28 +0200 Subject: [PATCH 3/3] Add a fallback if DOMParser does not exist --- src/libs/domhelpers.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libs/domhelpers.js b/src/libs/domhelpers.js index 27baca2..366f25c 100644 --- a/src/libs/domhelpers.js +++ b/src/libs/domhelpers.js @@ -92,6 +92,19 @@ export function insertAfter(insertion, target) { * @returns {string} */ export function htmlDecode(input) { + if (typeof window === "undefined" || !window.DOMParser) { + const replacements = { + "&": "&", + """: '"', + "<": "<", + ">": ">", + " ": "", + }; + 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; }