From 2ed9af82f21f1390d507f04638157b63bfd4b949 Mon Sep 17 00:00:00 2001 From: sD Date: Wed, 25 Mar 2020 23:12:27 +0100 Subject: move encoding stuff to seperate js file --- webAO/encoding.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 webAO/encoding.js (limited to 'webAO/encoding.js') diff --git a/webAO/encoding.js b/webAO/encoding.js new file mode 100644 index 0000000..76e0b6d --- /dev/null +++ b/webAO/encoding.js @@ -0,0 +1,85 @@ +/** + * Escapes a string to AO1 escape codes. + * @param {string} estring the string to be escaped + */ +export function escapeChat(estring) { + return estring + .replace(/#/g, "") + .replace(/&/g, "") + .replace(/%/g, "") + .replace(/\$/g, ""); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring) { + return estring + .replace(//g, "#") + .replace(//g, "&") + .replace(//g, "%") + .replace(//g, "$"); +} + +/** + * Escapes a string to be HTML-safe. + * + * XXX: This is unnecessary if we use `createTextNode` instead! + * @param {string} unsafe an unsanitized string + */ +export function safe_tags(unsafe) { + if (unsafe) { + return unsafe + .replace(/&/g, "&") + .replace(/