From 7067c1400a8fb549b064145693731f9cc3760ee5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 10 Dec 2025 08:05:54 +0400 Subject: [PATCH] Block API: Fallback to all attributes when checking for unmodified block --- packages/blocks/src/api/test/utils.js | 8 ++++++++ packages/blocks/src/api/utils.js | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/blocks/src/api/test/utils.js b/packages/blocks/src/api/test/utils.js index 71f4000ea105de..26ad5e0222d837 100644 --- a/packages/blocks/src/api/test/utils.js +++ b/packages/blocks/src/api/test/utils.js @@ -495,6 +495,14 @@ describe( 'isUnmodifiedBlock', () => { expect( isUnmodifiedBlock( block, 'non-existent-role' ) ).toBe( true ); } ); + it( 'should return false if no attributes exist for the role and some are modified', () => { + const block = createBlock( 'core/test-block', { + align: 'center', + content: 'Updated content', + } ); + expect( isUnmodifiedBlock( block, 'non-existent-role' ) ).toBe( false ); + } ); + it( 'should return true if metadata attributes is not modified for role content', () => { const block = createBlock( 'core/test-block' ); expect( isUnmodifiedBlock( block, 'content' ) ).toBe( true ); diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index d9b7da0094ed96..55b104c569227a 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -43,13 +43,16 @@ export function isUnmodifiedBlock( block, role ) { const blockAttributes = getBlockType( block.name )?.attributes ?? {}; // Filter attributes by role if a role is provided. - const attributesToCheck = role + const attributesByRole = role ? Object.entries( blockAttributes ).filter( ( [ key, definition ] ) => { // A special case for the metadata attribute. // It can include block bindings that serve as a source of content, // without directly modifying content attributes. if ( role === 'content' && key === 'metadata' ) { - return true; + return ( + Object.keys( block.attributes[ key ]?.bindings ?? {} ) + .length > 0 + ); } return ( @@ -57,6 +60,10 @@ export function isUnmodifiedBlock( block, role ) { definition.__experimentalRole === role ); } ) + : []; + // Fallback to all attributes if no attributes match the role. + const attributesToCheck = !! attributesByRole.length + ? attributesByRole : Object.entries( blockAttributes ); return attributesToCheck.every( ( [ key, definition ] ) => {