You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] multiline splits emoji and other astral characters (corrupt value, Enter twice stops submitting) #640
Package Version: @clack/prompts 1.9.0, @clack/core 1.6.0, same on main (5f27460)
Describe the bug
In a multiline prompt, typing an emoji (or any other character that JavaScript stores as a surrogate pair) leaves the cursor between its two halves. After that:
the emoji is drawn as two �, because the cursor highlight covers one half of it;
the next typed character is inserted inside the emoji, so the returned string contains lone surrogates;
pressing Enter twice no longer submits, because the cursor never reaches the end of the input;
Backspace and Delete remove half of the emoji, and Left and Right stop in the middle of it.
Node delivers such a character as one keypress whose char has length 2, but the key handler advances the cursor by one after inserting it (multi-line.ts#L139-L145). Backspace, Delete, the arrow keys and userInputWithCursor also work one UTF-16 code unit at a time. Typing in text is fine, since readline tracks the cursor there.
To Reproduce
// repro.mjs, with "@clack/prompts": "1.9.0"import{multiline}from'@clack/prompts';constvalue=awaitmultiline({message: 'Notes',showSubmit: true});console.log(JSON.stringify(value),value.isWellFormed());
Run node repro.mjs.
Type 👍, then a, press Tab, then Enter.
It prints "\ud83da\udc4d" false.
More sequences, fed to the prompt as the raw bytes a terminal sends (👍 is f0 9f 91 8d):
Keys
Returned
👍, Enter, Enter
nothing, the prompt stays open and keeps adding newlines
👍, Backspace, Tab, Enter (showSubmit, defaultValue: 'none')
"\udc4d"
👍, Left, Delete, Tab, Enter (showSubmit, defaultValue: 'none')
"\udc4d"
a, 👍, b, Left ×3, x, Tab, Enter (showSubmit)
"xa\ud83db\udc4d"
Expected behavior
"👍a" true. An emoji counts as one character when typing after it, deleting it and moving the cursor over it, and Enter twice submits as usual.
Additional Information
This is only about surrogate pairs, which is where the value gets corrupted. Joined emoji such as 👨👩👧 arrive as several keypresses and would still be stepped through one part at a time.
I have a fix with tests and will link the PR here.
Activity