diff options
| author | stonedDiscord <Tukz@gmx.de> | 2022-03-28 23:41:56 +0200 |
|---|---|---|
| committer | stonedDiscord <Tukz@gmx.de> | 2022-03-28 23:41:56 +0200 |
| commit | 903a13ff5b5beaaf42b4fcf8965eb55b642e1d2a (patch) | |
| tree | 7e7cbf09c1ce6d2539bb2f33a9c3c86f19ce9d48 /webAO/encoding.ts | |
| parent | 06ee582c4adefdb35220c63ee4a30444474e9388 (diff) | |
| parent | d3911aa9ad6bc16c70355fe11d1377d636b14565 (diff) | |
Merge branch 'master' into coolloading
Diffstat (limited to 'webAO/encoding.ts')
| -rw-r--r-- | webAO/encoding.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/webAO/encoding.ts b/webAO/encoding.ts new file mode 100644 index 0000000..1018144 --- /dev/null +++ b/webAO/encoding.ts @@ -0,0 +1,64 @@ +/** + * Escapes a string to AO1 escape codes. + * @param {string} estring the string to be escaped + */ +export function escapeChat(estring: string): string { + return estring + .replace(/#/g, '<num>') + .replace(/&/g, '<and>') + .replace(/%/g, '<percent>') + .replace(/\$/g, '<dollar>'); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring: string): string { + return estring + .replace(/<num>/g, '#') + .replace(/<and>/g, '&') + .replace(/<percent>/g, '%') + .replace(/<dollar>/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 safeTags(unsafe: string): string { + if (unsafe) { + return unsafe + .replace(/>/g, '>') + .replace(/</g, '<'); + } + return ''; +} + +/** + * Encode text on client side. + * @param {string} estring the string to be encoded + */ +export function encodeChat(estring: string): string { + return estring; +} + +/** + * Decodes text on client side. + * @param {string} estring the string to be decoded + */ +export function decodeChat(estring: string): string { + // Source: https://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode + return estring.replace(/\\u([\d\w]{1,})/gi, (match, group) => String.fromCharCode(parseInt(group, 16))); +} + +/** + * XXX: a nasty hack made by gameboyprinter. + * @param {string} msg chat message to prepare for display + */ +export function prepChat(msg: string): string { + // TODO: make this less awful + return unescapeChat(decodeChat(msg)); +} |
