$.htmlentities = { /** * Converts a string to its html characters completely. * It's equivalent to htmlentities() in PHP * Reference: https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/ * * @param {String} str String with unescaped HTML characters **/ encode (str) { return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); }, /** * Converts an html characterSet into its original character. * It's equivalent to html_entity_decode() in PHP * Reference: https://stackoverflow.com/questions/5796718/html-entity-decode * * @param {string} * @return {string} **/ decode: (() => { // this prevents any overhead from creating the object each time let element = document.createElement('div'); function decodeHTMLEntities(str) { if (str && typeof str === 'string') { // strip script/html tags str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, ''); str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, ''); element.innerHTML = str; str = element.textContent; element.textContent = ''; } return str; } return decodeHTMLEntities; })() };