diff --git a/e2e/get-title-and-content.spec.js b/e2e/get-title-and-content.spec.js index 0899d990e..723f11562 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 paragraph
', + '', +].join( '\n' ); + test.describe( 'getTitleAndContent', () => { test( 'returns correct title and content before any edits', async ( { page, @@ -28,6 +40,192 @@ test.describe( 'getTitleAndContent', () => { expect( result.changed ).toBe( false ); } ); + test( 'reports no change for untouched blocks migrated on parse', async ( { + page, + } ) => { + const editor = new EditorPage( page ); + await editor.setup( { + 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 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( isDirty ).toBe( true ); + expect( result.content ).not.toBe( MIGRATED_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: + '\nHello
\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( '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: + '\nHello
\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(), + '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 ); + } ); + + 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 9713f5371..3ddf52196 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,132 @@ 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 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 = '\nHost
\n'; diff --git a/src/components/editor/use-host-bridge.js b/src/components/editor/use-host-bridge.js index 9f8f7bd46..78ba27522 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 ); @@ -29,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. @@ -39,6 +47,7 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { normalizeAttribute( getEditedPostContent() || post.content ) ) ); + initialContentEditRef.current = getContentEdit(); } useEffect( () => { @@ -68,14 +77,24 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { getEditedPostAttribute( 'title' ) ); const content = normalizeAttribute( getEditedPostContent() ); - const changed = - title !== postTitleRef.current || - content !== postContentRef.current; + // 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 = + contentEdited && content !== postContentRef.current; + const changed = title !== postTitleRef.current || contentChanged; if ( changed ) { postTitleRef.current = title; postContentRef.current = content; } + if ( contentChanged ) { + reportedContentChangeRef.current = true; + } return { title, content, changed }; }; @@ -201,6 +220,7 @@ export function useHostBridge( post, editorRef, markBridgeReady ) { markBridgeReady, getEditedPostAttribute, getEditedPostContent, + getContentEdit, redo, switchEditorMode, undo,