From 7a7e870c7001a14d84bdf94936a982e97f1b8cb5 Mon Sep 17 00:00:00 2001 From: Rias Van der Veken Date: Tue, 10 Nov 2026 15:34:45 +0000 Subject: [PATCH] Publish legacy field edits that scripts make without an input event Legacy scripts such as Behave.js (Simple Text) change a value on keydown and cancel the key, so no input event fires and the edit was neither autosaved nor saved with Cmd/Ctrl+S. Craft 5's element editor rechecked the form on keyup; legacy HTML controls now do too, publishing only when a value changed. --- .../cpcompat/src/legacy-html-control.js | 11 +++++-- .../resources/js/legacy-html-control.test.ts | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/craftcms-legacy/cpcompat/src/legacy-html-control.js b/packages/craftcms-legacy/cpcompat/src/legacy-html-control.js index bdb950fb5d7..1996a753c05 100644 --- a/packages/craftcms-legacy/cpcompat/src/legacy-html-control.js +++ b/packages/craftcms-legacy/cpcompat/src/legacy-html-control.js @@ -198,6 +198,9 @@ import { // Legacy inputs (e.g. element select inputs) announce changes with // jQuery's `.trigger('change')`, which native listeners never see. window.jQuery?.(this).on('change', this.handleLegacyChange); + // Legacy scripts (e.g. Behave.js) can edit a value on keydown without an + // `input` event. Craft 5's element editor picked those up on keyup. + this.addEventListener('keyup', this.handleLegacyChange); this.ready = this.mount(); } @@ -206,6 +209,7 @@ import { this.removeEventListener('input', this.handleInput); this.removeEventListener('change', this.handleInput); window.jQuery?.(this).off('change', this.handleLegacyChange); + this.removeEventListener('keyup', this.handleLegacyChange); this._form = null; this._runId++; this.dispose(); @@ -584,12 +588,15 @@ import { function isTypingEvent(event) { const target = event.target; + if (!['input', 'keyup'].includes(event.type)) { + return false; + } + if (target instanceof HTMLTextAreaElement) { - return event.type === 'input'; + return true; } return ( - event.type === 'input' && target instanceof HTMLInputElement && ['text', 'email', 'url', 'tel', 'password', 'search'].includes( target.type diff --git a/yii2-adapter/resources/js/legacy-html-control.test.ts b/yii2-adapter/resources/js/legacy-html-control.test.ts index 4ba4e975eb9..ff7bd5cb323 100644 --- a/yii2-adapter/resources/js/legacy-html-control.test.ts +++ b/yii2-adapter/resources/js/legacy-html-control.test.ts @@ -283,6 +283,36 @@ describe('Legacy HTML UI Control', () => { expect(mutation.value).toEqual({fields: {keywords: ['5', '6']}}); }); + it('publishes edits legacy scripts make on keydown without an input event', async () => { + const {container, mutation} = await mount( + { + html: '', + headHtml: '', + bodyHtml: '', + }, + { + path: ['fields', 'notes'], + values: {fields: {notes: {'fields[notes]': 'Hello'}}}, + } + ); + await container.querySelector}>( + 'craft-legacy-html-control' + )!.ready; + const textarea = container.querySelector('textarea')!; + + // A key that changes nothing, such as an arrow key + textarea.dispatchEvent(new KeyboardEvent('keyup', {bubbles: true})); + await nextTick(); + expect(mutation.value).toEqual({}); + + // What Behave.js does on Backspace: it sets the value itself + textarea.value = 'Hell'; + textarea.dispatchEvent(new KeyboardEvent('keyup', {bubbles: true})); + await nextTick(); + + expect(mutation.value).toEqual({fields: {notes: 'Hell'}}); + }); + it('restores list inputs from an already-expanded value', async () => { // After an autosave, the island remounts with the value it last published // (expanded), not the server's flat input names.