From f864c19af05f6f7dd17484d4a90f7ca09ff30f6c Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Mon, 30 Nov 2026 13:50:08 -0500 Subject: [PATCH 1/4] fix: ignore load-time content changes in getTitleAndContent Untouched posts reported changed: true when core migrated a block while parsing or a block wrote non-persistent attributes on mount. Count a content change only when core replaced the content edit, which it does for every persistent change. Co-Authored-By: Claude Opus 5.5 --- e2e/get-title-and-content.spec.js | 67 ++++++++++++++ .../editor/test/use-host-bridge.test.jsx | 89 ++++++++++++++++++- src/components/editor/use-host-bridge.js | 23 ++++- 3 files changed, 176 insertions(+), 3 deletions(-) diff --git a/e2e/get-title-and-content.spec.js b/e2e/get-title-and-content.spec.js index 0899d990e..26b154730 100644 --- a/e2e/get-title-and-content.spec.js +++ b/e2e/get-title-and-content.spec.js @@ -28,6 +28,73 @@ test.describe( 'getTitleAndContent', () => { expect( result.changed ).toBe( false ); } ); + test( 'reports no change for untouched blocks migrated on parse', async ( { + page, + } ) => { + // Core migrates these legacy alignment attributes when parsing, so the + // serialized blocks differ from the stored markup. + const content = [ + '', + '

Centered heading

', + '', + '', + '', + '

Centered paragraph

', + '', + ].join( '\n' ); + const editor = new EditorPage( page ); + await editor.setup( { + post: { id: 1, type: 'post', status: 'draft', title: '', content }, + } ); + + const result = await editor.getTitleAndContent(); + + expect( result.content ).toBe( content ); + expect( result.changed ).toBe( false ); + } ); + + test( 'reports no change for non-persistent block updates', async ( { + page, + } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: + '\n

Hello

\n', + }, + } ); + + // Mirrors blocks that cache derived attributes on mount. + await page.evaluate( () => { + const blockEditor = window.wp.data.dispatch( 'core/block-editor' ); + const [ clientId ] = window.wp.data + .select( 'core/block-editor' ) + .getBlockOrder(); + blockEditor.__unstableMarkNextChangeAsNotPersistent(); + blockEditor.updateBlockAttributes( clientId, { + className: 'is-cached', + } ); + } ); + + const untouched = await editor.getTitleAndContent(); + expect( untouched.content ).toContain( 'is-cached' ); + expect( untouched.changed ).toBe( false ); + + await page + .getByRole( 'document', { name: /Block: Paragraph/ } ) + .click(); + await page.keyboard.press( 'End' ); + await page.keyboard.type( ' world' ); + + const edited = await editor.getTitleAndContent(); + expect( edited.content ).toContain( 'Hello world' ); + expect( edited.changed ).toBe( true ); + } ); + test( 'returns plain strings after editing the title', async ( { page, } ) => { diff --git a/src/components/editor/test/use-host-bridge.test.jsx b/src/components/editor/test/use-host-bridge.test.jsx index 9713f5371..d1a56a8da 100644 --- a/src/components/editor/test/use-host-bridge.test.jsx +++ b/src/components/editor/test/use-host-bridge.test.jsx @@ -5,6 +5,7 @@ import { useHostBridge } from '../use-host-bridge'; const mockGetEditedPostAttribute = vi.fn(); const mockGetEditedPostContent = vi.fn(); +const mockGetEntityRecordEdits = vi.fn(); const mockGetSelectedBlockClientId = vi.fn(); const mockGetBlock = vi.fn(); const mockGetSelectionStart = vi.fn(); @@ -20,6 +21,9 @@ vi.mock( '@wordpress/data', () => ( { getEditedPostContent: mockGetEditedPostContent, }; } + if ( store?.name === 'core' ) { + return { getEntityRecordEdits: mockGetEntityRecordEdits }; + } // block-editor store selectors return { getSelectedBlockClientId: mockGetSelectedBlockClientId, @@ -37,7 +41,7 @@ vi.mock( '@wordpress/data', () => ( { selectionChange: mockSelectionChange, } ), } ) ); -vi.mock( '@wordpress/core-data' ); +vi.mock( '@wordpress/core-data', () => ( { store: { name: 'core' } } ) ); vi.mock( '@wordpress/editor' ); vi.mock( '@wordpress/blocks' ); vi.mock( '@wordpress/rich-text', () => ( { @@ -72,6 +76,11 @@ describe( 'useHostBridge', () => { beforeEach( () => { vi.clearAllMocks(); + // Default to a new persistent edit on every read, so tests that only + // vary the content exercise the markup comparison. + mockGetEntityRecordEdits.mockImplementation( () => ( { + content: () => {}, + } ) ); editorRef = { current: document.createElement( 'div' ) }; markBridgeReady = vi.fn(); // Reset window.editor to initial state (matches the module-level @@ -270,6 +279,84 @@ describe( 'useHostBridge', () => { } ); } ); + it( 'getTitleAndContent ignores blocks migrated while parsing', () => { + // The baseline serializes the migrated blocks, but without an edit + // the editor still returns the stored markup. + parse.mockImplementationOnce( () => 'Migrated' ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEntityRecordEdits.mockReturnValue( {} ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValue( 'Stored' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + expect( window.editor.getTitleAndContent() ).toEqual( { + title: '', + content: 'Stored', + changed: false, + } ); + } ); + + it( 'getTitleAndContent ignores non-persistent changes', () => { + parse.mockImplementationOnce( ( value ) => value ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValueOnce( 'Original' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + // A block wrote an attribute on mount without replacing the edit. + mockGetEditedPostContent.mockReturnValue( 'Original with cache' ); + + expect( window.editor.getTitleAndContent().changed ).toBe( false ); + } ); + + it( 'getTitleAndContent reports a persistent change after non-persistent ones', () => { + parse.mockImplementationOnce( ( value ) => value ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEntityRecordEdits.mockReturnValue( {} ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValueOnce( 'Original' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + mockGetEditedPostContent.mockReturnValue( 'Original with cache' ); + expect( window.editor.getTitleAndContent().changed ).toBe( false ); + + mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } ); + mockGetEditedPostContent.mockReturnValue( 'Edited with cache' ); + expect( window.editor.getTitleAndContent() ).toEqual( { + title: '', + content: 'Edited with cache', + changed: true, + } ); + + // The baseline advanced, so the same state reports no further change. + expect( window.editor.getTitleAndContent().changed ).toBe( false ); + } ); + + it( 'getTitleAndContent reports no change when an edit restores the baseline', () => { + parse.mockImplementationOnce( ( value ) => value ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValue( 'Original' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + // The default mock replaces the edit, as undo does, but the markup + // matches the baseline. + expect( window.editor.getTitleAndContent().changed ).toBe( false ); + } ); + it( "getTitleAndContent starts from the host's content when the editor has none", () => { const content = '\n

Host

\n'; diff --git a/src/components/editor/use-host-bridge.js b/src/components/editor/use-host-bridge.js index 9f8f7bd46..59cfbae9a 100644 --- a/src/components/editor/use-host-bridge.js +++ b/src/components/editor/use-host-bridge.js @@ -11,6 +11,7 @@ window.editor = window.editor || {}; export function useHostBridge( post, editorRef, markBridgeReady ) { const { editEntityRecord } = useDispatch( coreStore ); + const { getEntityRecordEdits } = useSelect( coreStore ); const { undo, redo, switchEditorMode } = useDispatch( editorStore ); const { getEditedPostAttribute, getEditedPostContent } = useSelect( editorStore ); @@ -41,6 +42,20 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { ); } + // Core replaces the `content` edit for every persistent change and never + // for non-persistent ones, nor for blocks it migrates while parsing. Content + // that differs from the baseline without a new edit isn't a user change. + const getContentEdit = useCallback( + () => + getEntityRecordEdits( 'postType', post.type, post.id )?.content ?? + null, + [ getEntityRecordEdits, post.type, post.id ] + ); + const contentEditRef = useRef( undefined ); + if ( contentEditRef.current === undefined ) { + contentEditRef.current = getContentEdit(); + } + useEffect( () => { window.editor.setContent = ( content ) => { editContent( { content: decodeURIComponent( content ) } ); @@ -68,13 +83,16 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { getEditedPostAttribute( 'title' ) ); const content = normalizeAttribute( getEditedPostContent() ); - const changed = - title !== postTitleRef.current || + const contentEdit = getContentEdit(); + const contentChanged = + contentEdit !== contentEditRef.current && content !== postContentRef.current; + const changed = title !== postTitleRef.current || contentChanged; if ( changed ) { postTitleRef.current = title; postContentRef.current = content; + contentEditRef.current = contentEdit; } return { title, content, changed }; @@ -201,6 +219,7 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { markBridgeReady, getEditedPostAttribute, getEditedPostContent, + getContentEdit, redo, switchEditorMode, undo, From 505b9ae75fc0fd03f50c2e628ec94c1df153d7cd Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Mon, 30 Nov 2026 14:39:56 -0500 Subject: [PATCH 2/4] test: cover edits that cancel out on migrated blocks Guards the serialized baseline: seeding it with the raw stored markup would report these edits as a change. Co-Authored-By: Claude Opus 5.5 --- e2e/get-title-and-content.spec.js | 67 +++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/e2e/get-title-and-content.spec.js b/e2e/get-title-and-content.spec.js index 26b154730..797b59334 100644 --- a/e2e/get-title-and-content.spec.js +++ b/e2e/get-title-and-content.spec.js @@ -1,6 +1,18 @@ import { test, expect } from '@playwright/test'; import EditorPage from './editor-page'; +// Core migrates these legacy alignment attributes when parsing, so the +// serialized blocks differ from the stored markup. +const MIGRATED_CONTENT = [ + '', + '

Centered heading

', + '', + '', + '', + '

Centered paragraph

', + '', +].join( '\n' ); + test.describe( 'getTitleAndContent', () => { test( 'returns correct title and content before any edits', async ( { page, @@ -31,25 +43,54 @@ test.describe( 'getTitleAndContent', () => { test( 'reports no change for untouched blocks migrated on parse', async ( { page, } ) => { - // Core migrates these legacy alignment attributes when parsing, so the - // serialized blocks differ from the stored markup. - const content = [ - '', - '

Centered heading

', - '', - '', - '', - '

Centered paragraph

', - '', - ].join( '\n' ); const editor = new EditorPage( page ); await editor.setup( { - post: { id: 1, type: 'post', status: 'draft', title: '', content }, + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: MIGRATED_CONTENT, + }, + } ); + + const result = await editor.getTitleAndContent(); + + expect( result.content ).toBe( MIGRATED_CONTENT ); + expect( result.changed ).toBe( false ); + } ); + + test( 'reports no change for edits that cancel out on migrated blocks', async ( { + page, + } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: MIGRATED_CONTENT, + }, } ); + await page.getByText( 'Centered paragraph' ).click(); + await page.keyboard.press( 'End' ); + await page.keyboard.type( 'x' ); + await page.keyboard.press( 'Backspace' ); + + const contentEdit = await page.evaluate( + () => + typeof window.wp.data + .select( 'core' ) + .getEntityRecordEdits( 'postType', 'post', 1 )?.content + ); const result = await editor.getTitleAndContent(); - expect( result.content ).toBe( content ); + // The edits are real and the content is now the migrated markup, so + // only a serialized baseline recognizes it as unchanged. + expect( contentEdit ).toBe( 'function' ); + expect( result.content ).not.toBe( MIGRATED_CONTENT ); expect( result.changed ).toBe( false ); } ); From c1d7bc347075dbaa9d1a23be3c61814a7ce92143 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Mon, 30 Nov 2026 16:15:40 -0500 Subject: [PATCH 3/4] fix: report typing that continues after a host read Core keeps one content edit for a run of typing, so comparing the edit with the one seen at the last read hid keystrokes typed after it. Compare with the edit at open instead, and keep comparing markup once the host has received edited content, so undoing back to the start is reported. Co-Authored-By: Claude Opus 5.5 --- e2e/get-title-and-content.spec.js | 90 +++++++++++++++++++ .../editor/test/use-host-bridge.test.jsx | 52 ++++++++++- src/components/editor/use-host-bridge.js | 37 ++++---- 3 files changed, 159 insertions(+), 20 deletions(-) diff --git a/e2e/get-title-and-content.spec.js b/e2e/get-title-and-content.spec.js index 797b59334..5e25c7380 100644 --- a/e2e/get-title-and-content.spec.js +++ b/e2e/get-title-and-content.spec.js @@ -136,6 +136,96 @@ test.describe( 'getTitleAndContent', () => { expect( edited.changed ).toBe( true ); } ); + test( 'reports typing that continues after a read', async ( { page } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: + '\n

Hello

\n', + }, + } ); + const isSameContentEdit = () => + page.evaluate( () => { + const edit = window.wp.data + .select( 'core' ) + .getEntityRecordEdits( 'postType', 'post', 1 )?.content; + const same = edit === window.__lastContentEdit; + window.__lastContentEdit = edit; + return same; + } ); + + await page + .getByRole( 'document', { name: /Block: Paragraph/ } ) + .click(); + await page.keyboard.press( 'End' ); + await page.keyboard.type( ' wo' ); + await isSameContentEdit(); + const first = await editor.getTitleAndContent(); + await page.keyboard.type( 'rld' ); + const second = await editor.getTitleAndContent(); + + // Core keeps one edit for a run of typing in the same field. + expect( await isSameContentEdit() ).toBe( true ); + expect( first.changed ).toBe( true ); + expect( second.content ).toContain( 'Hello world' ); + expect( second.changed ).toBe( true ); + } ); + + test( 'reports undoing to the opened content after a read', async ( { + page, + } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: MIGRATED_CONTENT, + }, + } ); + + await page.getByText( 'Centered paragraph' ).click(); + await page.keyboard.press( 'End' ); + await page.keyboard.type( 'x' ); + const edited = await editor.getTitleAndContent(); + await page.evaluate( () => window.editor.undo() ); + const undone = await editor.getTitleAndContent(); + + // The host holds the edit, so it must get the opened content back. + expect( edited.changed ).toBe( true ); + expect( undone.content ).toBe( MIGRATED_CONTENT ); + expect( undone.changed ).toBe( true ); + } ); + + test( 'reports no change for undoing to the opened content without a read', async ( { + page, + } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + post: { + id: 1, + type: 'post', + status: 'draft', + title: '', + content: MIGRATED_CONTENT, + }, + } ); + + await page.getByText( 'Centered paragraph' ).click(); + await page.keyboard.press( 'End' ); + await page.keyboard.type( 'x' ); + await page.evaluate( () => window.editor.undo() ); + const result = await editor.getTitleAndContent(); + + expect( result.content ).toBe( MIGRATED_CONTENT ); + expect( result.changed ).toBe( false ); + } ); + test( 'returns plain strings after editing the title', async ( { page, } ) => { diff --git a/src/components/editor/test/use-host-bridge.test.jsx b/src/components/editor/test/use-host-bridge.test.jsx index d1a56a8da..3ddf52196 100644 --- a/src/components/editor/test/use-host-bridge.test.jsx +++ b/src/components/editor/test/use-host-bridge.test.jsx @@ -352,11 +352,59 @@ describe( 'useHostBridge', () => { useHostBridge( defaultPost, editorRef, markBridgeReady ) ); - // The default mock replaces the edit, as undo does, but the markup - // matches the baseline. + // The default mock reports an edit, but the markup matches the baseline. expect( window.editor.getTitleAndContent().changed ).toBe( false ); } ); + it( 'getTitleAndContent reports typing that continues after a read', () => { + parse.mockImplementationOnce( ( value ) => value ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEntityRecordEdits.mockReturnValue( {} ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValueOnce( 'Hello' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + // Core keeps one edit for a run of typing in the same field. + mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } ); + mockGetEditedPostContent.mockReturnValue( 'Hello wo' ); + expect( window.editor.getTitleAndContent().changed ).toBe( true ); + + mockGetEditedPostContent.mockReturnValue( 'Hello world' ); + expect( window.editor.getTitleAndContent() ).toEqual( { + title: '', + content: 'Hello world', + changed: true, + } ); + } ); + + it( 'getTitleAndContent reports undoing to the opened content after a read', () => { + parse.mockImplementationOnce( () => 'Migrated' ); + serialize.mockImplementationOnce( ( blocks ) => blocks ); + mockGetEntityRecordEdits.mockReturnValue( {} ); + mockGetEditedPostAttribute.mockReturnValue( '' ); + mockGetEditedPostContent.mockReturnValueOnce( 'Stored' ); + + renderHook( () => + useHostBridge( defaultPost, editorRef, markBridgeReady ) + ); + + mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } ); + mockGetEditedPostContent.mockReturnValue( 'Edited' ); + expect( window.editor.getTitleAndContent().changed ).toBe( true ); + + // Undo removes the edit, so the host must get the opened content back. + mockGetEntityRecordEdits.mockReturnValue( {} ); + mockGetEditedPostContent.mockReturnValue( 'Stored' ); + expect( window.editor.getTitleAndContent() ).toEqual( { + title: '', + content: 'Stored', + changed: true, + } ); + } ); + it( "getTitleAndContent starts from the host's content when the editor has none", () => { const content = '\n

Host

\n'; diff --git a/src/components/editor/use-host-bridge.js b/src/components/editor/use-host-bridge.js index 59cfbae9a..78ba27522 100644 --- a/src/components/editor/use-host-bridge.js +++ b/src/components/editor/use-host-bridge.js @@ -30,8 +30,15 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { [ editEntityRecord, post.id, post.type ] ); + const getContentEdit = useCallback( + () => getEntityRecordEdits( 'postType', post.type, post.id )?.content, + [ getEntityRecordEdits, post.type, post.id ] + ); + const postTitleRef = useRef( normalizeAttribute( post.title ) ); const postContentRef = useRef( null ); + const initialContentEditRef = useRef(); + const reportedContentChangeRef = useRef( false ); if ( postContentRef.current === null ) { // Start from the content the editor set up, which includes the template // applied to a new post, so an untouched post doesn't report a change. @@ -40,20 +47,7 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { normalizeAttribute( getEditedPostContent() || post.content ) ) ); - } - - // Core replaces the `content` edit for every persistent change and never - // for non-persistent ones, nor for blocks it migrates while parsing. Content - // that differs from the baseline without a new edit isn't a user change. - const getContentEdit = useCallback( - () => - getEntityRecordEdits( 'postType', post.type, post.id )?.content ?? - null, - [ getEntityRecordEdits, post.type, post.id ] - ); - const contentEditRef = useRef( undefined ); - if ( contentEditRef.current === undefined ) { - contentEditRef.current = getContentEdit(); + initialContentEditRef.current = getContentEdit(); } useEffect( () => { @@ -83,16 +77,23 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { getEditedPostAttribute( 'title' ) ); const content = normalizeAttribute( getEditedPostContent() ); - const contentEdit = getContentEdit(); + // Core replaces the `content` edit only for persistent changes (a run + // of typing shares one), never for blocks migrated while parsing or + // non-persistent writes. So markup differences count only once the + // edit differs from the one at open or the host holds edited content. + const contentEdited = + getContentEdit() !== initialContentEditRef.current || + reportedContentChangeRef.current; const contentChanged = - contentEdit !== contentEditRef.current && - content !== postContentRef.current; + contentEdited && content !== postContentRef.current; const changed = title !== postTitleRef.current || contentChanged; if ( changed ) { postTitleRef.current = title; postContentRef.current = content; - contentEditRef.current = contentEdit; + } + if ( contentChanged ) { + reportedContentChangeRef.current = true; } return { title, content, changed }; From 3ffcc7776ffa178002c5c0b16dd7ba29df3331e8 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Mon, 30 Nov 2026 16:52:00 -0500 Subject: [PATCH 4/4] test: check core edit state through public behavior where possible Asserting the content edit's type tied a test to how core stores it; isEditedPostDirty() proves the same precondition. The same-edit check stays, labelled so a failure points at the test rather than the bridge. Co-Authored-By: Claude Opus 5.5 --- e2e/get-title-and-content.spec.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/e2e/get-title-and-content.spec.js b/e2e/get-title-and-content.spec.js index 5e25c7380..723f11562 100644 --- a/e2e/get-title-and-content.spec.js +++ b/e2e/get-title-and-content.spec.js @@ -79,17 +79,14 @@ test.describe( 'getTitleAndContent', () => { await page.keyboard.type( 'x' ); await page.keyboard.press( 'Backspace' ); - const contentEdit = await page.evaluate( - () => - typeof window.wp.data - .select( 'core' ) - .getEntityRecordEdits( 'postType', 'post', 1 )?.content + const isDirty = await page.evaluate( () => + window.wp.data.select( 'core/editor' ).isEditedPostDirty() ); const result = await editor.getTitleAndContent(); // The edits are real and the content is now the migrated markup, so // only a serialized baseline recognizes it as unchanged. - expect( contentEdit ).toBe( 'function' ); + expect( isDirty ).toBe( true ); expect( result.content ).not.toBe( MIGRATED_CONTENT ); expect( result.changed ).toBe( false ); } ); @@ -169,7 +166,10 @@ test.describe( 'getTitleAndContent', () => { const second = await editor.getTitleAndContent(); // Core keeps one edit for a run of typing in the same field. - expect( await isSameContentEdit() ).toBe( true ); + expect( + await isSameContentEdit(), + 'Precondition: core no longer keeps one edit for a run of typing, so revisit this test rather than the bridge' + ).toBe( true ); expect( first.changed ).toBe( true ); expect( second.content ).toContain( 'Hello world' ); expect( second.changed ).toBe( true );