Skip to content

Commit

Permalink
Add ancestor polyfill during block registration
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Mar 31, 2022
1 parent 8fe0545 commit 76ce7e0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
serverSideBlockDefinitions[ blockName ].apiVersion =
definitions[ blockName ].apiVersion;
}
// The `ancestor` prop is not included in the definitions shared
// from the server yet, so it needs to be polyfilled as well.
// @see https://github.com/WordPress/gutenberg/pull/39894
if (
serverSideBlockDefinitions[ blockName ].ancestor ===
undefined &&
definitions[ blockName ].ancestor
) {
serverSideBlockDefinitions[ blockName ].ancestor =
definitions[ blockName ].ancestor;
}
continue;
}
serverSideBlockDefinitions[ blockName ] = mapKeys(
Expand All @@ -187,6 +198,7 @@ function getBlockSettingsFromMetadata( { textdomain, ...metadata } ) {
'title',
'category',
'parent',
'ancestor',
'icon',
'description',
'keywords',
Expand Down
36 changes: 36 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,42 @@ describe( 'blocks', () => {
} );
} );

// This test can be removed once the polyfill for ancestor gets removed.
it( 'should apply ancestor on the client when not set on the server', () => {
const blockName = 'core/test-block-with-ancestor';
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
category: 'widgets',
},
} );
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
ancestor: 'core/test-block-ancestor',
category: 'ignored',
},
} );

const blockType = {
title: 'block title',
};
registerBlockType( blockName, blockType );
expect( getBlockType( blockName ) ).toEqual( {
ancestor: 'core/test-block-ancestor',
name: blockName,
save: expect.any( Function ),
title: 'block title',
category: 'widgets',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
providesContext: {},
usesContext: [],
keywords: [],
supports: {},
styles: [],
variations: [],
} );
} );

it( 'should validate the icon', () => {
const blockType = {
save: noop,
Expand Down

0 comments on commit 76ce7e0

Please sign in to comment.