1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/**
* Escapes a string to AO1 escape codes.
* @param {string} estring the string to be escaped
*/
export function escapeChat(estring: string): string {
return estring
.replaceAll("#", "<num>")
.replaceAll("&", "<and>")
.replaceAll("%", "<percent>")
.replaceAll("$", "<dollar>");
}
/**
* Unescapes a string to AO1 escape codes.
* @param {string} estring the string to be unescaped
*/
export function unescapeChat(estring: string): string {
return estring
.replaceAll("<num>", "#")
.replaceAll("<and>", "&")
.replaceAll("<percent>", "%")
.replaceAll("<dollar>", "$");
}
/**
* 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.replaceAll(">", ">").replaceAll("<", "<");
}
return "";
}
/**
* 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 safeTags(unescapeChat(decodeChat(msg)));
}
|