From da709b5fcb37b4ed6d69ff09ebc01bf236c057ca Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Tue, 17 Mar 2026 16:12:00 +0100 Subject: [PATCH 1/4] Stop keeping stale controlled blocks after reset (test) --- .../block-editor/src/store/test/reducer.js | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index bb91e1bd3a33c1..3022c8ac8a34d5 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -44,7 +44,7 @@ import { withDerivedBlockEditingModes, viewportModalClientIds, } from '../reducer'; - +import { getBlockOrder, getBlocks } from '../selectors'; import { unlock } from '../../lock-unlock'; import { sectionRootClientIdKey, isIsolatedEditorKey } from '.././private-keys'; @@ -2478,6 +2478,73 @@ describe( 'state', () => { expect( state.controlledInnerBlocks.chicken ).toBe( true ); } ); + it( 'should not leave stale controlled tree entries after root replacement and reset', () => { + const chickenBlock = { + clientId: 'chicken', + name: 'core/test-block', + attributes: {}, + innerBlocks: [], + }; + const eggBlock = { + clientId: 'egg', + name: 'core/test-block', + attributes: {}, + innerBlocks: [], + }; + + // Initialize content, simulates root useBlockSync initialization. + let state = blocks( undefined, { + type: 'RESET_BLOCKS', + blocks: [ chickenBlock ], + } ); + + // Give chicken a controlled child with two actions, simulates inner useBlockSync initialization. + state = blocks( state, { + type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', + clientId: 'chicken', + hasControlledInnerBlocks: true, + } ); + + state = blocks( state, { + type: 'REPLACE_INNER_BLOCKS', + rootClientId: 'chicken', + blocks: [ eggBlock ], + } ); + + // Reset blocks completely, simulates root useBlockSync cleanup. + state = blocks( state, { + type: 'RESET_BLOCKS', + blocks: [], + } ); + + // Unset controlled inner blocks, simulates inner useBlockSync cleanup. + state = blocks( state, { + type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', + clientId: 'chicken', + hasControlledInnerBlocks: false, + } ); + + // Initialize content again, simulates useBlockSync after navigation. + state = blocks( state, { + type: 'RESET_BLOCKS', + blocks: [ chickenBlock ], + } ); + + // Set controlled inner blocks again. + state = blocks( state, { + type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', + clientId: 'chicken', + hasControlledInnerBlocks: true, + } ); + + // At this point useInnerBlockTemplateSync would check if `getBlocks` is empty before applying the template. + const fullState = { blocks: state }; + expect( getBlockOrder( fullState, 'chicken' ) ).toEqual( + [] + ); + expect( getBlocks( fullState, 'chicken' ) ).toEqual( [] ); + } ); + it( 'should not create new state references when setting controlled inner blocks on a block with no inner blocks', () => { const original = blocks( undefined, { type: 'RESET_BLOCKS', From ea3f5c08666b865cf8758ff9fa3498bf53f0b94d Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 18 Mar 2026 10:52:27 +0100 Subject: [PATCH 2/4] Improve the test code to make clear what is it simulating --- .../block-editor/src/store/test/reducer.js | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 3022c8ac8a34d5..6e2e544875652e 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -2479,70 +2479,77 @@ describe( 'state', () => { } ); it( 'should not leave stale controlled tree entries after root replacement and reset', () => { - const chickenBlock = { - clientId: 'chicken', - name: 'core/test-block', + const templateBlock = { + clientId: 'template', + name: 'core/post-content', attributes: {}, innerBlocks: [], }; - const eggBlock = { - clientId: 'egg', - name: 'core/test-block', + const contentBlock = { + clientId: 'content', + name: 'core/paragraph', attributes: {}, innerBlocks: [], }; - // Initialize content, simulates root useBlockSync initialization. + // Initialize template, simulates root `useBlockSync` initialization. let state = blocks( undefined, { type: 'RESET_BLOCKS', - blocks: [ chickenBlock ], + blocks: [ templateBlock ], } ); - // Give chicken a controlled child with two actions, simulates inner useBlockSync initialization. + // Add a controlled child to the template using two actions, simulates inner `useBlockSync` initialization. state = blocks( state, { type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', - clientId: 'chicken', + clientId: 'template', hasControlledInnerBlocks: true, } ); state = blocks( state, { type: 'REPLACE_INNER_BLOCKS', - rootClientId: 'chicken', - blocks: [ eggBlock ], + rootClientId: 'template', + blocks: [ contentBlock ], } ); - // Reset blocks completely, simulates root useBlockSync cleanup. + // Reset blocks completely, simulates root `useBlockSync` cleanup. state = blocks( state, { type: 'RESET_BLOCKS', blocks: [], } ); - // Unset controlled inner blocks, simulates inner useBlockSync cleanup. + // Unset controlled inner blocks, simulates inner `useBlockSync` cleanup. state = blocks( state, { type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', - clientId: 'chicken', + clientId: 'template', hasControlledInnerBlocks: false, } ); - // Initialize content again, simulates useBlockSync after navigation. + // Initialize template again, simulates `useBlockSync` after navigation. state = blocks( state, { type: 'RESET_BLOCKS', - blocks: [ chickenBlock ], + blocks: [ templateBlock ], } ); - // Set controlled inner blocks again. + // Set controlled inner blocks again, this time to empty. state = blocks( state, { type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', - clientId: 'chicken', + clientId: 'template', hasControlledInnerBlocks: true, } ); - // At this point useInnerBlockTemplateSync would check if `getBlocks` is empty before applying the template. + state = blocks( state, { + type: 'REPLACE_INNER_BLOCKS', + rootClientId: 'template', + blocks: [], + } ); + + // At this point the template has empty content, and `useInnerBlockTemplateSync` should apply + // its template content. It will check if `getBlocks` is empty before applying the template. const fullState = { blocks: state }; - expect( getBlockOrder( fullState, 'chicken' ) ).toEqual( + expect( getBlockOrder( fullState, 'template' ) ).toEqual( [] ); - expect( getBlocks( fullState, 'chicken' ) ).toEqual( [] ); + expect( getBlocks( fullState, 'template' ) ).toEqual( [] ); } ); it( 'should not create new state references when setting controlled inner blocks on a block with no inner blocks', () => { From 940350c233116559a9d98a32944ace539b8ab9d8 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 18 Mar 2026 15:10:02 +0100 Subject: [PATCH 3/4] withBlockReset: don't copy over stale tree entries --- packages/block-editor/src/store/reducer.js | 37 ++++-- .../block-editor/src/store/test/reducer.js | 123 ++++++++++++++++++ 2 files changed, 147 insertions(+), 13 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index f4a90494080b6c..890d2271fc4cfb 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -607,20 +607,23 @@ const withBlockReset = ( reducer ) => ( state, action ) => { * flags are cleaned up naturally by unsetControlledBlocks() * when useBlockSync unmounts. */ - const preservedControlledInnerBlocks = - state?.controlledInnerBlocks ?? {}; const newState = { - ...state, byClientId: new Map( getFlattenedBlocksWithoutAttributes( action.blocks ) ), attributes: new Map( getFlattenedBlockAttributes( action.blocks ) ), order: mapBlockOrder( action.blocks ), parents: new Map( mapBlockParents( action.blocks ) ), - controlledInnerBlocks: preservedControlledInnerBlocks, + controlledInnerBlocks: {}, + tree: new Map(), }; + updateBlockTreeForBlocks( newState, action.blocks ); + + const preservedControlledInnerBlocks = + state?.controlledInnerBlocks ?? {}; + // Preserve controlled inner blocks data from the old state. // The maps above are rebuilt solely from action.blocks, but // controlled inner blocks live under cloned IDs that aren't @@ -637,23 +640,24 @@ const withBlockReset = ( reducer ) => ( state, action ) => { if ( ! newState.byClientId.has( clientId ) ) { continue; } + newState.controlledInnerBlocks[ clientId ] = true; const oldOrder = state.order.get( clientId ); if ( ! oldOrder?.length ) { continue; } newState.order.set( clientId, oldOrder ); const preserveBlock = ( blockId, parentId ) => { - const blockData = state.byClientId?.get( blockId ); + const blockData = state.byClientId.get( blockId ); if ( ! blockData ) { return; } newState.byClientId.set( blockId, blockData ); newState.attributes.set( blockId, - state.attributes?.get( blockId ) + state.attributes.get( blockId ) ); newState.parents.set( blockId, parentId ); - const childOrder = state.order?.get( blockId ) || []; + const childOrder = state.order.get( blockId ) || []; newState.order.set( blockId, childOrder ); childOrder.forEach( ( childId ) => preserveBlock( childId, blockId ) @@ -663,18 +667,15 @@ const withBlockReset = ( reducer ) => ( state, action ) => { } } - newState.tree = new Map( state?.tree ); - updateBlockTreeForBlocks( newState, action.blocks ); - // Fix tree entries for controlled blocks. updateBlockTreeForBlocks // built tree entries using action.blocks' inner block structure // (entity-level IDs), but we need them to reference the preserved // cloned inner blocks instead. Mutating the existing object // preserves references held by ancestor tree entries. for ( const clientId of Object.keys( - preservedControlledInnerBlocks + newState.controlledInnerBlocks ) ) { - if ( ! preservedControlledInnerBlocks[ clientId ] ) { + if ( ! newState.controlledInnerBlocks[ clientId ] ) { continue; } if ( ! newState.byClientId.has( clientId ) ) { @@ -685,13 +686,23 @@ const withBlockReset = ( reducer ) => ( state, action ) => { continue; } const innerBlocks = controlledOrder.map( ( id ) => - newState.tree.get( id ) + state.tree.get( id ) ); const existingEntry = newState.tree.get( clientId ); if ( existingEntry ) { existingEntry.innerBlocks = innerBlocks; } newState.tree.set( 'controlled||' + clientId, { innerBlocks } ); + const preserveTreeEntry = ( blockId ) => { + const treeEntry = state.tree.get( blockId ); + if ( ! treeEntry ) { + return; + } + newState.tree.set( blockId, treeEntry ); + const childOrder = newState.order.get( blockId ) || []; + childOrder.forEach( preserveTreeEntry ); + }; + controlledOrder.forEach( preserveTreeEntry ); } newState.tree.set( '', { diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 6e2e544875652e..d082fae4ccbcd8 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -2478,6 +2478,129 @@ describe( 'state', () => { expect( state.controlledInnerBlocks.chicken ).toBe( true ); } ); + it( 'should preserve controlledInnerBlocks blocks across RESET_BLOCKS', () => { + const original = blocks( undefined, { + type: 'RESET_BLOCKS', + blocks: [ + { + clientId: 'chicken', + name: 'core/test-block', + attributes: {}, + innerBlocks: [], + }, + ], + } ); + const withControlled = blocks( original, { + type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', + clientId: 'chicken', + hasControlledInnerBlocks: true, + } ); + + const withControlledContent = blocks( withControlled, { + type: 'REPLACE_INNER_BLOCKS', + rootClientId: 'chicken', + blocks: [ + { + clientId: 'content', + innerBlocks: [ + { + clientId: 'content-inner', + innerBlocks: [], + }, + ], + }, + ], + } ); + + expect( + getBlocks( + { blocks: withControlledContent }, + 'chicken' + ).map( ( b ) => b.clientId ) + ).toEqual( [ 'content' ] ); + expect( + getBlocks( + { blocks: withControlledContent }, + 'content' + ).map( ( b ) => b.clientId ) + ).toEqual( [ 'content-inner' ] ); + + const state = blocks( withControlledContent, { + type: 'RESET_BLOCKS', + blocks: [ + { + clientId: 'chicken', + name: 'core/test-block', + attributes: {}, + innerBlocks: [], + }, + ], + } ); + + expect( state.controlledInnerBlocks.chicken ).toBe( true ); + expect( + getBlocks( { blocks: state }, 'chicken' ).map( + ( b ) => b.clientId + ) + ).toEqual( [ 'content' ] ); + expect( + getBlocks( { blocks: state }, 'content' ).map( + ( b ) => b.clientId + ) + ).toEqual( [ 'content-inner' ] ); + } ); + + it( 'should forget controlledInnerBlocks during full RESET_BLOCKS', () => { + const templateBlock = { + clientId: 'template', + name: 'core/post-content', + attributes: {}, + innerBlocks: [], + }; + const contentBlock = { + clientId: 'content', + name: 'core/paragraph', + attributes: {}, + innerBlocks: [], + }; + + let state = blocks( undefined, { + type: 'RESET_BLOCKS', + blocks: [ templateBlock ], + } ); + + state = blocks( state, { + type: 'SET_HAS_CONTROLLED_INNER_BLOCKS', + clientId: 'template', + hasControlledInnerBlocks: true, + } ); + + state = blocks( state, { + type: 'REPLACE_INNER_BLOCKS', + rootClientId: 'template', + blocks: [ contentBlock ], + } ); + + // Reset blocks completely, we expect that the controlled blocks are forgotten. + state = blocks( state, { + type: 'RESET_BLOCKS', + blocks: [], + } ); + + // Reset back to the template. + state = blocks( state, { + type: 'RESET_BLOCKS', + blocks: [ templateBlock ], + } ); + + // Expect that the `template`/`content` blocks are reconstructed. + const fullState = { blocks: state }; + expect( getBlocks( fullState, 'template' ) ).toEqual( [] ); + expect( getBlockOrder( fullState, 'template' ) ).toEqual( + [] + ); + } ); + it( 'should not leave stale controlled tree entries after root replacement and reset', () => { const templateBlock = { clientId: 'template', From fa003c13d243d12f01353398258a90b312f9d6cf Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Thu, 19 Mar 2026 11:58:52 +0100 Subject: [PATCH 4/4] Address feedback --- packages/block-editor/src/store/reducer.js | 42 +++++++--------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 890d2271fc4cfb..43a5365da7ee4d 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -601,25 +601,19 @@ const withBlockReset = ( reducer ) => ( state, action ) => { if ( action.type === 'RESET_BLOCKS' ) { /** * Preserve controlled inner block flags across RESET_BLOCKS. - * Previously this was cleared to `{}`, which caused nested - * controllers (e.g. post-content, patterns) to lose their - * controlled status and unnecessarily re-clone blocks. Stale - * flags are cleaned up naturally by unsetControlledBlocks() - * when useBlockSync unmounts. + * If there are old and new blocks that: + * - have the same `clientId` + * - have the `controlledInnerBlocks` flag + * - don't have any own, uncontrolled children + * then we preserve the `controlledInnerBlocks` flag and the controlled sub-trees. + * Nested controllers (e.g., `post-content`, patterns) don't lose their + * controlled status and don't unnecessarily re-clone blocks. */ - - const newState = { - byClientId: new Map( - getFlattenedBlocksWithoutAttributes( action.blocks ) - ), - attributes: new Map( getFlattenedBlockAttributes( action.blocks ) ), - order: mapBlockOrder( action.blocks ), - parents: new Map( mapBlockParents( action.blocks ) ), - controlledInnerBlocks: {}, - tree: new Map(), - }; - - updateBlockTreeForBlocks( newState, action.blocks ); + const newState = reducer( undefined, { + type: 'INSERT_BLOCKS', + rootClientId: '', + blocks: action.blocks, + } ); const preservedControlledInnerBlocks = state?.controlledInnerBlocks ?? {}; @@ -675,12 +669,6 @@ const withBlockReset = ( reducer ) => ( state, action ) => { for ( const clientId of Object.keys( newState.controlledInnerBlocks ) ) { - if ( ! newState.controlledInnerBlocks[ clientId ] ) { - continue; - } - if ( ! newState.byClientId.has( clientId ) ) { - continue; - } const controlledOrder = newState.order.get( clientId ); if ( ! controlledOrder?.length ) { continue; @@ -705,12 +693,6 @@ const withBlockReset = ( reducer ) => ( state, action ) => { controlledOrder.forEach( preserveTreeEntry ); } - newState.tree.set( '', { - innerBlocks: action.blocks.map( ( subBlock ) => - newState.tree.get( subBlock.clientId ) - ), - } ); - return newState; }