more escape fixes

This commit is contained in:
LenAnderson
2024-04-19 09:03:22 -04:00
parent 06006b6c08
commit fe81221c56

View File

@ -12,12 +12,14 @@ export class SlashCommandParser {
/**@type {Object.<string, SlashCommand>}*/ commands = {}; /**@type {Object.<string, SlashCommand>}*/ commands = {};
// @ts-ignore // @ts-ignore
/**@type {Object.<string, string>}*/ helpStrings = {}; /**@type {Object.<string, string>}*/ helpStrings = {};
/**@type {Boolean}*/ verifyCommandNames = true; /**@type {boolean}*/ verifyCommandNames = true;
/**@type {String}*/ text; /**@type {string}*/ text;
/**@type {String}*/ keptText; /**@type {string}*/ keptText;
/**@type {Number}*/ index; /**@type {number}*/ index;
/**@type {SlashCommandScope}*/ scope; /**@type {SlashCommandScope}*/ scope;
/**@type {boolean}*/ jumpedEscapeSequence = false;
/**@type {{start:number, end:number}[]}*/ closureIndex; /**@type {{start:number, end:number}[]}*/ closureIndex;
/**@type {SlashCommandExecutor[]}*/ commandIndex; /**@type {SlashCommandExecutor[]}*/ commandIndex;
/**@type {SlashCommandScope[]}*/ scopeIndex; /**@type {SlashCommandScope[]}*/ scopeIndex;
@ -227,6 +229,7 @@ export class SlashCommandParser {
* @returns The last character taken. * @returns The last character taken.
*/ */
take(length = 1, keep = false) { take(length = 1, keep = false) {
this.jumpedEscapeSequence = false;
let content = this.char; let content = this.char;
this.index++; this.index++;
if (keep) this.keptText += content; if (keep) this.keptText += content;
@ -236,7 +239,10 @@ export class SlashCommandParser {
return content; return content;
} }
discardWhitespace() { discardWhitespace() {
while (/\s/.test(this.char)) this.take(); // discard whitespace while (/\s/.test(this.char)) {
this.take(); // discard whitespace
this.jumpedEscapeSequence = false;
}
} }
/** /**
* Tests if the next characters match a symbol. * Tests if the next characters match a symbol.
@ -260,12 +266,16 @@ export class SlashCommandParser {
// /echo abc \\\\| /echo def // /echo abc \\\\| /echo def
// -> TOAST: abc \\ // -> TOAST: abc \\
// -> TOAST: def // -> TOAST: def
const escapes = this.text.slice(this.index + offset).replace(/^(\\*).*$/s, '$1').length; // /echo title=\:} \{: | /echo title=\{: \:}
// -> TOAST: *:}* {:
// -> TOAST: *{:* :}
const escapeOffset = this.jumpedEscapeSequence ? -1 : 0;
const escapes = this.text.slice(this.index + offset + escapeOffset).replace(/^(\\*).*$/s, '$1').length;
const test = (sequence instanceof RegExp) ? const test = (sequence instanceof RegExp) ?
(text) => new RegExp(`^${sequence.source}`).test(text) : (text) => new RegExp(`^${sequence.source}`).test(text) :
(text) => text.startsWith(sequence) (text) => text.startsWith(sequence)
; ;
if (test(this.text.slice(this.index + offset + escapes))) { if (test(this.text.slice(this.index + offset + escapeOffset + escapes))) {
// no backslashes before sequence // no backslashes before sequence
// -> sequence found // -> sequence found
if (escapes == 0) return true; if (escapes == 0) return true;
@ -276,7 +286,10 @@ export class SlashCommandParser {
// even number of backslashes before sequence // even number of backslashes before sequence
// = every pair is one literal backslash // = every pair is one literal backslash
// -> move index forward to skip the backslash escaping the first backslash // -> move index forward to skip the backslash escaping the first backslash
if (offset == 0) this.index++; if (!this.jumpedEscapeSequence && offset == 0) {
this.index++;
this.jumpedEscapeSequence = true;
}
return false; return false;
} }
} }
@ -460,7 +473,7 @@ export class SlashCommandParser {
} }
parseUnnamedArgument() { parseUnnamedArgument() {
/**@type {SlashCommandClosure|String}*/ /**@type {SlashCommandClosure|String}*/
let value = this.take(); // take the first, already tested, char let value = this.jumpedEscapeSequence ? this.take() : ''; // take the first, already tested, char if it is an escaped one
let isList = false; let isList = false;
let listValues = []; let listValues = [];
while (!this.testUnnamedArgumentEnd()) { while (!this.testUnnamedArgumentEnd()) {
@ -526,7 +539,7 @@ export class SlashCommandParser {
return this.testCommandEnd(); return this.testCommandEnd();
} }
parseValue() { parseValue() {
let value = this.take(); // take the first, already tested, char let value = this.jumpedEscapeSequence ? this.take() : ''; // take the first, already tested, char if it is an escaped one
while (!this.testValueEnd()) value += this.take(); // take all chars until value end while (!this.testValueEnd()) value += this.take(); // take all chars until value end
return value; return value;
} }