Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/blocks/src/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
11 changes: 9 additions & 2 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,27 @@ 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 ?? {} )
Comment thread
Mamaduka marked this conversation as resolved.
.length > 0
);
Comment on lines +52 to +55
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped on this idea originally, but it became an obvious choice when working on this fix. Otherwise, the fallback doesn't work.

}

return (
definition.role === 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 ] ) => {
Expand Down
Loading