From 533aeffa369ef4f61b961323c7578a13c6440d38 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 27 Mar 2025 23:22:38 +0200 Subject: [PATCH 1/5] Add opt-in for rawQuotes in SlashCommand registration Closes #2739 Supersedes #2921 --- public/scripts/slash-commands.js | 5 +++++ public/scripts/slash-commands/SlashCommand.js | 2 ++ public/scripts/slash-commands/SlashCommandParser.js | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 304544051..c7a4dd86c 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -227,6 +227,7 @@ export function initDefaultSlashCommands() { })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sendas', + rawQuotes: true, callback: sendMessageAs, returns: 'Optionally the text of the sent message, if specified in the "return" argument', namedArgumentList: [ @@ -293,6 +294,7 @@ export function initDefaultSlashCommands() { })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sys', + rawQuotes: true, callback: sendNarratorMessage, aliases: ['nar'], returns: 'Optionally the text of the sent message, if specified in the "return" argument', @@ -357,6 +359,7 @@ export function initDefaultSlashCommands() { })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'comment', + rawQuotes: true, callback: sendCommentMessage, returns: 'Optionally the text of the sent message, if specified in the "return" argument', namedArgumentList: [ @@ -574,6 +577,7 @@ export function initDefaultSlashCommands() { })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'send', + rawQuotes: true, callback: sendUserMessageCallback, returns: 'Optionally the text of the sent message, if specified in the "return" argument', namedArgumentList: [ @@ -936,6 +940,7 @@ export function initDefaultSlashCommands() { })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'echo', + rawQuotes: true, callback: echoCallback, returns: 'the text', namedArgumentList: [ diff --git a/public/scripts/slash-commands/SlashCommand.js b/public/scripts/slash-commands/SlashCommand.js index 274dac577..ec57546ba 100644 --- a/public/scripts/slash-commands/SlashCommand.js +++ b/public/scripts/slash-commands/SlashCommand.js @@ -36,6 +36,7 @@ export class SlashCommand { * @param {string} [props.helpString] * @param {boolean} [props.splitUnnamedArgument] * @param {Number} [props.splitUnnamedArgumentCount] + * @param {boolean} [props.rawQuotes] * @param {string[]} [props.aliases] * @param {string} [props.returns] * @param {SlashCommandNamedArgument[]} [props.namedArgumentList] @@ -54,6 +55,7 @@ export class SlashCommand { /**@type {string}*/ helpString; /**@type {boolean}*/ splitUnnamedArgument = false; /**@type {Number}*/ splitUnnamedArgumentCount; + /** @type {boolean} */ rawQuotes = false; /**@type {string[]}*/ aliases = []; /**@type {string}*/ returns; /**@type {SlashCommandNamedArgument[]}*/ namedArgumentList = []; diff --git a/public/scripts/slash-commands/SlashCommandParser.js b/public/scripts/slash-commands/SlashCommandParser.js index ea5b2b37b..c664bc641 100644 --- a/public/scripts/slash-commands/SlashCommandParser.js +++ b/public/scripts/slash-commands/SlashCommandParser.js @@ -975,7 +975,7 @@ export class SlashCommandParser { cmd.startUnnamedArgs = this.index - (/\s(\s*)$/s.exec(this.behind)?.[1]?.length ?? 0); cmd.endUnnamedArgs = this.index; if (this.testUnnamedArgument()) { - cmd.unnamedArgumentList = this.parseUnnamedArgument(cmd.command?.unnamedArgumentList?.length && cmd?.command?.splitUnnamedArgument, cmd?.command?.splitUnnamedArgumentCount); + cmd.unnamedArgumentList = this.parseUnnamedArgument(cmd.command?.unnamedArgumentList?.length && cmd?.command?.splitUnnamedArgument, cmd?.command?.splitUnnamedArgumentCount, cmd?.command?.rawQuotes); cmd.endUnnamedArgs = this.index; if (cmd.name == 'let') { const keyArg = cmd.namedArgumentList.find(it=>it.name == 'key'); @@ -1035,7 +1035,7 @@ export class SlashCommandParser { testUnnamedArgumentEnd() { return this.testCommandEnd(); } - parseUnnamedArgument(split, splitCount = null) { + parseUnnamedArgument(split, splitCount = null, rawQuotes = false) { const wasSplit = split; /**@type {SlashCommandClosure|String}*/ let value = this.jumpedEscapeSequence ? this.take() : ''; // take the first, already tested, char if it is an escaped one @@ -1045,7 +1045,7 @@ export class SlashCommandParser { /**@type {SlashCommandUnnamedArgumentAssignment}*/ let assignment = new SlashCommandUnnamedArgumentAssignment(); assignment.start = this.index; - if (!split && this.testQuotedValue()) { + if (!split && !rawQuotes && this.testQuotedValue()) { // if the next bit is a quoted value, take the whole value and gather contents as a list assignment.value = this.parseQuotedValue(); assignment.end = this.index; From c05b6b0ae807a8259ba32024018546d0ef593bec Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 28 Mar 2025 10:28:02 +0200 Subject: [PATCH 2/5] Add JSDoc to rawQuotes --- public/scripts/slash-commands/SlashCommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/slash-commands/SlashCommand.js b/public/scripts/slash-commands/SlashCommand.js index ec57546ba..388850fa6 100644 --- a/public/scripts/slash-commands/SlashCommand.js +++ b/public/scripts/slash-commands/SlashCommand.js @@ -36,7 +36,7 @@ export class SlashCommand { * @param {string} [props.helpString] * @param {boolean} [props.splitUnnamedArgument] * @param {Number} [props.splitUnnamedArgumentCount] - * @param {boolean} [props.rawQuotes] + * @param {boolean} [props.rawQuotes] If set to true, does not remove wrapping quotes from the unnamed argument. * @param {string[]} [props.aliases] * @param {string} [props.returns] * @param {SlashCommandNamedArgument[]} [props.namedArgumentList] From deb13e9c97dcf724fff3342f69b6ad0f27ca936b Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 28 Mar 2025 10:29:32 +0200 Subject: [PATCH 3/5] Fix backward compatibility with escaped leading quote --- public/scripts/slash-commands/SlashCommandParser.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/public/scripts/slash-commands/SlashCommandParser.js b/public/scripts/slash-commands/SlashCommandParser.js index c664bc641..b87914d7e 100644 --- a/public/scripts/slash-commands/SlashCommandParser.js +++ b/public/scripts/slash-commands/SlashCommandParser.js @@ -566,9 +566,10 @@ export class SlashCommandParser { * Expects that the current char is taken after testing. * @param {string|RegExp} sequence Sequence of chars or regex character group that is the symbol. * @param {number} offset Offset from the current index (won't move the index if offset != 0). + * @param {boolean} escape Whether to escape the symbol with backslashes. * @returns Whether the next characters are the indicated symbol. */ - testSymbol(sequence, offset = 0) { + testSymbol(sequence, offset = 0, escape = true) { if (!this.flags[PARSER_FLAG.STRICT_ESCAPING]) return this.testSymbolLooseyGoosey(sequence, offset); // /echo abc | /echo def // -> TOAST: abc @@ -587,7 +588,7 @@ export class SlashCommandParser { // -> TOAST: *:}* {: // -> TOAST: *{:* :} const escapeOffset = this.jumpedEscapeSequence ? -1 : 0; - const escapes = this.text.slice(this.index + offset + escapeOffset).replace(/^(\\*).*$/s, '$1').length; + const escapes = escape ? this.text.slice(this.index + offset + escapeOffset).replace(/^(\\*).*$/s, '$1').length : 0; const test = (sequence instanceof RegExp) ? (text) => new RegExp(`^${sequence.source}`).test(text) : (text) => text.startsWith(sequence) @@ -1045,6 +1046,9 @@ export class SlashCommandParser { /**@type {SlashCommandUnnamedArgumentAssignment}*/ let assignment = new SlashCommandUnnamedArgumentAssignment(); assignment.start = this.index; + if (rawQuotes && this.testEscapedQuote()) { + this.take(); // discard the escaping backslash + } if (!split && !rawQuotes && this.testQuotedValue()) { // if the next bit is a quoted value, take the whole value and gather contents as a list assignment.value = this.parseQuotedValue(); @@ -1187,6 +1191,11 @@ export class SlashCommandParser { testQuotedValue() { return this.testSymbol('"'); } + + testEscapedQuote() { + return this.testSymbol('\\"', 0, false); + } + testQuotedValueEnd() { if (this.endOfText) { if (this.verifyCommandNames) throw new SlashCommandParserError(`Unexpected end of quoted value at position ${this.index}`, this.text, this.index); From bdcf9b088e3369023a69c909aa25fd9b95b0ab57 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 28 Mar 2025 21:00:02 +0200 Subject: [PATCH 4/5] Revert "Fix backward compatibility with escaped leading quote" This reverts commit deb13e9c97dcf724fff3342f69b6ad0f27ca936b. --- public/scripts/slash-commands/SlashCommandParser.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/public/scripts/slash-commands/SlashCommandParser.js b/public/scripts/slash-commands/SlashCommandParser.js index b87914d7e..c664bc641 100644 --- a/public/scripts/slash-commands/SlashCommandParser.js +++ b/public/scripts/slash-commands/SlashCommandParser.js @@ -566,10 +566,9 @@ export class SlashCommandParser { * Expects that the current char is taken after testing. * @param {string|RegExp} sequence Sequence of chars or regex character group that is the symbol. * @param {number} offset Offset from the current index (won't move the index if offset != 0). - * @param {boolean} escape Whether to escape the symbol with backslashes. * @returns Whether the next characters are the indicated symbol. */ - testSymbol(sequence, offset = 0, escape = true) { + testSymbol(sequence, offset = 0) { if (!this.flags[PARSER_FLAG.STRICT_ESCAPING]) return this.testSymbolLooseyGoosey(sequence, offset); // /echo abc | /echo def // -> TOAST: abc @@ -588,7 +587,7 @@ export class SlashCommandParser { // -> TOAST: *:}* {: // -> TOAST: *{:* :} const escapeOffset = this.jumpedEscapeSequence ? -1 : 0; - const escapes = escape ? this.text.slice(this.index + offset + escapeOffset).replace(/^(\\*).*$/s, '$1').length : 0; + const escapes = this.text.slice(this.index + offset + escapeOffset).replace(/^(\\*).*$/s, '$1').length; const test = (sequence instanceof RegExp) ? (text) => new RegExp(`^${sequence.source}`).test(text) : (text) => text.startsWith(sequence) @@ -1046,9 +1045,6 @@ export class SlashCommandParser { /**@type {SlashCommandUnnamedArgumentAssignment}*/ let assignment = new SlashCommandUnnamedArgumentAssignment(); assignment.start = this.index; - if (rawQuotes && this.testEscapedQuote()) { - this.take(); // discard the escaping backslash - } if (!split && !rawQuotes && this.testQuotedValue()) { // if the next bit is a quoted value, take the whole value and gather contents as a list assignment.value = this.parseQuotedValue(); @@ -1191,11 +1187,6 @@ export class SlashCommandParser { testQuotedValue() { return this.testSymbol('"'); } - - testEscapedQuote() { - return this.testSymbol('\\"', 0, false); - } - testQuotedValueEnd() { if (this.endOfText) { if (this.verifyCommandNames) throw new SlashCommandParserError(`Unexpected end of quoted value at position ${this.index}`, this.text, this.index); From 819ce198a8ddf2ec2ff42b170f9bdfba8fb37643 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 29 Mar 2025 18:04:11 +0200 Subject: [PATCH 5/5] Add raw quotes indicator in slash command autocomplete --- public/scripts/slash-commands/SlashCommand.js | 10 ++++++++++ public/style.css | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/public/scripts/slash-commands/SlashCommand.js b/public/scripts/slash-commands/SlashCommand.js index 388850fa6..4c90ca3ee 100644 --- a/public/scripts/slash-commands/SlashCommand.js +++ b/public/scripts/slash-commands/SlashCommand.js @@ -1,4 +1,5 @@ import { hljs } from '../../lib.js'; +import { t } from '../i18n.js'; import { SlashCommandAbortController } from './SlashCommandAbortController.js'; import { SlashCommandArgument, SlashCommandNamedArgument } from './SlashCommandArgument.js'; import { SlashCommandClosure } from './SlashCommandClosure.js'; @@ -260,6 +261,15 @@ export class SlashCommand { ].filter(it=>it).join('\n'); head.append(src); } + if (this.rawQuotes) { + const rawQuotes = document.createElement('div'); { + rawQuotes.classList.add('rawQuotes'); + rawQuotes.classList.add('fa-solid'); + rawQuotes.classList.add('fa-quote-left'); + rawQuotes.title = t`Does not alter quoted literal unnamed arguments`; + head.append(rawQuotes); + } + } specs.append(head); } const body = document.createElement('div'); { diff --git a/public/style.css b/public/style.css index 13a1267ab..7355c4f7b 100644 --- a/public/style.css +++ b/public/style.css @@ -2086,6 +2086,15 @@ body[data-stscript-style] .hljs.language-stscript { } } + >.head>.rawQuotes { + padding: 0 0.5em; + cursor: help; + + &:hover { + text-decoration: 1px dotted underline; + } + } + >.head>.source { padding: 0 0.5em; cursor: help;