mirror of
https://git.sr.ht/~emersion/gamja
synced 2024-11-14 19:25:26 -05:00
Avoid String.prototype.replaceAll
Not well supported in web browsers, except Firefox.
This commit is contained in:
parent
73f14f38fa
commit
deb8d4c01e
1 changed files with 13 additions and 9 deletions
22
lib/irc.js
22
lib/irc.js
|
@ -20,7 +20,7 @@ export const ERR_SASLALREADY = "907";
|
|||
|
||||
export const STD_CHANNEL_TYPES = "#&+!";
|
||||
|
||||
var tagsEscape = {
|
||||
const tagEscapeMap = {
|
||||
";": "\\:",
|
||||
" ": "\\s",
|
||||
"\\": "\\\\",
|
||||
|
@ -28,6 +28,16 @@ var tagsEscape = {
|
|||
"\n": "\\n",
|
||||
};
|
||||
|
||||
const tagUnescapeMap = Object.fromEntries(Object.entries(tagEscapeMap).map(([from, to]) => [to, from]));
|
||||
|
||||
function escapeTag(s) {
|
||||
return s.replace(/[; \\\r\n]/, (ch) => tagEscapeMap[ch]);
|
||||
}
|
||||
|
||||
function unescapeTag(s) {
|
||||
return s.replace(/\\[:s\\rn]/, (seq) => tagUnescapeMap[seq]);
|
||||
}
|
||||
|
||||
function parseTags(s) {
|
||||
var tags = {};
|
||||
s.split(";").forEach(function(s) {
|
||||
|
@ -39,10 +49,7 @@ function parseTags(s) {
|
|||
throw new Error("expected an equal sign in tag");
|
||||
}
|
||||
var k = parts[0];
|
||||
var v = parts[1];
|
||||
for (var ch in tagsEscape) {
|
||||
v = v.replaceAll(tagsEscape[ch], ch);
|
||||
}
|
||||
var v = unescapeTag(parts[1]);
|
||||
if (v.endsWith("\\")) {
|
||||
v = v.slice(0, v.length - 1)
|
||||
}
|
||||
|
@ -54,10 +61,7 @@ function parseTags(s) {
|
|||
function formatTags(tags) {
|
||||
var l = [];
|
||||
for (var k in tags) {
|
||||
var v = tags[k];
|
||||
for (var ch in tagsEscape) {
|
||||
v = v.replaceAll(ch, tagsEscape[ch]);
|
||||
}
|
||||
var v = escapeTag(tags[k]);
|
||||
l.push(k + "=" + v);
|
||||
}
|
||||
return l.join(";");
|
||||
|
|
Loading…
Reference in a new issue