From b63cd67c24ed7434c1de4a0e0e2f346b3bcc67a2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 11 Oct 2023 21:32:18 -0400 Subject: [PATCH] fix: Expensive recursive regex when using French language, closes #5939 --- shared/editor/extensions/Suggestion.tsx | 11 +++++++---- shared/editor/nodes/Emoji.tsx | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/shared/editor/extensions/Suggestion.tsx b/shared/editor/extensions/Suggestion.tsx index 745358a6a..916b0ae59 100644 --- a/shared/editor/extensions/Suggestion.tsx +++ b/shared/editor/extensions/Suggestion.tsx @@ -15,12 +15,15 @@ export default class Suggestion extends Extension { keys() { return { Backspace: (state: EditorState) => { - const textBeforeCursor = state.doc.textBetween( - 0, - Math.max(0, state.selection.from - 1) + const { $from } = state.selection; + const textBefore = $from.parent.textBetween( + Math.max(0, $from.parentOffset - 500), // 500 = max match + Math.max(0, $from.parentOffset - 1), // 1 = account for deleted character + null, + "\ufffc" ); - if (this.options.openRegex.test(textBeforeCursor)) { + if (this.options.openRegex.test(textBefore)) { return false; } diff --git a/shared/editor/nodes/Emoji.tsx b/shared/editor/nodes/Emoji.tsx index a4e49b144..3f5845c10 100644 --- a/shared/editor/nodes/Emoji.tsx +++ b/shared/editor/nodes/Emoji.tsx @@ -14,7 +14,7 @@ import { SuggestionsMenuType } from "../plugins/Suggestions"; import emojiRule from "../rules/emoji"; /** - * Languages using the colon character with a space infront in standard + * Languages using the colon character with a space in front in standard * punctuation. In this case the trigger is only matched once there is additional * text after the colon. */ @@ -34,7 +34,7 @@ export default class Emoji extends Suggestion { return { type: SuggestionsMenuType.Emoji, openRegex: new RegExp( - `(?:^|\\s):([0-9a-zA-Z_+-]+)${languageIsUsingColon ? "+" : "?"}$` + `(?:^|\\s):([0-9a-zA-Z_+-]+)${languageIsUsingColon ? "" : "?"}$` ), closeRegex: /(?:^|\s):(([0-9a-zA-Z_+-]*\s+)|(\s+[0-9a-zA-Z_+-]+)|[^0-9a-zA-Z_+-]+)$/,