From 4e7f755ae9ef9d8a5057a692b1c0a9425853321a Mon Sep 17 00:00:00 2001 From: JasonOA888 Date: Tue, 10 Mar 2026 12:00:47 +0800 Subject: [PATCH] fix(page-controller): address PR review feedback ## Changes 1. **Fix keyboard event semantics** (per review feedback) - Only dispatch keydown/keyup for single-character input - Avoids inconsistent event payloads for multi-character strings - Prevents confusion in editors that correlate key events with text changes 2. **Remove extra blank line** - Formatting consistency Reviewer noted that dispatching key events with only the last character of multi-character text creates semantic inconsistency with the actual DOM mutation (which inserts the full string at once). This fix follows the suggested change from the review. --- packages/page-controller/src/actions.ts | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/page-controller/src/actions.ts b/packages/page-controller/src/actions.ts index 8c2198f..7cbcfb0 100644 --- a/packages/page-controller/src/actions.ts +++ b/packages/page-controller/src/actions.ts @@ -140,20 +140,23 @@ export async function inputTextElement(element: HTMLElement, text: string) { // Dispatch input event (standard) editableElement.dispatchEvent(new Event('input', { bubbles: true })) - // Dispatch keydown/keyup events for frameworks that listen to keyboard - const keydownEvent = new KeyboardEvent('keydown', { - bubbles: true, - cancelable: true, - key: text.slice(-1), // Last character - }) - editableElement.dispatchEvent(keydownEvent) + // Dispatch keydown/keyup events for frameworks that listen to keyboard. + // To avoid inconsistent semantics, only do this for single-character input. + if (text.length === 1) { + const keydownEvent = new KeyboardEvent('keydown', { + bubbles: true, + cancelable: true, + key: text, + }) + editableElement.dispatchEvent(keydownEvent) - const keyupEvent = new KeyboardEvent('keyup', { - bubbles: true, - cancelable: true, - key: text.slice(-1), - }) - editableElement.dispatchEvent(keyupEvent) + const keyupEvent = new KeyboardEvent('keyup', { + bubbles: true, + cancelable: true, + key: text, + }) + editableElement.dispatchEvent(keyupEvent) + } // Dispatch change event (for good measure) editableElement.dispatchEvent(new Event('change', { bubbles: true }))