refactor: move encode HTML function into separate module (#4591)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
co-authored by
Alexandr
parent
af1d8cad6f
commit
b240a06e7b
@@ -0,0 +1,19 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Encode string as HTML.
|
||||
*
|
||||
* @see https://stackoverflow.com/a/48073476/10629172
|
||||
*
|
||||
* @param {string} str String to encode.
|
||||
* @returns {string} Encoded string.
|
||||
*/
|
||||
const encodeHTML = (str) => {
|
||||
return str
|
||||
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
|
||||
return "&#" + i.charCodeAt(0) + ";";
|
||||
})
|
||||
.replace(/\u0008/gim, "");
|
||||
};
|
||||
|
||||
export { encodeHTML };
|
||||
Reference in New Issue
Block a user