import DOMPurify from 'dompurify'; /** * Gets the number of lines in a string * * @param {string | undefined} s The string to check the lines for * @returns {number} The number of lines in that string */ export const getRows = (s) => { if (!s) return 1; let str = breakToPlaceholder(s); str = str.replace(/\\n/g, '#br#'); return str.split('#br#'); }; export const removeEscapes = (text) => { let newStr = text.replace(/\\u[\dA-F]{4}/gi, function (match) { return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); }); newStr = newStr.replace(/\\x([0-9a-f]{2})/gi, (_, c) => String.fromCharCode(parseInt(c, 16))); newStr = newStr.replace(/\\[\d\d\d]{3}/gi, function (match) { return String.fromCharCode(parseInt(match.replace(/\\/g, ''), 8)); }); newStr = newStr.replace(/\\[\d\d\d]{2}/gi, function (match) { return String.fromCharCode(parseInt(match.replace(/\\/g, ''), 8)); }); return newStr; }; /** * Removes script tags from a text * * @param {string} txt The text to sanitize * @returns {string} The safer text */ export const removeScript = (txt) => { var rs = ''; var idx = 0; while (idx >= 0) { idx = txt.indexOf('= 0) { rs += txt.substr(0, idx); txt = txt.substr(idx + 1); idx = txt.indexOf(''); if (idx >= 0) { idx += 9; txt = txt.substr(idx); } } else { rs += txt; idx = -1; break; } } let decodedText = removeEscapes(rs); decodedText = decodedText.replace(/script>/gi, '#'); decodedText = decodedText.replace(/javascript:/gi, '#'); decodedText = decodedText.replace(/javascript&colon/gi, '#'); decodedText = decodedText.replace(/onerror=/gi, 'onerror:'); decodedText = decodedText.replace(/