Skip to content

The design language that makes your AI harness better at design.

JavaScript

Fix: live picker inputs submit on the Enter that commits an IME candidate (#856) (#917)

Abdul Wahabe1db0f31e034
diff --git a/skill/scripts/live-browser.js b/skill/scripts/live-browser.js
index 8a0318ccb..c4d229aff 100644
--- a/skill/scripts/live-browser.js
+++ b/skill/scripts/live-browser.js
@@ -868,8 +868,15 @@
setTimeout(() => input.focus(), 0);
}
+ // The Enter that commits an IME candidate (Zhuyin, Pinyin, kana, Hangul) is
+ // not a submit. Safari fires that keydown after compositionend, so
+ // isComposing is already false there and keyCode 229 is the only signal.
+ function isImeKeydown(e) {
+ return e.isComposing || e.keyCode === 229;
+ }
+
function onAnnotInputKey(e) {
- if (e.key === 'Enter') {
+ if (e.key === 'Enter' && !isImeKeydown(e)) {
e.preventDefault(); e.stopPropagation();
finalizeEditingPin();
} else if (e.key === 'Escape') {
@@ -2463,7 +2470,7 @@
input.addEventListener('focus', () => syncConfigureInputChrome());
input.addEventListener('blur', () => syncConfigureInputChrome());
input.addEventListener('keydown', (e) => {
- if (e.key === 'Enter') { e.stopPropagation(); e.preventDefault(); handleGo(); return; }
+ if (e.key === 'Enter' && !isImeKeydown(e)) { e.stopPropagation(); e.preventDefault(); handleGo(); return; }
if (e.key === 'Escape') {
e.stopPropagation();
e.preventDefault();
@@ -2549,7 +2556,7 @@
try { input.focus({ preventScroll: true }); } catch { input.focus(); }
});
input.addEventListener('keydown', (e) => {
- if (e.key === 'Enter') {
+ if (e.key === 'Enter' && !isImeKeydown(e)) {
e.stopPropagation(); e.preventDefault();
if (isInsertCreateEnabled()) handleInsertCreate();
return;
@@ -11441,7 +11448,7 @@ void main() {
}
return;
}
- if (e.key === 'Enter') {
+ if (e.key === 'Enter' && !isImeKeydown(e)) {
e.preventDefault();
submitSteerMessage();
}
diff --git a/tests/live-browser-source.test.mjs b/tests/live-browser-source.test.mjs
index c38ccacdb..546301a55 100644
--- a/tests/live-browser-source.test.mjs
+++ b/tests/live-browser-source.test.mjs
@@ -1187,4 +1187,34 @@ describe('live-browser source contracts', () => {
'done should source-inject via the 750ms fallback for harnesses without HMR',
);
});
+
+ it('never submits on the Enter that commits an IME candidate (#856)', () => {
+ const imeSrc = SOURCE.match(/function isImeKeydown\(e\) \{[\s\S]*?\n \}/)?.[0];
+ const annotSrc = SOURCE.match(/function onAnnotInputKey\(e\) \{[\s\S]*?\n \}/)?.[0];
+ const calls = [];
+ const onAnnotInputKey = runInNewContext(`${imeSrc}\n(${annotSrc})`, {
+ finalizeEditingPin: () => calls.push('finalize'),
+ });
+ const press = (isComposing, keyCode) => {
+ const ev = {
+ key: 'Enter', isComposing, keyCode, prevented: false,
+ preventDefault() { this.prevented = true; }, stopPropagation() {},
+ };
+ onAnnotInputKey(ev);
+ return ev;
+ };
+ assert.equal(press(true, 13).prevented, false, 'the IME must still receive the key');
+ // Safari fires the committing keydown after compositionend, so only keyCode says so.
+ press(false, 229);
+ assert.deepEqual(calls, []);
+ press(false, 13);
+ assert.deepEqual(calls, ['finalize']);
+
+ const handlers = SOURCE.match(/(?:input|pageChatInput)\.addEventListener\('keydown', \(e\) => \{[\s\S]*?\n \}\);/g);
+ assert.equal(handlers?.length, 3, 'configure, insert and steer inputs');
+ for (const handler of handlers) {
+ assert.match(handler, /e\.key === 'Enter' && !isImeKeydown\(e\)/);
+ assert.doesNotMatch(handler, /e\.key === 'Enter'(?! && !isImeKeydown\(e\))/);
+ }
+ });
});