From 8a3eb1105cc754734b97070deb1694fe73202c41 Mon Sep 17 00:00:00 2001 From: Mo Date: Fri, 6 Sep 2024 10:38:39 -0500 Subject: [PATCH] Fix issue where cmd/ctrl + left arrow after a tab character would cause exception (#6588) --- packages/lexical-code/src/CodeHighlighter.ts | 2 ++ .../__tests__/e2e/Tab.spec.mjs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/lexical-code/src/CodeHighlighter.ts b/packages/lexical-code/src/CodeHighlighter.ts index e4a4f626f28..dd721b03e9a 100644 --- a/packages/lexical-code/src/CodeHighlighter.ts +++ b/packages/lexical-code/src/CodeHighlighter.ts @@ -773,7 +773,9 @@ function $handleMoveTo( const focusNode = focus.getNode(); const isMoveToStart = type === MOVE_TO_START; + // Ensure the selection is within the codeblock if ( + !$isSelectionInCode(selection) || !($isCodeHighlightNode(anchorNode) || $isTabNode(anchorNode)) || !($isCodeHighlightNode(focusNode) || $isTabNode(focusNode)) ) { diff --git a/packages/lexical-playground/__tests__/e2e/Tab.spec.mjs b/packages/lexical-playground/__tests__/e2e/Tab.spec.mjs index abbfd5c316c..118e39536d4 100644 --- a/packages/lexical-playground/__tests__/e2e/Tab.spec.mjs +++ b/packages/lexical-playground/__tests__/e2e/Tab.spec.mjs @@ -8,9 +8,11 @@ import { assertHTML, + assertSelection, focusEditor, html, initialize, + keyDownCtrlOrMeta, test, } from '../utils/index.mjs'; @@ -114,5 +116,32 @@ test.describe('Tab', () => { `, ); }); + + test('can go to start of line after a tab character', async ({ + page, + isPlainText, + }) => { + test.skip(isPlainText); + await focusEditor(page); + + await page.keyboard.type('Foo'); + await page.keyboard.press('Tab'); + + page.on('pageerror', (error) => { + throw new Error(`Uncaught exception: ${error.message}`); + }); + + // Press ctrl + left arrow key to go to start of line + await keyDownCtrlOrMeta(page); + await page.keyboard.press('ArrowLeft'); + + // ensure cursor is now at beginning of line + await assertSelection(page, { + anchorOffset: 0, + anchorPath: [0, 0, 0], + focusOffset: 0, + focusPath: [0, 0, 0], + }); + }); }); /* eslint-enable sort-keys-fix/sort-keys-fix */