Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep message formatting linear on unmatched markers
Yudhi Armyndharis3fbf169434a1
diff --git a/dashboard/src/utils/messageFormatter.test.ts b/dashboard/src/utils/messageFormatter.test.tsindex c444bc48c..ff7639c34 100644--- a/dashboard/src/utils/messageFormatter.test.ts+++ b/dashboard/src/utils/messageFormatter.test.ts@@ -103,6 +103,21 @@ test('hostile input: a flood of formatted segments parses iteratively', () => {assert.deepEqual(nodes[1], text(' '));});+test('hostile input: openers with no closer do not rescan the rest of the message each time', () => {+ // Every `*` below follows a space, so none can close, and each opener used to scan to the end of+ // the body: about 3 s of main thread for one 64k message. The second body adds a valid span per+ // repetition, which used to rescan the dead `_` openers after every split.+ for (const evil of ['*a '.repeat(21_000), '_a *b* '.repeat(9_000)]) {+ const started = performance.now();+ const nodes = parseMessageBody(evil);+ const elapsed = performance.now() - started;+ assert.ok(elapsed < 500, `parsing ${evil.length} chars took ${Math.round(elapsed)} ms`);+ assert.ok(nodes.length > 0);+ }+ assert.deepEqual(parseMessageBody('*a '.repeat(3)), [text('*a *a *a ')]);+ assert.deepEqual(parseMessageBody('_a *b* _a'), [text('_a '), { type: 'bold', children: [text('b')] }, text(' _a')]);+});+test('a MENTION_OPEN/MENTION_CLOSE span becomes its own mention node, not text', () => {assert.deepEqual(parseMessageBody(`hi ${wrap('@Ravi')} there`), [text('hi '),diff --git a/dashboard/src/utils/messageFormatter.ts b/dashboard/src/utils/messageFormatter.tsindex 7359c63cc..0fca37a1f 100644--- a/dashboard/src/utils/messageFormatter.ts+++ b/dashboard/src/utils/messageFormatter.ts@@ -185,9 +185,12 @@ function parseFormatting(input: string, depth = 0): MessageNode[] {if (depth >= MAX_FORMAT_DEPTH) return pushText([], input);const nodes: MessageNode[] = [];+ // Shared across the loop because every `rest` is a suffix of `input`, so a marker that found no+ // closer stays without one; not shared with the recursive call, whose `inner` ends earlier.+ const exhausted = new Set<string>();let rest = input;while (rest.length > 0) {- const split = splitFirstFormat(rest);+ const split = splitFirstFormat(rest, exhausted);if (!split) {pushText(nodes, rest);break;@@ -205,9 +208,15 @@ function parseFormatting(input: string, depth = 0): MessageNode[] {* marker satisfying the same boundary rules. Returns the text before the span, the format, the* inner content, and the unconsumed remainder — or null when the segment holds no valid span* (unbalanced or boundary-violating markers stay literal).+ *+ * `exhausted` holds the markers an earlier opener already scanned to the end without a closer. A+ * closer's validity depends only on its neighbours, so a later opener of the same marker would see+ * a subset of those candidates and cannot succeed either; skipping it keeps the parse linear instead+ * of rescanning the rest of the message for every unmatched opener.*/function splitFirstFormat(input: string,+ exhausted: Set<string>,): { before: string; fmt: 'bold' | 'italic' | 'strike'; inner: string; after: string } | null {for (let i = 0; i < input.length; i++) {const skip = mentionEnd(input, i);@@ -217,7 +226,7 @@ function splitFirstFormat(}const ch = input[i];const fmt = FORMATS[ch];- if (!fmt) continue;+ if (!fmt || exhausted.has(ch)) continue;// Boundary outside the opener: previous char must be a boundary or string-start.const prev = i === 0 ? '' : input[i - 1];@@ -249,7 +258,8 @@ function splitFirstFormat(after: input.slice(j + 1),};}- // No matching closer for this opener — fall through to next character.+ // No matching closer for this opener, so none for any later one of the same marker either.+ exhausted.add(ch);}return null;