From 7ce43aaf38b4c24a7d202dafcc42a535ecd6d85a Mon Sep 17 00:00:00 2001 From: Positiveoo1 Date: Fri, 3 Jul 2026 20:00:00 +0200 Subject: [PATCH 1/2] fix caret error on incorrect final word --- frontend/src/ts/elements/caret.ts | 13 +++++++------ frontend/src/ts/test/test-ui.ts | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/ts/elements/caret.ts b/frontend/src/ts/elements/caret.ts index 8d54c3f3d4dc..801ad70615cf 100644 --- a/frontend/src/ts/elements/caret.ts +++ b/frontend/src/ts/elements/caret.ts @@ -395,12 +395,13 @@ export class Caret { isDirectionReversed: boolean; }): { left: number; top: number; width: number } { const letters = options.word?.qsa("letter"); - let letter; - if (!letters?.length || !(letter = letters[options.letterIndex])) { - // maybe we should return null here instead of throwing - throw new Error( - "Caret getTargetPositionAndWidth: no letters found in word", - ); + let letter = letters?.[options.letterIndex] ?? letters?.at(-1); + if (letter === undefined) { + return { + left: options.word.getOffsetLeft(), + top: options.word.getOffsetTop(), + width: getTotalInlineMargin(options.word.native), + }; } if (caretDebug) { diff --git a/frontend/src/ts/test/test-ui.ts b/frontend/src/ts/test/test-ui.ts index cbfed884ea3f..c12691104036 100644 --- a/frontend/src/ts/test/test-ui.ts +++ b/frontend/src/ts/test/test-ui.ts @@ -1841,6 +1841,7 @@ export function beforeTestWordChange( if ( (Config.stopOnError === "letter" && (correct || correct === null)) || nospaceEnabled || + correct === false || forceUpdateActiveWordLetters ) { void updateWordLetters({ From e461a91432ebead6b84c71751aeba6e474abb4e3 Mon Sep 17 00:00:00 2001 From: Positiveoo1 Date: Sat, 4 Jul 2026 06:57:20 +0200 Subject: [PATCH 2/2] fix: address caret review --- frontend/src/ts/elements/caret.ts | 20 +++++++++++++------- frontend/src/ts/test/test-ui.ts | 1 - 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/frontend/src/ts/elements/caret.ts b/frontend/src/ts/elements/caret.ts index 801ad70615cf..2b0786fe1964 100644 --- a/frontend/src/ts/elements/caret.ts +++ b/frontend/src/ts/elements/caret.ts @@ -395,13 +395,19 @@ export class Caret { isDirectionReversed: boolean; }): { left: number; top: number; width: number } { const letters = options.word?.qsa("letter"); - let letter = letters?.[options.letterIndex] ?? letters?.at(-1); - if (letter === undefined) { - return { - left: options.word.getOffsetLeft(), - top: options.word.getOffsetTop(), - width: getTotalInlineMargin(options.word.native), - }; + + if (letters.length === 0) { + throw new Error( + "Caret getTargetPositionAndWidth: no letters found in word", + ); + } + + let letter = letters[options.letterIndex] ?? letters[letters.length - 1]; + + if (!letter) { + throw new Error( + `Caret getTargetPositionAndWidth: letter not found for index ${options.letterIndex}`, + ); } if (caretDebug) { diff --git a/frontend/src/ts/test/test-ui.ts b/frontend/src/ts/test/test-ui.ts index c12691104036..cbfed884ea3f 100644 --- a/frontend/src/ts/test/test-ui.ts +++ b/frontend/src/ts/test/test-ui.ts @@ -1841,7 +1841,6 @@ export function beforeTestWordChange( if ( (Config.stopOnError === "letter" && (correct || correct === null)) || nospaceEnabled || - correct === false || forceUpdateActiveWordLetters ) { void updateWordLetters({