@@ -6,6 +6,9 @@ import { setLastInsertCompositionTextData } from "../state";
66import * as CompositionDisplay from "../../elements/composition-display" ;
77import { onInsertText } from "../handlers/insert-text" ;
88import * as TestUI from "../../test/test-ui" ;
9+ import * as TestWords from "../../test/test-words" ;
10+ import * as TestInput from "../../test/test-input" ;
11+ import * as Strings from "../../utils/strings" ;
912
1013const inputEl = getInputElement ( ) ;
1114
@@ -31,8 +34,22 @@ inputEl.addEventListener("compositionupdate", (event) => {
3134 } ) ;
3235
3336 if ( TestState . testRestarting || TestUI . resultCalculating ) return ;
34- CompositionState . setData ( event . data ) ;
35- CompositionDisplay . update ( event . data ) ;
37+ const currentWord = TestWords . words . getCurrent ( ) ;
38+ const typedSoFar = TestInput . input . current ;
39+ const remainingChars =
40+ Strings . splitIntoCharacters ( currentWord ) . length -
41+ Strings . splitIntoCharacters ( typedSoFar ) . length ;
42+ // Prevent rendering more composition glyphs than the word has remaining letters,
43+ // so IME preedit strings (e.g. romaji) don't push text to the next line.
44+ const limitedCompositionData =
45+ remainingChars > 0
46+ ? Strings . splitIntoCharacters ( event . data )
47+ . slice ( 0 , remainingChars )
48+ . join ( "" )
49+ : "" ;
50+
51+ CompositionState . setData ( limitedCompositionData ) ;
52+ CompositionDisplay . update ( limitedCompositionData ) ;
3653} ) ;
3754
3855inputEl . addEventListener ( "compositionend" , async ( event ) => {
0 commit comments