Skip to content

Commit

Permalink
Block registration: normalize blockType.parent to array (WordPress#…
Browse files Browse the repository at this point in the history
…66250)

Co-authored-by: oandregal <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: mcsf <[email protected]>
  • Loading branch information
4 people authored and karthick-murugan committed Nov 13, 2024
1 parent 77b2ea4 commit 39b665d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
14 changes: 2 additions & 12 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1628,18 +1628,8 @@ const isBlockVisibleInTheInserter = (
checkedBlocks.add( blockName );

// If parent blocks are not visible, child blocks should be hidden too.
//
// In some scenarios, blockType.parent may be a string.
// A better approach would be sanitize parent in all the places that can be modified:
// block registration, processBlockType, filters, etc.
// In the meantime, this is a hotfix to prevent the editor from crashing.
const parent =
typeof blockType.parent === 'string' ||
blockType.parent instanceof String
? [ blockType.parent ]
: blockType.parent;
if ( Array.isArray( parent ) ) {
return parent.some(
if ( Array.isArray( blockType.parent ) ) {
return blockType.parent.some(
( name ) =>
( blockName !== name &&
isBlockVisibleInTheInserter(
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking changes

- Normalize `blockType.parent` to be an array. While string values were never supported, they appeared to work with some unintended side-effects that have been fixed by [#66250](https://github.com/WordPress/gutenberg/pull/66250). For that reason, we've added some code that automatically migrates strings to arrays — though it still raises a warning.

## 13.10.0 (2024-10-16)

## 13.9.0 (2024-10-03)
Expand Down
9 changes: 0 additions & 9 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,6 @@ export function registerBlockType( blockNameOrMetadata, settings ) {
return;
}

if ( 1 === settings?.parent?.length && name === settings.parent[ 0 ] ) {
warning(
'Block "' +
name +
'" cannot be a parent of itself. Please remove the block name from the parent list.'
);
return;
}

if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( name ) ) {
warning(
'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block'
Expand Down
33 changes: 33 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,39 @@ describe( 'blocks', () => {
} );
} );

it( 'should transform parent string to array', () => {
const blockType = {
save: noop,
category: 'text',
title: 'block title',
parent: 'core/paragraph',
};
const block = registerBlockType(
'core/test-block-parent-string',
blockType
);
expect( console ).toHaveWarnedWith(
'Parent must be undefined or an array of strings (block types), but it is a string.'
);
expect( block ).toEqual( {
name: 'core/test-block-parent-string',
save: noop,
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
providesContext: {},
usesContext: [],
keywords: [],
selectors: {},
supports: {},
styles: [],
variations: [],
blockHooks: {},
parent: [ 'core/paragraph' ],
} );
} );

describe( 'applyFilters', () => {
afterEach( () => {
removeAllFilters( 'blocks.registerBlockType' );
Expand Down
36 changes: 36 additions & 0 deletions packages/blocks/src/store/process-block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,41 @@ export const processBlockType =
return;
}

if (
typeof settings?.parent === 'string' ||
settings?.parent instanceof String
) {
settings.parent = [ settings.parent ];
warning(
'Parent must be undefined or an array of strings (block types), but it is a string.'
);
// Intentionally continue:
//
// While string values were never supported, they appeared to work with some unintended side-effects
// that have been fixed by [#66250](https://github.com/WordPress/gutenberg/pull/66250).
//
// To be backwards-compatible, this code that automatically migrates strings to arrays.
}

if (
! Array.isArray( settings?.parent ) &&
settings?.parent !== undefined
) {
warning(
'Parent must be undefined or an array of block types, but it is ',
settings.parent
);
return;
}

if ( 1 === settings?.parent?.length && name === settings.parent[ 0 ] ) {
warning(
'Block "' +
name +
'" cannot be a parent of itself. Please remove the block name from the parent list.'
);
return;
}

return settings;
};

0 comments on commit 39b665d

Please sign in to comment.