티스토리 수익 글 보기

티스토리 수익 글 보기

Block API: Fallback to all attributes when checking for unmodified block · WordPress/gutenberg@7067c14 · GitHub
Skip to content

Commit 7067c14

Browse files
committed
Block API: Fallback to all attributes when checking for unmodified block
1 parent c6d4da9 commit 7067c14

File tree

2 files changed

+17
2
lines changed

2 files changed

+17
2
lines changed

packages/blocks/src/api/test/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,14 @@ describe( 'isUnmodifiedBlock', () => {
495495
expect( isUnmodifiedBlock( block, 'non-existent-role' ) ).toBe( true );
496496
} );
497497

498+
it( 'should return false if no attributes exist for the role and some are modified', () => {
499+
const block = createBlock( 'core/test-block', {
500+
align: 'center',
501+
content: 'Updated content',
502+
} );
503+
expect( isUnmodifiedBlock( block, 'non-existent-role' ) ).toBe( false );
504+
} );
505+
498506
it( 'should return true if metadata attributes is not modified for role content', () => {
499507
const block = createBlock( 'core/test-block' );
500508
expect( isUnmodifiedBlock( block, 'content' ) ).toBe( true );

packages/blocks/src/api/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,27 @@ export function isUnmodifiedBlock( block, role ) {
4343
const blockAttributes = getBlockType( block.name )?.attributes ?? {};
4444

4545
// Filter attributes by role if a role is provided.
46-
const attributesToCheck = role
46+
const attributesByRole = role
4747
? Object.entries( blockAttributes ).filter( ( [ key, definition ] ) => {
4848
// A special case for the metadata attribute.
4949
// It can include block bindings that serve as a source of content,
5050
// without directly modifying content attributes.
5151
if ( role === 'content' && key === 'metadata' ) {
52-
return true;
52+
return (
53+
Object.keys( block.attributes[ key ]?.bindings ?? {} )
54+
.length > 0
55+
);
5356
}
5457

5558
return (
5659
definition.role === role ||
5760
definition.__experimentalRole === role
5861
);
5962
} )
63+
: [];
64+
// Fallback to all attributes if no attributes match the role.
65+
const attributesToCheck = !! attributesByRole.length
66+
? attributesByRole
6067
: Object.entries( blockAttributes );
6168

6269
return attributesToCheck.every( ( [ key, definition ] ) => {

0 commit comments

Comments
 (0)